【问题标题】:How whatsapp/telegram ensures incoming call notification?whatsapp/telegram 如何确保来电通知?
【发布时间】:2018-09-17 06:33:17
【问题描述】:

我正在制作一个 VoIP 应用程序。即使应用程序在后台,如何确保来电通知? Google FCM 仅在应用程序处于前台时才有效(可能是由于 android 电池优化)。 Whatsapp 和 Telegram 做了什么来确保近 100% 的来电通知?

当应用在后台时,onMessageReceived 方法不会被调用。

public class MyFirebaseMessagingService extends FirebaseMessagingService {
    private static final String TAG = "FCM Service";
    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        Log.d(TAG, "From: " + remoteMessage.getFrom());
        Log.d(TAG, "Notification Message Body: " + remoteMessage.getData().toString());
    }
}

附言 在浏览Telegram's codebase 时,我发现他们总是在前台运行他们的应用程序

<service android:name=".BringAppForegroundService" android:enabled="true"/>

他们是这样解决通知的,还是有更多的东西?

【问题讨论】:

  • 您在 Firebase 通知响应中尝试过仅数据消息吗?
  • @DäñishShärmà 是的,尝试过user.sendCloudMessage({customData: data, priority: "high"})。数据在哪里{ "sinch": { ... "PushData": pushData, ... } }
  • 我也遇到了同样的问题,你找到这个问题的真正原因了吗?数据消息并不总是在小米/华为手机中传递,但电报/WhatsApp 通话正常。

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


【解决方案1】:

去找你的后端开发人员,告诉他不要在 firebase 通知中使用通知对象。仅发送带有 to 参数的 data 对象。

不要从服务器 Notification 对象发送。因为如果您在json 中使用notification 对象,当您的应用处于后台时,您的应用将不会显示推送通知。

例如

{
    "to": "e1w6hEbZn-8:APA91bEUIb2JewYCIiApsMu5JfI5Ak...",
    "notification": {
        "body": "Cool offers. Get them before expiring!",
        "title": "Flat 80% discount",
        "icon": "appicon"
    }
}

您的后端开发人员必须像这样发送 json:

{ 
"data": {
"score": "5x1",
"time": "15:10"
},
  "to" : "bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1..."
}

如果您使用data only messages,则在这两种情况下都会调用onMessageReceived() 方法,即使您的应用程序位于foregroundbackground。然后只需使用key values 获取响应。

 @Override
  public void onMessageReceived(RemoteMessage remoteMessage) {
  ...
   Map<String, String> data = remoteMessage.getData();

  //you can get your text message here.
  String text= data.get("text");
 ...
}

【讨论】:

  • 我遇到了同样的问题,在使用纯数据消息后解决了。现在应用程序通知在前台和后台两种情况下都有效,但是如果应用程序被刷卡杀死,在这种情况下,如果您没有收到通知,则在清单中添加这些权限“uses-permission android:name="com.google.android .c2dm.permission.RECEIVE" "使用权限 android:name="android.permission.WAKE_LOCK"。请参考这些链接 1) github.com/firebase/quickstart-android/issues/368 2) github.com/firebase/quickstart-android/issues/365
  • com.google.android.c2dm.permission.RECEIVE 不推荐用于 FCM,它已被 GCM 使用
猜你喜欢
  • 2016-01-10
  • 2022-06-21
  • 2020-08-19
  • 2022-08-15
  • 1970-01-01
  • 1970-01-01
  • 2014-02-10
  • 2021-05-22
  • 1970-01-01
相关资源
最近更新 更多