当应用程序处于后台或未运行时,推送通知无法正常工作 [英] Push notification works incorrectly when app is on background or not running

查看:37
本文介绍了当应用程序处于后台或未运行时,推送通知无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am using Firebase Cloud Messaging to send push notifications.

这是我的 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());
    }
}

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 未显示在状态栏中.
为什么会发生这种情况,我该如何解决?

So issue is that when App is running ticker is shown well and notification come with default sound, but when App is in background or is not running, notification come without any sound and ticker is not shown in status bar.
Why is this happening and how can I fix it?

推荐答案

使用 FCM,您可以指定要发送到 https://fcm.googleapis.com/fcm/send 的 POST 有效负载.在该负载中,您可以指定 datanotification 键,或两者都指定.

With FCM you specify a POST payload to send to https://fcm.googleapis.com/fcm/send. In that payload you can specify a data or a notification key, or both.

如果您的负载仅包含一个 data 键,您的应用将自行处理所有推送消息.例如.它们都被传送到您的 onMessageReceived 处理程序.

If your payload contains only a data key, your app will handle all push messages itself. E.g. they are all delivered to your onMessageReceived handler.

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

If your payload contains a notification key, your app will handle push messages itself only if your app is active/in the foreground. If it is not (so it's in the background, or closed entirely), FCM handles showing the notification for you by using the values you put into the notification key payload.

请注意,从控制台(如 Firebase 控制台)发送的通知始终包含一个 notification 键.

Note that notifications sent from a console (like Firebase console), they always include a notification key.

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

Looks like you want to be handling the FCM messages yourself so you can customize the notification a bit more etc, so it would be better to not include the notification key in the POST payload, so all push messages are delivered to your onMessageReceived.

您可以在此处阅读更多相关信息:
高级消息传递选项
下游消息语法

You can read more about it here:
Advanced messaging options
Downstream message syntax

这篇关于当应用程序处于后台或未运行时,推送通知无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆