Firebase推送通知在后台振动
我正在使用com.google.firebase:firebase-messaging:10.0.1
进行Android和尝试使设备在应用程序处于后台并收到推送通知时进行振动。 我发送以下JSON来FCM服务器:Firebase推送通知在后台振动
to: 'topics/...',
notification: {
title: 'New Message',
body: 'top security body',
sound: 'mysound',
action_click: 'top_security'
}
当应用程序在前台,被触发的FirebaseMessagingService
的onMessageReceived
方法,我可以添加震动。但是,当应用程序在后台,onMessageReceived
不叫,我无法控制振动。我的第一个想法是使用data
块代替背景和前景,但如果没有notification
块,iOS客户端不会在后台接收到推送。
那么如何添加振动来推送通知,当应用程序在后台? P.S.当我在设备上打开振动模式时,推送通知会导致振动而不是播放声音。
使用data
有效载荷而不是notification
有效载荷,因为即使应用程序是背景或前景,数据有效载荷也会触发onMessageReceived
。您的要求将这个样子
data: {
title: 'New Message',
body: 'top security body',
sound: 'mysound',
action_click: 'top_security'
}
从data
有效载荷获取里面的数据onMessageReceived
,叫remoteMessage.getData();
将在一个Map<String,String>
返回结果。例如:
Map<String, String> data = remoteMessage.getData();
String title = data.get("title");
String body = data.get("body");
然后定制根据您的需要通知该数据。
希望这有助于:)
我们使用推送通知android和ios。如果没有'notification'块,iOS客户端不会在后台显示推送通知。 – IlyaEremin
您还可以在一个请求中组合两个有效负载(通知和数据)。你试过了吗? @IlyaEremin – Wilik
,因为您需要发送无声通知,按照这个[链接](http://stackoverflow.com/questions/37570200/firebase-silent-apns-notification) –
请参阅我的回答在这个链接,http://stackoverflow.com/a/42505362/1404798 – Thirumalvalavan
@mahmoudmoustafa我正在使用android,看标签 – IlyaEremin