【问题标题】:Android - notification putExtra issueAndroid - 通知 putExtra 问题
【发布时间】:2016-03-11 21:50:35
【问题描述】:

我已经搜索了大约一个小时来找到一些解决方案,当用户点击通知框时如何向活动发送额外内容,但我发现的所有内容都对我不起作用。

我需要将事件 ID 传递给显示有关此事件的用户信息的活动。

我使用了 setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP)、setAction("Action")、PendingIntent.FLAG_UPDATE_CURRENT 以及所有这些解决方案的组合,但都没有奏效。

这是我的代码:

Notification.Builder notiBuilder = new Notification.Builder(context);
                notiBuilder.setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.drawable.logo));
                notiBuilder.setSmallIcon(R.drawable.logo);
                notiBuilder.setContentTitle(context.getString(R.string.eventNitificationTitle));
                notiBuilder.setContentText(obj.getString("nazwa") + " " + obj.getString("data"));

                Intent eventIntenet = new Intent(context, EventActivity.class);
                eventIntenet.setAction("Action_"+id);
                eventIntenet.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);

                eventIntenet.putExtra("eventID", id);

                PendingIntent pIntent = PendingIntent.getActivity(context, 0, eventIntenet, PendingIntent.FLAG_UPDATE_CURRENT);

                eventIntenet = null;

                notiBuilder.setContentIntent(pIntent);

                pIntent = null;

                NotificationManager notiManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
                    notiManager.notify(Integer.parseInt(id), notiBuilder.build());
                } else {
                    notiManager.notify(Integer.parseInt(id), notiBuilder.getNotification());
                }

以及获得我的 Int 的方法:

this.eventID = getIntent().getIntExtra("eventID", -1);

每次点击通知时,我都会转到活动但 getExtra 返回 -1。

【问题讨论】:

  • 尝试 getIntent().getExtra().getInt("eventID") 并删除那些你设置为 null 的 Intent 至少在你完成通知之前不要设置它。
  • 感谢您的回复。我已经做到了,但仍然没有工作。我还有一个问题 - 当我不再需要它时将变量的值设置为 null 是个好主意吗?

标签: android android-intent notifications android-pendingintent extra


【解决方案1】:

您可以尝试以下方法:

                NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(ChatService.this).setSmallIcon(R.drawable.app_icon)
                        .setContentTitle(senderName)
                        .setContentText("Notification Text Here");
                mBuilder.setAutoCancel(true);

                Intent resultIntent = new Intent(MyService.this, TargetActivity.class);
                resultIntent.putExtra("eventID", id);

                TaskStackBuilder stackBuilder = TaskStackBuilder.create(MyService.this);
                stackBuilder.addParentStack(TargetActivity.class);
                stackBuilder.addNextIntent(resultIntent);

                PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);

                mBuilder.setContentIntent(resultPendingIntent);

                NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
                mNotificationManager.notify(id, mBuilder.build());

【讨论】:

    【解决方案2】:

    如果您的应用已经在运行并且您的 EventActivity 已经在堆栈顶部,则此代码:

    this.eventID = getIntent().getIntExtra("eventID", -1);
    

    将始终使用EventActivity 第一次启动时使用的Intent

    您应该覆盖onNewIntent() 并从Intent 中获取作为参数传递给该方法的“额外”。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-01-12
      • 1970-01-01
      • 1970-01-01
      • 2016-10-03
      • 2020-06-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多