Android - 通知putExtra问题

时间:2016-03-11 21:50:36

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

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

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

2 个答案:

答案 0 :(得分: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());

答案 1 :(得分:0)

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

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

将始终使用Intent EventActivity首次使用启动的<{1}}。

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