【问题标题】:Android Push Notification Not opening the Activity if n number of notifications in Notification BarAndroid推送通知如果通知栏中有n个通知,则不打开活动
【发布时间】:2015-11-13 06:16:45
【问题描述】:

我正在开发一个 android 应用程序,我也实现了 GCM 推送通知,并且每个通知 id 都不同,问题是当我有 n 个推送通知并且应用程序不在后台时,当我第一次单击通知时它将打开活动。然后我再次从后台清除应用程序实例并单击另一个推送通知它不会打开应用程序。

        Intent intent = new Intent(this, MainActivity.class);
    TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
    stackBuilder.addParentStack(MainActivity.class);
    stackBuilder.addNextIntent(intent);
    long[] vibrate = {0, 100, 200, 300};
    Uri notification = RingtoneManager
            .getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    PendingIntent contentIntent = stackBuilder.getPendingIntent(0,
            PendingIntent.FLAG_UPDATE_CURRENT
                    | PendingIntent.FLAG_ONE_SHOT);
    Bitmap largIcon = BitmapFactory.decodeResource(this.getResources(),
            R.mipmap.ic_launcher);
    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
            this).setSmallIcon(R.mipmap.small_icon_notification)
            .setContentTitle(getResources().getString(R.string.app_name))
            .setLargeIcon(largIcon)
            .setSound(notification)
            .setStyle(new NotificationCompat.BigTextStyle().bigText(msg))
            .setContentText(msg).      setSound(notification)
            .setStyle((new NotificationCompat.BigTextStyle()).bigText(msg)) .setAutoCancel(true);
    mBuilder.setVibrate(vibrate);
    mBuilder.setContentIntent(contentIntent);
    mNotificationManager.notify((int) System.currentTimeMillis(), mBuilder.build());

【问题讨论】:

  • 我认为您不应该将 System.currentTimeMillis() 转换为 int。请改用随机数生成器。
  • 如你所说,我更改了通知 ID。但结果是一样的
  • 你能用你的改变更新代码吗?

标签: android android-activity push-notification google-cloud-messaging


【解决方案1】:

在行中

PendingIntent contentIntent = stackBuilder.getPendingIntent(0,
        PendingIntent.FLAG_UPDATE_CURRENT
                | PendingIntent.FLAG_ONE_SHOT);

对于PendingIntent,您始终使用相同的请求代码。

PendingIntent 的所有实例具有相同的请求代码(第一个参数)和相同的意图(也许这并不重要,我还没有测试过)都是相同的未决意图,无论您在意图中添加了什么额外内容.使它们不同的唯一方法是使用不同的请求代码。

特别是如果你使用FLAG_ONE_SHOT"Flag indicating that this PendingIntent can be used only once"

【讨论】:

    【解决方案2】:

    我将PendingIntent.FLAG_ONE_SHOT 和待处理的意图请求代码删除为随机数。现在它工作正常。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多