android - FCM 推送通知 Android 在后台接收 2 个通知

标签 android firebase push-notification firebase-cloud-messaging

我在使用 FCM 推送通知消息服务时遇到问题,因为我已经覆盖了 handleIntent() 方法以在应用程序位于前台时接收通知。我也在使用 onMessageReceived() 方法。

但是当应用程序在后台时,我会收到 2 个通知,其中一个只打开应用程序并运行 MainActivity,而另一个按我想要的方式打开应用程序到。

仅供引用:当我在前台时收到的通知正是我希望它打开的方式。

这是我在下面写的代码:

public class MyFirebaseMessagingService extends FirebaseMessagingService {

private final String NOTIFICATION_TYPE = "type";
private final String NOTIFICATION_ID = "id";
private final String NOTIFICATION_TYPE_PRODUCT_DETAIL = "productdetail";

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    super.onMessageReceived(remoteMessage);

    String title = remoteMessage.getNotification().getTitle();R
    String body = remoteMessage.getNotification().getBody();

    String token = remoteMessage.getFrom();
    Log.d("FireBase TAG: ", token);

    // Check if message contains a data payload.
    if (remoteMessage.getData().size() > 0) {
        Log.d("FireBaseMessageService","FireBase Data payload : " + remoteMessage.getData());
    }

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



}


@Override
public void handleIntent(Intent intent) {
    super.handleIntent(intent);

    String type = intent.getExtras().getString(NOTIFICATION_TYPE, "");

    int id = 0;
    try {
        id = Integer.valueOf(intent.getExtras().getString(NOTIFICATION_ID, ""));
    } catch (NumberFormatException e) {
        e.printStackTrace();
    }
    //Intents
    Intent mainIntent = MainActivity.newIntent(this);
    Intent editProfileIntent = EditProfileActivity.newIntent(this);
    Intent settingsIntent = SettingsActivity.newIntent(this);
    Intent productIntent = ProductActivity.newNotificationIntent(this, id, false, true);


    if (UserManager.getSingleton().isUserLoggedIn(getApplicationContext())) {

        PendingIntent pendingIntent;

        if (type.equalsIgnoreCase(NOTIFICATION_TYPE_PRODUCT_DETAIL)) {
            TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
            stackBuilder.addParentStack(MainActivity.class);
            stackBuilder.addNextIntent(mainIntent);
            stackBuilder.addNextIntent(productIntent);

            editProfileIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            pendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_ONE_SHOT);
        }
        else {
            pendingIntent = PendingIntent.getActivity(this, 0, productIntent,
                    PendingIntent.FLAG_ONE_SHOT);
        }

        NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
                .setSmallIcon(R.mipmap.ic_launcher)
                .setContentTitle(intent.getExtras().getString("gcm.notification.title"))
                .setContentText(intent.getExtras().getString("gcm.notification.body"))
                .setDefaults(NotificationCompat.DEFAULT_VIBRATE)
                .setPriority(NotificationCompat.PRIORITY_MAX)
                .setContentIntent(pendingIntent);

        NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        manager.notify(0, builder.build());
    }
}

我已经从 onMessageReceived() 方法中删除了 NotificationCompat.Builder。 但是我仍然在后台收到两条通知。

应用 Gradle :

 compile 'com.google.firebase:firebase-core:11.4.2' //Firebase
compile 'com.google.firebase:firebase-messaging:11.4.2' //Firebase Cloud Messaging

我试过在线搜索解决方案,但不幸的是没有指向 Android 的解决方案。

最佳答案

您正在将通知内容处理到 handleIntent(Intent intent) 中。您可能应该删除 super.handleIntent(intent); 以防止 Firebase 系统在应用程序处于后台时处理通知。

解决方案:删除super.handleIntent(intent);

关于android - FCM 推送通知 Android 在后台接收 2 个通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48089965/

相关文章:

ios - 推送通知在 Live iOS 应用程序上停止

android - 如何在 Android 上禁用 OneSignal?

android - 如何将 Actionbarsherlock 添加到 android studio 项目

java - MainThread 异常上的网络(一个应用程序正在运行,另一个应用程序抛出异常)

java - Android中的传感器精确服务

android - 桌面文件到安卓手机的自定义通知

android - Firebase Crashlytics 无法在 Firebase 控制台中报告崩溃

ios - 点击通知后如何显示某个 View Controller 并发送数据?

ios - 在 UITableView 数组中保存 Firebase 结构 [Swift 4]

c# - 即将到来的 GCM 响应是 : Error=NotRegistered