Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/218.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/firebase/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 应用程序位于前台时无法接收FCM通知_Android_Firebase_Android Fragments_Firebase Cloud Messaging_Android Notifications - Fatal编程技术网

Android 应用程序位于前台时无法接收FCM通知

Android 应用程序位于前台时无法接收FCM通知,android,firebase,android-fragments,firebase-cloud-messaging,android-notifications,Android,Firebase,Android Fragments,Firebase Cloud Messaging,Android Notifications,我正在处理FCM通知。当应用程序位于后台时,我的通知工作正常,但当应用程序位于前台时,我无法接收通知。我几乎什么都试过了,但没有成功。当应用程序位于前台时,不会收到通知 Manifest.xml: <service android:name=".Notification.MyFirebaseMessagingService" android:exported="false"> <intent-filter>

我正在处理FCM通知。当应用程序位于后台时,我的通知工作正常,但当应用程序位于前台时,我无法接收通知。我几乎什么都试过了,但没有成功。当应用程序位于前台时,不会收到通知

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>

FCM支持两种类型的消息:

  • 通知消息
  • 数据电文
如果服务器发送通知消息,则会在系统托盘上自动显示通知

当您希望FCM处理显示文件时,请使用通知消息 代表您的客户端应用程序发出通知。使用数据消息时 要处理客户端应用程序上的消息

FCM可以发送包含可选数据的通知消息 有效载荷。在这种情况下,FCM将处理通知的显示 负载,客户端应用程序处理数据负载

关键在于你的情况:

当 应用程序在后台。对于前台的应用程序,消息是 由回调函数处理


这里唯一重要的是您随通知发送的有效负载。请分享it@TimCastelijns不明白你在说什么我觉得很愚蠢,在我从AndroidManifest文件中的服务声明中删除exported=“false”后工作。大家好!
           @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());
    }
}