whatsapp / telegram如何确保来电通知?

时间:2018-04-07 10:05:47

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

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

当app在后台时,不会调用

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());
    }
}

PS 在浏览Telegram's codebase时,我发现他们始终在前台运行他们的应用

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

他们是如何解决通知的,还是有什么比满足眼睛的?

1 个答案:

答案 0 :(得分:3)

转到后端开发人员并告诉他不要在firebase通知中使用通知对象。仅使用参数发送数据对象。

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

例如。

{
    "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");
 ...
}