1

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

我需要将事件 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。

4

2 回答 2

0

您可以尝试以下方法:

                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());
于 2016-03-11T22:22:18.207 回答
0

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

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

将始终使用第一次开始的Intent那个EventActivity

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

于 2016-12-30T16:35:25.413 回答