jer*_*use 5 android push-notification android-notifications firebase firebase-cloud-messaging
我正在制作一个 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());
}
}
Run Code Online (Sandbox Code Playgroud)
PS 在浏览Telegram 的代码库时,我发现他们总是在前台运行他们的应用程序
<service android:name=".BringAppForegroundService" android:enabled="true"/>
他们就是这样解决通知问题的,还是还有比表面上看到的更多的事情?
去找你的后端开发人员并告诉他不要在 firebase 通知中使用通知对象。仅发送带有to参数的数据对象。
不要从服务器发送通知对象。因为如果您notification在 中使用对象json,当您的应用程序处于后台时,您的应用程序将不会显示推送通知。
例如
{
"to": "e1w6hEbZn-8:APA91bEUIb2JewYCIiApsMu5JfI5Ak...",
"notification": {
"body": "Cool offers. Get them before expiring!",
"title": "Flat 80% discount",
"icon": "appicon"
}
}
Run Code Online (Sandbox Code Playgroud)
您的后端开发人员必须像这样发送 json:
{
"data": {
"score": "5x1",
"time": "15:10"
},
"to" : "bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1..."
}
Run Code Online (Sandbox Code Playgroud)
如果您使用data only messages,onMessageReceived()方法在两种情况下都会被调用,即使您的应用程序位于foreground或background。然后只需使用 即可获得响应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");
...
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3471 次 |
| 最近记录: |