0

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!

nymvno
  • 155
  • 2
  • 12

2 Answers2

1

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 });
Paresh P.
  • 5,913
  • 1
  • 11
  • 23
  • Thank you very much! This answer got me close to what I try to achieve and helped me out a lot! – nymvno Mar 24 '17 at 14:21
  • You're welcome. Don't forget to upvote if the answer is useful, it makes the answer trustworthy to others. – Paresh P. Mar 24 '17 at 15:57
1

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.

Frank van Puffelen
  • 418,229
  • 62
  • 649
  • 645
  • There you go, with all you need! – Paresh P. Mar 24 '17 at 13:13
  • Thank you for this helpful advice! Notification messages are simply done over the console but how do I send data messages? – nymvno Mar 24 '17 at 14:22
  • You can only send *notification* messages from the Notifications console. Data messages can only be sent through the API. A simple CURL request can be enough though: http://stackoverflow.com/questions/37371990/how-can-i-send-a-firebase-cloud-messaging-notification-without-use-the-firebase (although that snippet send a notification message, you can easily it to send a data message). – Frank van Puffelen Mar 24 '17 at 16:07