17

Is it possible to make an Android device vibrate when receiving notifications in the background with Firebase Cloud Messaging? From the Syntax Reference I have seen and tested that sound is supported, but no info on vibration. Or is it somehow possible to provide a customized implementation for this notification where I would directly set the vibration?

apidae
  • 529
  • 2
  • 5
  • 14
  • 1
    I think you need to implement on upon building the Notification. Have you seen this [post](http://stackoverflow.com/q/18253482/4625829) before? – AL. Jul 19 '16 at 08:17
  • 2
    The thing with FCM is that when a notification is sent and the application is in the background the [Notification is built internally](https://firebase.google.com/docs/cloud-messaging/concept-options#notifications). I think the solution to customizing the notification is to only deliver a data payload, thus [guaranteeing that my service's onMessageReceived() method will be called](https://firebase.google.com/docs/cloud-messaging/concept-options#notifications_and_data_messages) and then building the notification in the method implementation – apidae Jul 19 '16 at 08:47
  • See also https://stackoverflow.com/questions/42555899/firebase-push-notifications-vibrate-in-background – Kato Sep 15 '17 at 17:07

4 Answers4

30

You should mark yourself as a correct answer.

On a sidenote, if you do send a Notification and a Data payload, use

"notification":{
    "sound":"default"
}

This will play the sound when the application is in background and the device is set to sound, and it will play a vibration if the device is set to vibrate.

However, as far as customization goes I think you need to use the Data Payload only if you want to customize the vibration pattern for example.

buddhabath
  • 664
  • 1
  • 10
  • 22
7

From what I have tested the format of the FCM Notification message seems to be quite rigid, so for customized notifications, the solution seems to be:

  1. Making sure that the payload of the JSON message being sent downstream contains the data parameter but doesn't contain the notification parameter (Sources: Message JSON Syntax, FCM Message Types)
  2. Creating the Notification in the implementation of the overridden onMessageReceived(RemoteMessage remoteMessage) from the application's FirebaseMessagingService (Example)
apidae
  • 529
  • 2
  • 5
  • 14
  • is this just Notification **sound** or also **vibration**? – newbieguy Dec 29 '17 at 11:14
  • What if I use same message for iOS and Android? I cannot avoid `notification` object in the message. Adding `sound` field doesn't vibrate in the background though... – Igor SKRYL Mar 30 '18 at 08:28
0

It's now supported by Firebase. The notification should look like this:

{...
    default_vibrate_timings: false,
    vibrate_timings: [
                "0.0s",
                "0.2s",
                "0.1s",
                "o.2s"
     ],
...}

Official docs

alizeyn
  • 2,300
  • 1
  • 20
  • 30
-5

array("title"=>"hello","body" =>"hello world",'sound' => 'default')

geert3
  • 7,086
  • 1
  • 33
  • 49
Ramya Roy
  • 229
  • 4
  • 10
  • function send_notification($tokens,$notification) { $url = 'https://fcm.googleapis.com/fcm/send'; $priority="high"; $fields = array( 'registration_ids' => $tokens, 'notification' => $notification ); – Ramya Roy Jan 10 '17 at 05:41