Firebase推送通知在背景中振动

时间:2017-03-02 12:44:37

标签: android firebase push-notification firebase-cloud-messaging

我正在使用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客户端不会在后台接收推送。

那么当应用程序处于后台时,如何添加振动以推送通知? 附:当我打开设备上的振动模式时,推送通知会导致振动而不是播放声音。

1 个答案:

答案 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");

然后根据需要自定义通知。

希望这会有所帮助:)