nymvno
nymvno

Reputation: 400

Handle Received message firebase notifications

I want to trigger a specific event (like ordinary vibration) when the device gets an notification from the Firebase Notifications.

All I discovered so far is that one can handle the on_click of a notification that was sent with display-messages in the background of the app.

Is it possible to let the device vibrate in the very moment the notification arrives? I would love to get the users attention to participate on my field study by answering the question sheet in the moment, the notification comes in.

Thanks alot!

Upvotes: 0

Views: 237

Answers (2)

Frank van Puffelen
Frank van Puffelen

Reputation: 598765

There are two types of messages:

  • data messages (with a data property in the JSON)
  • notification messages (with only a notification property in the JSON)

If a notification/data message arrives while your app is active, you can handle it in onMessageReceived and do whatever you want.

If a data message arrives while your app is inactive, you can handle it in onMessageReceived and do whatever you want.

To make the phone vibrate in these cases, see the excellent example from Wizard.

If a notification message arrives while your app is inactive, it is automatically handled by the system and you can't control what happens.

Also see the documentation on message types as there are some more nuances.

Upvotes: 1

Paresh
Paresh

Reputation: 6857

Is it possible to let the device vibrate in the very moment the notification arrives?

Yes, you can define it while generating Notification using NotificationManager class -

NotificationCompat.Builder mBuilder =
                    (NotificationCompat.Builder) new NotificationCompat.Builder(this)
                            .setSmallIcon(R.drawable.ic_notification)
                            .setAutoCancel(true)
                            ....
                            ..
                            .setVibrate(new long[] { 1000, 1000, 1000, 1000, 1000 });

Upvotes: 1

Related Questions