【问题标题】:Multiple notification from GCM not directing to correct activity来自 FCM 的多个通知未重定向到正确的活动
【发布时间】:2014-05-28 07:42:39
【问题描述】:

我正在使用 GCM 进行推送通知。我的限制是,从 GCM 服务器收到的包中,我必须将用户重定向到我的应用程序中的特定位置。当我只有一个通知时,每件事都运行良好。如果通知托盘中有两个通知,则用户将被重定向到基于最新捆绑包的活动。所有其他捆绑包都将被忽略。我关注了许多 SO 帖子,例如 this 和 this,但它并没有解决我的问题。以下是我的代码的摘录:

private void sendNotification(String msg, String type, String id) {
    mNotificationManager = (NotificationManager) this
            .getSystemService(Context.NOTIFICATION_SERVICE);
    Intent intent = new Intent(this, MainActivity.class);
    intent.putExtra("notification_type", type);
    intent.putExtra("notification_id", id);
    intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
    PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
            intent, 0);//I have tried all flags of pending intent like PendingIntent.FLAG_UPDATE_CURRENT and other 3
    // flag_update_current
    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
            this).setSmallIcon(R.drawable.ic_launcher)
            .setContentTitle("RWD Rugby").setAutoCancel(true)
            .setStyle(new NotificationCompat.BigTextStyle().bigText(msg))
            .setContentText(msg);
    Random rnd = new Random();
    NOTIFICATION_ID = rnd.nextInt(90000);
    Log.d("notification number", NOTIFICATION_ID + "");
    mBuilder.setContentIntent(contentIntent);
    mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
}

在接收端我使用:

Intent intent = getIntent();//this gives me extras from the intent that started the activity not pending intent
String fromNotification = intent.getStringExtra("notification_type");

我可能不得不对可能得到的结果做出妥协。对我来说,最好的情况是根据我在创建意图时设置的属性将用户重定向到正确的页面。非常感谢任何帮助,如果我不够具体,请询问。

【问题讨论】:

  • @girish nair 你改了什么??
  • 点击“Edited May 28 at 11:55”然后选择第三个选项,并排比较。
  • @downvoter 关心评论??

标签: android android-intent push-notification google-cloud-messaging android-pendingintent


【解决方案1】:

你可以试试下面的代码.. (尝试使用 FLAG_UPDATE_CURRENT)

private void sendNotification(String msg) {
        mNotificationManager = (NotificationManager)
                this.getSystemService(Context.NOTIFICATION_SERVICE);

        Intent myintent = new Intent(this, ReceiveActivity.class);
        myintent.putExtra("message", msg);
        PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
                myintent, PendingIntent.FLAG_UPDATE_CURRENT);

        NotificationCompat.Builder mBuilder =
                new NotificationCompat.Builder(this)
       // .setSmallIcon(R.drawable.ic_stat_gcm)
        .setContentTitle("GCM Notification")
        .setStyle(new NotificationCompat.BigTextStyle()
        .bigText(msg))
        .setContentText(msg);

        mBuilder.setContentIntent(contentIntent);
        mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());


    }

【讨论】:

  • 已经尝试过,以及所有 5 种待处理的意图。没有一个对我有用。 FLAG_UPDATE_CURRENT 会将我拥有的所有通知更新为最新通知,因此如果通知旨在将用户带到消息片段,它可以通过任何方式将其带到另一个片段,感谢您的尝试
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-03-17
  • 1970-01-01
  • 2018-04-30
  • 2020-08-27
  • 2021-07-21
  • 2022-11-18
  • 2022-01-20
相关资源
最近更新 更多