应用程序关闭时的 Firebase 通知(问题)

Firebase Notification when application is close (Issue)

各位,我正在使用 firebase 从服务器接收推送通知。当应用程序 运行 时一切正常。我收到了通知,我处理它并在通知托盘上显示它。看起来很完美。这是我的代码。

public class FirebasePushService extends FirebaseMessagingService {
    private static final String TAG = "FireBase main service ";

    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        Log.d(TAG, "Got Message: " + remoteMessage.getFrom());
        try {
            if (remoteMessage != null && remoteMessage.getNotification() != null
                    && remoteMessage.getNotification().getBody() != null) {
                String body = remoteMessage.getNotification().getBody();

                Log.d(TAG, "From: " + remoteMessage.getFrom());
                Log.d(TAG, "Notification Message Body: " + body );

            }
        } catch (JSONException e) {
            e.printStackTrace();
        }


    }}

清单代码。

 <service android:name="app.asparagus.com.asparagus.firebase.FirebasePushService">
            <intent-filter>
                <action android:name="com.google.firebase.MESSAGING_EVENT" />
            </intent-filter>
        </service>

。问题是应用程序何时关闭。这个 class 没有日志,什么都没有。但这是有趣的部分。我可以从服务器看到整个 JSON,它显示在通知托盘上(显示整个 JSON 对象)。真的没有明白我的代码有什么问题。检查图像。 1- 成功案例。

2- 未知问题案例

据此link,我认为该消息应该同时包含通知和数据负载。

或者您可以像这样将消息的优先级设置为高one

{
  "to" : "bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...",
  "priority" : "high",
  "notification" : {
    "body" : "This week's edition is now available.",
    "title" : "NewsMagazine.com",
    "icon" : "new"
  },
  "data" : {
    "volume" : "3.21.15",
    "contents" : "http://www.news-magazine.com/world-week/21659772"
  }
}

很抱歉回复晚了,我已经找到了解决方案,以防万一其他人对 it.Here 有疑问来自 firebase 文档。 消息类型

使用 FCM,您可以向客户端发送两种消息:

通知消息,有时被认为是“显示消息。Data messages,由客户端应用处理。 通知消息 包含一组预定义的 user-visible 键。 数据消息,相比之下,仅包含自定义 key-value 对。 通知消息 可以包含一个可选的数据负载,当用户点击通知时传送。

使用场景
通知消息 FCM 代表客户端应用程序自动向 end-user 设备显示消息。通知消息具有一组预定义的 user-visible 键和自定义 key-value 对的可选数据负载。

发送方式 在 Cloud Functions 或您的应用程序服务器等受信任的环境中,使用 Admin SDK 或 HTTP 和 XMPP API:设置通知密钥。可能有可选的数据负载。总是可折叠的。 使用通知编辑器:输入消息文本、标题等,然后发送。通过提供自定义数据来添加可选的数据负载。始终可折叠。

使用场景

数据消息客户端应用负责处理数据消息。数据消息只有自定义 key-value 对。 在 Cloud Functions 或您的应用程序服务器等受信任的环境中,使用 Admin SDK 或 HTTP 和 XMPP API:仅设置数据密钥。可以是可折叠的或 non-collapsible.

Link