【问题标题】:Firebase push notifications vibrate in backgroundFirebase 推送通知在后台振动
【发布时间】:2017-03-02 12:44:37
【问题描述】:

我在 Android 上使用 com.google.firebase:firebase-messaging:10.0.1,并尝试在应用程序处于后台并收到推送通知时使设备振动。 我将以下 json 发送到 FCM 服务器:

to: 'topics/...',
notification: {
    title: 'New Message',
    body: 'top security body',
    sound: 'mysound',
    action_click: 'top_security'
}

当应用在前台时,触发FirebaseMessagingServiceonMessageReceived方法,我可以添加振动。但是,当应用程序在后台时,onMessageReceived 不会被调用,我无法控制振动。我的第一个想法是在后台和前台都使用data 块,但是如果没有notification 块,iOS 客户端不会在后台接收推送。

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

【问题讨论】:

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


【解决方案1】:

使用data 负载而不是notification 负载,因为即使应用程序是后台或前台,数据负载也会触发onMessageReceived。您的请求将如下所示

data: {
    title: 'New Message',
    body: 'top security body',
    sound: 'mysound',
    action_click: 'top_security'
}

要从onMessageReceived 内部的data 有效负载中检索数据,请调用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
猜你喜欢
  • 2020-06-26
  • 2019-01-17
  • 2017-11-06
  • 1970-01-01
  • 2017-01-03
  • 1970-01-01
  • 2020-08-02
  • 1970-01-01
  • 2023-04-11
相关资源
最近更新 更多