Reputation: 3397
I'm using com.google.firebase:firebase-messaging:10.0.1
for Android and trying to make the device vibrate while application is in background and a push notification is received.
I send following json to FCM server:
to: 'topics/...',
notification: {
title: 'New Message',
body: 'top security body',
sound: 'mysound',
action_click: 'top_security'
}
When the app is in foreground, the onMessageReceived
method of FirebaseMessagingService
is triggered and I can add vibration. But, when the app is in background, onMessageReceived
is not called and I have no control on the vibration. My first thought was to use data
block instead both for background and foreground, but iOS client does not receive pushes in background if there is no notification
block.
So how can I add vibration to push notification when the app is in background? P.S. When I turn on vibrate mode on the device, the push notification cause vibrating instead of playing the sound.
Upvotes: 3
Views: 7236
Reputation: 7720
Use data
payload instead of notification
payload because data payload will trigger onMessageReceived
even though the app is background or foreground. Your request will look like this
data: {
title: 'New Message',
body: 'top security body',
sound: 'mysound',
action_click: 'top_security'
}
To retrieve the data from data
payload inside onMessageReceived
, call remoteMessage.getData();
which will return the result in a Map<String,String>
For example:
Map<String, String> data = remoteMessage.getData();
String title = data.get("title");
String body = data.get("body");
Then customize the notification as you want with that data.
Hope this helps :)
Upvotes: 2