首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >FCM推送通知Android在后台接收2份通知

FCM推送通知Android在后台接收2份通知
EN

Stack Overflow用户
提问于 2018-01-04 06:41:07
回答 5查看 2.7K关注 0票数 1

我在使用时遇到了问题,因为我已经重写了handleIntent()方法,以便在应用程序处于前台时接收通知。我还使用了onMessageReceived()方法。

但是当应用程序处于后台时,我将收到两个通知,其中一个只打开应用程序并运行MainActivity,而另一个则以我想要的方式打开应用程序。

FYI:我在前台时收到的通知正是我希望它打开的方式。

以下是我编写的代码:

代码语言:javascript
复制
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());
    }
}

}

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

App Gradle:

代码语言:javascript
复制
 compile 'com.google.firebase:firebase-core:11.4.2' //Firebase
compile 'com.google.firebase:firebase-messaging:11.4.2' //Firebase Cloud Messaging

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

EN

Stack Overflow用户

发布于 2018-08-31 03:44:40

对于仍有此问题的人:

这里有一个防止这种行为的黑客。我已经查看过所有这些信息,似乎只有很少的信息,但是如果您将实际消息保存在共享首选项中,然后在onRecieve中对照该值进行检查,则可以很容易地防止这种情况发生。唯一的缺点是您的用户不能以通知的形式连续两次发送完全相同的消息(但无论如何都会很烦人)。示例:

代码语言:javascript
复制
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
SharedPreferences Settings = getDefaultSharedPreferences(context);        
SharedPreferences.Editor editor = Settings.edit();

message = remoteMessage.getNotification().getBody();
from = remoteMessage.getFrom(); //this comes through as the topic, oddly...

 if(from.equals("/topics/"+userID+deviceID+"all")) {

    if(!message.equals(Settings.getString("messageall",null))) {//this filters any dupe messages

    utils.postNotification(title, message, context, extra, "messages");//create notification
    editor.putString("messageall", message);//always update to the last message
    editor.commit();
   }
 }
}
票数 0
EN
查看全部 5 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48089965

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档