当应用程序在后台或未运行时,推送通知无法正常工作

2024-01-11

我正在使用 Firebase Cloud Messaging 发送推送通知。

这是我的FirebaseMessageService:

public class FireBaseMessageService extends FirebaseMessagingService {


@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    Log.e("TAG", "From: " + remoteMessage.getFrom());
    Log.e("TAG", "Notification Message Body: " + remoteMessage.getData().get("CardName")+"  :  "+remoteMessage.getData().get("CardCode"));
    sendNotification(remoteMessage.getNotification().getBody());
}


private void sendNotification(String messageBody) {
    Intent intent = new Intent(this, StartActivity.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
            PendingIntent.FLAG_ONE_SHOT);

    Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
            .setSmallIcon(R.mipmap.ic_launcher_final)
            .setContentTitle("Notification")
            .setContentText(messageBody)
            .setTicker("Test")
            .setAutoCancel(true)
            .setDefaults(Notification.DEFAULT_SOUND)
            .setContentIntent(pendingIntent);

    NotificationManager notificationManager =
            (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

        notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
    }
}

And FirebaseInstanceServer:

public class FirebaseInstanceService extends FirebaseInstanceIdService {


@Override
public void onTokenRefresh() {
    // Get updated InstanceID token.
    String refreshedToken = FirebaseInstanceId.getInstance().getToken();
    Log.e("TAG", "Refreshed token: " + refreshedToken);

    // TODO: Implement this method to send any registration to your app's servers.
    sendRegistrationToServer(refreshedToken);

}

private void sendRegistrationToServer(String token) {


      // Add custom implementation, as needed.
        Log.e("TAG", "Refreshed token2: " + token);
    }
}

其中声明于AndroidManifest:

<service
    android:name=".util.notifications.FireBaseMessageService">
    <intent-filter>
        <action android:name="com.google.firebase.MESSAGING_EVENT"/>
    </intent-filter>
</service>

<service
    android:name=".util.notifications.FirebaseInstanceService">
    <intent-filter>
        <action android:name="com.google.firebase.INSTANCE_ID_EVENT"/>
    </intent-filter>
</service>

所以问题是当应用程序运行时ticker显示良好,通知带有默认声音,但是当应用程序在后台或未运行时,通知没有任何声音,并且ticker不显示在状态栏中。
为什么会发生这种情况以及如何解决它?


使用 FCM,您可以指定要发送到的 POST 有效负载https://fcm.googleapis.com/fcm/send。在该有效负载中,您可以指定data or a notification键,或两者兼而有之。

如果您的有效负载仅包含data关键,您的应用程序将自行处理所有推送消息。例如。它们都已交付给您onMessageReceived处理程序。

如果您的有效负载包含notification关键,您的应用程序将自行处理推送消息only如果您的应用程序处于活动状态/在前台。如果不是(因此它在后台,或完全关闭),FCM 会使用您放入的值来处理为您显示通知notification关键有效负载。

请注意,从控制台(例如 Firebase 控制台)发送的通知,它们始终包含notification key.

看起来您想自己处理 FCM 消息,以便您可以更多地自定义通知等,因此最好不要包含notification在 POST 有效负载中键入密钥,以便所有推送消息都会传递到您的onMessageReceived.

你可以在这里读更多关于它的内容:
高级消息传递选项 https://firebase.google.com/docs/cloud-messaging/concept-options
下游消息语法 https://firebase.google.com/docs/cloud-messaging/http-server-ref#send-downstream

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

当应用程序在后台或未运行时,推送通知无法正常工作 的相关文章