【问题标题】:Push notification works incorrectly when app is on background or not running当应用程序处于后台或未运行时,推送通知无法正常工作
【发布时间】:2016-10-18 23:28:39
【问题描述】:

我正在使用 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());
    }
}

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

【问题讨论】:

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


    【解决方案1】:

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

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

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

    请注意,从控制台(如 Firebase 控制台)发送的通知总是包含 notification 键。

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

    您可以在此处阅读更多信息:
    Advanced messaging options
    Downstream message syntax

    【讨论】:

    • 谢谢,你是对的,当我删除 notification 键时,我开始正确接收通知
    • Tim 是正确的,我想补充一点,从 Firebase 控制台发送的消息是通知消息,因此如果您确实使用 Firebase 控制台,则必须相应地处理它们。
    • @tim-castelijns 你确定后台仍然收到数据消息吗?直到最近它才对我有用。我在 FCM 9.2.0 下有一个工作应用程序,但是在最后一个工作室、sdk、java 更新之后,我不知道,尽管我没有对应用程序代码进行任何更改,但它不再工作了!?!跨度>
    • 是的,我确定。毕竟这是他们的用途
    • @miroslavign 我不知道。感谢分享:)
    【解决方案2】:

    我花了 2 周的时间来了解为什么我的应用程序在后台时无法再接收数据消息并且可能很奇怪,我得出的结论是 Android-Studio 2.1.2 是问题所在!

    I cannot receive any FCM message anymore in background application

    https://github.com/firebase/quickstart-android/issues/89

    【讨论】:

      【解决方案3】:
      {
      "to": "user_device_token",
      "collapse_key": "type_a",
      
      "data": {
          "body": "your message body",
           "title": "notification title",
          "description":"notification description ",
          "imageUrl":"image url",
          "key_1": "Value for key_1",
          "key_2": "Value for key_2"
             }
      }
      

      从后端的通知消息 json 响应中删除 notification

      【讨论】:

      • 这把钥匙在哪里?用户设备令牌? var message = new Message() { Data = Data, Token = token, Notification = new Notification() { Body = notificationBody, Title = title } };
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-04-28
      • 1970-01-01
      • 2016-10-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多