简体   繁体   English

当应用处于前台时,无法接收FCM通知

[英]Not able to receive FCM notifications when app is in foreground

I am working with FCM notifications. 我正在使用FCM通知。 My notifications are working perfectly when the App is in background but not able to receive a notification when the app is in foreground. 当应用程序处于后台时,我的通知工作正常,但当应用程序处于前台时无法接收通知。 I tried almost everything but not working. 我几乎尝试了一切,但没有工作。 Notifications are not received when App is in foreground. App位于前台时未收到通知。

Manifest.xml: 的Manifest.xml:

       <service
        android:name=".Notification.MyFirebaseMessagingService"
        android:exported="false">
        <intent-filter>

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

    <service android:name=".Notification.MyFirebaseInstanceIDService">
        <intent-filter>
            <action android:name="com.google.firebase.INSTANCE_ID_EVENT" />
        </intent-filter>
    </service>

Code(FirebaseMessagingService) : 代码(FirebaseMessagingService):

           @Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    Log.e(TAG, "From: " + remoteMessage.getFrom());

    if (remoteMessage == null)
        return;

    // Check if message contains a notification payload.
    if (remoteMessage.getNotification() != null) {
        Log.e(TAG, "Notification Body: " + remoteMessage.getNotification().getBody());
        handleNotification(remoteMessage.getNotification().getBody());
    }

    try {

        JSONObject json = new JSONObject(remoteMessage.getData().toString());
        handleDataMessage(json);
    } catch (Exception e) {
        Log.e(TAG, "Exception: " + e.getMessage());
    }


    Log.e(TAG, "Data Payload: " + remoteMessage.getData().toString());

}

private void handleNotification(String message) {
    if (!NotificationUtils.isAppIsInBackground(getApplicationContext())) {
        // app is in foreground, broadcast the push message
        Intent pushNotification = new Intent(Config.PUSH_NOTIFICATION);
        pushNotification.putExtra("message", message);
        LocalBroadcastManager.getInstance(this).sendBroadcast(pushNotification);

        // play notification sound
        NotificationUtils notificationUtils = new NotificationUtils(getApplicationContext());
        notificationUtils.playNotificationSound();
    } else {
        // If the app is in background, firebase itself handles the notification
    }
}

private void handleDataMessage(JSONObject json) {
    Log.e(TAG, "push json: " + json.toString());

    try {
        JSONObject data = json.getJSONObject("data");
        String title = data.getString("title");
        String message = data.getString("body");
        boolean isBackground = data.getBoolean("is_background");
        String imageUrl = data.getString("image");
        String timestamp = data.getString("timestamp");
        JSONObject payload = data.getJSONObject("payload");

        Log.e(TAG, "title: " + title);
        Log.e(TAG, "message: " + message);
        Log.e(TAG, "isBackground: " + isBackground);
        Log.e(TAG, "payload: " + payload.toString());
        Log.e(TAG, "imageUrl: " + imageUrl);
        Log.e(TAG, "timestamp: " + timestamp);


        if (!NotificationUtils.isAppIsInBackground(getApplicationContext())) {
            // app is in foreground, broadcast the push message
            Intent pushNotification = new Intent(Config.PUSH_NOTIFICATION);
            pushNotification.putExtra("message", message);
            LocalBroadcastManager.getInstance(this).sendBroadcast(pushNotification);

            // play notification sound
            NotificationUtils notificationUtils = new NotificationUtils(getApplicationContext());
            notificationUtils.showNotificationMessage("iGrab", message, timestamp, pushNotification);
            notificationUtils.playNotificationSound();


        } else {
            // app is in background, show the notification in notification tray
            Intent resultIntent = new Intent(getApplicationContext(), Splash.class);
            resultIntent.putExtra("message", message);

            // check for image attachment
            if (TextUtils.isEmpty(imageUrl)) {

                showNotificationMessage(getApplicationContext(), title, message, timestamp, resultIntent);
            } else {
                // image is present, show notification with image
                showNotificationMessageWithBigImage(getApplicationContext(), title, message, timestamp, resultIntent, imageUrl);
            }
        }
    } catch (JSONException e) {
        Log.e(TAG, "Json Exception: " + e.getMessage());
    } catch (Exception e) {
        Log.e(TAG, "Exception: " + e.getMessage());
    }
}

FCM support two types of messages: FCM支持两种类型的消息:

  • Notification message 通知消息
  • Data message 数据信息

If server send Notification message then it automatically displays a notification on system tray. 如果服务器发送通知消息,则它会自动在系统托盘上显示通知。

Use notification messages when you want FCM to handle displaying a notification on your client app's behalf. 当您希望FCM处理代表客户端应用程序显示通知时,请使用通知消息。 Use data messages when you want to process the messages on your client app. 如果要在客户端应用上处理消息,请使用数据消息。

FCM can send a notification message including an optional data payload. FCM可以发送包括可选数据有效载荷的通知消息。 In such cases, FCM handles displaying the notification payload, and the client app handles the data payload. 在这种情况下,FCM处理显示通知有效负载,客户端应用程序处理数据有效负载。

And the point is in your case: 重点在于你的情况:

Notification messages are delivered to the notification tray when the app is in the background. 当应用程序在后台时,通知消息将传递到通知托盘。 For apps in the foreground, messages are handled by a callback function. 对于前台的应用程序,消息由回调函数处理。

https://firebase.google.com/docs/cloud-messaging/concept-options https://firebase.google.com/docs/cloud-messaging/concept-options

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当App位于前台时,FCM推送通知将被替换 - FCM Push Notifications are replacing when App is in Foreground FCM:我已使用FCM接收有关我的应用程序的通知。 当我的应用程序处于前台时,它将所有通知分组,但不将其组合到后台时 - FCM: I have used FCM to receive notification for my app. When my app is in foreground it groups all notifications but not when app is in background 当应用程序处于前台状态时无法从 FCM 接收通知 - Cant receive Notifications from FCM while the app is in Foreground state 当应用程序处于前台或后台时如何使用 FCM 处理通知 - How to handle notifications with FCM when app is in either foreground or background 当应用不在前台时接收服务中的 FCM 数据消息 - Receive FCM data messages in service when the app is not in Foreground 应用程序被杀死时是否可以接收 FCM 推送通知? - Is it possible to receive FCM push notifications when app is killed? 当应用程序在前台时未收到FCM推送通知,但在应用程序在后台时收到 - FCM Push notification didn't receive when app is in foreground but receive when app in background 工作灯:应用程序在前台时无法接收推送通知 - Worklight: App is not able to receive the push notification when in foreground 应用被杀死时,应用未收到FCM通知 - app not receiving FCM notifications when the app is killed 当应用程序处于活动状态时通过 FCM 发送通知 - Sending notifications via FCM when app is active
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM