Firebase 通知在后台不起作用

6

我需要帮助。Firebase通知在后台无法工作。这是我的代码:

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {

    Log.d(TAG, "FROM:" + remoteMessage.getFrom());
   sharedPreference = getSharedPreferences(Global.SECURETRADE, 0);
    UID = sharedPreference.getString(Global.ID, "");


        Uri defaultSoundUri=RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)`enter code here`;

        NotificationCompat.Builder notificationBuilder = new
                NotificationCompat.Builder(this);

        if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {

            notificationBuilder.setSmallIcon(R.mipmap.small_secure_trade_app_icon);

        } else {

            notificationBuilder.setSmallIcon(R.drawable.small_secure_trade_app_icon);
        }

        notificationBuilder.setLargeIcon(BitmapFactory.decodeResource(this.getResources(), R.drawable.securetrade_icon));
        notificationBuilder.setContentTitle(remoteMessage.getData().get("title"));
        notificationBuilder.setContentText(remoteMessage.getData().get("body"));
        notificationBuilder.setAutoCancel(true);
        notificationBuilder.setSound(defaultSoundUri);
        notificationBuilder.setContentIntent(pendingIntent);

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

        notificationManager.notify(0, notificationBuilder.build());

    }




}

你是如何发送消息的? - AL.
我也有同样的疑问,在此之前它是正常工作的。 - Raut Darpan
我也在想为什么它不工作,Firebase 给出了很多错误,之前它是可以工作的。 - Raut Darpan
你能发一下你的清单吗? - Anil
当应用程序在后台运行时,“onMessageReceived”将不会被触发。您必须像这样处理它https://dev59.com/DloU5IYBdhLWcg3wi3WW#37395785。或者尝试这个https://dev59.com/DloU5IYBdhLWcg3wi3WW#43869272(未尝试)。 - Sunil Sunny
尝试我的解决方案吗?<br> 我已经尝试过了。<br> 而且它有效。<br>https://dev59.com/36nka4cB1Zd3GeqPKjir#48899186 - Leon Chang
5个回答

2

1
只需从通过推送通知发送的json中删除“notification”部分。只需发送“data”部分,onMessageReceived将正常工作。

不,它只在托盘中显示通知,没有显示消息。 - Aman Deep

0

0
你是否正在发送数据消息(而不是通知消息)? 通知消息不会调用onMessageReceived()

当你想让 FCM 代表你的客户端应用程序处理显示通知时,请使用通知消息。当你想在客户端应用程序上处理消息时,请使用数据消息。

在此阅读更多信息:https://firebase.google.com/docs/cloud-messaging/concept-options#notifications_and_data_messages

附注:FCM Web 控制台始终发送通知消息。


如果您正在发送一个数据消息,但是onMessageReceived()没有被调用...那么这就是一个不同的问题。

这甚至可能是该特定设备的问题。
请参见使用FCM推送通知在Android应用程序被杀死时未收到


-1

当应用程序在后台运行时,onMessageReceive将不起作用。

离线消息将发送到启动器活动,只需将此代码片段复制到您的启动器活动中并进行检查即可。它会起作用。

    Bundle bundle = getIntent().getExtras();
    if (bundle != null) {
        Logger.info(TAG, "FIRE BASE OFF LINE NOTIFICATIONS COMING TO THIS BLOCK--->");

        JSONObject json = new JSONObject();
        Set<String> keys = bundle.keySet();
        for (String key : keys) {
            Logger.info(TAG, "json object--->" + key + "---values--" + JSONObject.wrap(bundle.get(key)));
        }
    }

而你的有效载荷应该长这样

{ "notification": {
    "title": "Your Title",
    "text": "Your Text",
     "click_action": "OPEN_ACTIVITY_1" // should match to your intent filter
  },
    "data": {
    "keyname": "any value " //you can get this data as extras in your activity and this data is optional
    },
  "to" : "to_id(firebase refreshedToken)"
}

参考资料


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接