当应用程序未运行时处理 Android 推送通知,如 Whatsapp、Twitter、Facebook、Instagram 等

Dou*_*e_M 5 android background-service push-notification firebase firebase-cloud-messaging

我正在开发像 Whatsapp 这样的聊天应用程序。当应用程序运行时,我使用 websocket 连接来处理两个用户之间的聊天,但是当应用程序被终止或未运行时,我尝试使用 FCM 推送通知服务来通知用户何时收到消息,就像 Whatsapp 的方式一样做。

现在的问题是,当应用程序位于前台或后台时(从视图中看不到,但仍在最近的任务菜单中),一旦应用程序从最近的任务菜单中滑出或根本未启动,FCM 就会收到推送通知,没有收到通知。

我已经在这里股票了一整周,我搜索并阅读了 github、stackoverflow、quora 和一些博客文章上的各种文章和社区对话,但我还没有找到有效的东西。

我尝试使用后台服务来保持与服务器的 websocket 连接,但我无法让服务继续运行,因为当应用程序不在前台时,android 会杀死后台服务。

我的意思是像whatsapp、twitter、instagram、facebook、gmail、likee、tiktok等应用程序如何处理推送通知,这样即使应用程序关闭(从最近的菜单中滑出或根本没有启动),它仍然通知它的用户一些服务器上的更新。

这是我的代码...在服务器上

const firebase_admin = require('firebase-admin');
var service_account = require('./service_account.json');
firebase_admin.initializeApp({
    credential: firebase_admin.credential.cert(service_account),
    databaseURL: 'https://fcm_pushnotification-b9983.firebaseio.com/'
});

app.get('/sendPushNotification', (req, res) => {
    // This registration token comes from the client FCM SDKs.
    var registrationToken = 'clIilmqTRYarMF4gcrpEeH:APA91bFjkmZP7gU836ZCAzyPZaOWU4nU4SLL5OPWNkgukt0zBe0zvn5PEQ-42g60R5UXFN0tXQISjCDcbl032j2Tc81_OZ5uAJ7Aq3_OAaIz7g56oT547LnB9wiiBIKRZhc1TWGMP7lr';

    var message = {
        notification: {
            title: 'Samuel',
            body: 'This is an urgent message!',
        },
        webpush:{
            headers:{
                Urgency:'high'
            }
        },
        android:{
            priority:'high'
        },
        token: registrationToken
    };


    // Send a message to the device corresponding to the provided
    // registration token.
    firebase_admin.messaging().send(message)
      .then((response) => {
        // Response is a message ID string.
        console.log('Successfully sent message:', response);
        res.send('Successfully sent message:- '+ response);
      })
      .catch((error) => {
        console.log('Error sending message:- ', error);
        res.send('Error sending message:'+ error);
      });
});

Run Code Online (Sandbox Code Playgroud)

我在 Android 上的服务类

public class MyFirebaseMessagingService extends FirebaseMessagingService{
    /**
     * Called if InstanceID token is updated. This may occur if the security of
     * the previous token had been compromised. Note that this is called when the InstanceID token
     * is initially generated so this is where you would retrieve the token.
     */
    @Override
    public void onNewToken(@NonNull String token) {
        Log.d(TAG, "Refreshed token: " + token);

        // If you want to send messages to this application instance or
        // manage this apps subscriptions on the server side, send the
        // Instance ID token to your app server.
        sendRegistrationToServer(token);
    }

    @Override
    public void onMessageReceived(@NonNull RemoteMessage remoteMessage) {
        super.onMessageReceived(remoteMessage);
        String title = remoteMessage.getNotification().getTitle();
        String body = remoteMessage.getNotification().getBody();
        this.sendNotification(new Notification(null, title, body, 0));
    }

    private void sendNotification(Notification notification){
    // Notification channel and notification is build here.
    }

}

Run Code Online (Sandbox Code Playgroud)

清单

<uses-permission android:name="android.permission.INTERNET" />

        <service
            android:name=".Services.MyFirebaseMessagingService"
            android:exported="true">
            <intent-filter>
                <action android:name="com.google.firebase.MESSAGING_EVENT" />
            </intent-filter>
        </service>

 <!-- Set custom default icon. This is used when no icon is set for incoming notification messages. -->
        <meta-data
            android:name="com.google.firebase.messaging.default_notification_icon"
            android:resource="@drawable/ic_heart" />
        <!-- Set color used with incoming notification messages. This is used when no color is set for the incoming
             notification message. -->
        <meta-data
            android:name="com.google.firebase.messaging.default_notification_color"
            android:resource="@color/red" />
        <meta-data
            android:name="com.google.firebase.messaging.default_notification_channel_id"
            android:value="@string/notification_channel_id" />
Run Code Online (Sandbox Code Playgroud)

当应用程序未运行时,我是否需要请求此权限才能工作。我什至将服务器上的通知优先级设置得尽可能高。我对此感到沮丧。欢迎任何帮助。

Dou*_*e_M 2

对于可能遇到类似问题的任何人:

我联系了 firebase 支持团队,他们表示这是由一些原始设备制造商 (OEM) 实施的电池优化引起的问题。当应用程序在应用程序切换器中滑开时,该应用程序将被视为强制停止,这不是默认的 Android 行为。不幸的是,这样做的副作用是它可能会导致您的应用程序的 FCM 服务停止运行。

检查您的应用程序是否受到任何 OEM 电池管理功能影响的一种方法如下:

  1. 将 OEM 设备连接到 adb。
  2. 在设备上运行您的应用程序。
  3. 将应用程序从设备上最近的屏幕上滑开。
  4. 运行命令:adb shell dumpsys package MY-PACKAGE | grep stopped

如果显示stopped=true,则可以安全地假设 OEM 具有此类机制,并且您的应用程序受到此问题的影响。

您还可以阅读有关此问题的更多信息以及解决该问题的可能方法: https: //www.freecodecamp.org/news/why-your-push-notifications-never-see-the-light-of-day- 3fa297520793/