我正在使用com.google.firebase:firebase-messaging:10.0.1
用于Android,并且尝试让设备在应用程序处于后台并且收到推送通知时振动。
我将以下json发送到FCM服务器:
to: 'topics/...',
notification: {
title: 'New Message',
body: 'top security body',
sound: 'mysound',
action_click: 'top_security'
}
当应用处于前台时,会触发onMessageReceived
FirebaseMessagingService
方法,我可以添加振动。但是,当应用程序处于后台时,onMessageReceived
未被调用,我无法控制振动。我的第一个想法是使用data
块代替背景和前景,但如果没有notification
块,iOS客户端不会在后台接收推送。
那么当应用程序处于后台时,如何添加振动以推送通知? 附:当我打开设备上的振动模式时,推送通知会导致振动而不是播放声音。
答案 0 :(得分:1)
使用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");
然后根据需要自定义通知。
希望这会有所帮助:)