Android 安卓-额外问题通知
我已经搜索了大约一个小时,想找到一些解决方案,当用户点击通知框时,如何向活动发送额外信息,但我发现的一切都不适合我 我需要将事件ID传递给活动,在活动中我显示有关此事件的用户信息 我使用过setFlags(Intent.FLAG\u ACTIVITY\u SINGLE\u TOP)、setAction(“Action”)、pendingent.FLAG\u UPDATE\u CURRENT以及所有这些解决方案的组合,但都不起作用 这是我的密码:Android 安卓-额外问题通知,android,android-intent,notifications,android-pendingintent,extra,Android,Android Intent,Notifications,Android Pendingintent,Extra,我已经搜索了大约一个小时,想找到一些解决方案,当用户点击通知框时,如何向活动发送额外信息,但我发现的一切都不适合我 我需要将事件ID传递给活动,在活动中我显示有关此事件的用户信息 我使用过setFlags(Intent.FLAG\u ACTIVITY\u SINGLE\u TOP)、setAction(“Action”)、pendingent.FLAG\u UPDATE\u CURRENT以及所有这些解决方案的组合,但都不起作用 这是我的密码: Notification.Builder noti
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);
每次单击通知时,我都会转到“活动”,但会获得额外的返回值-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());
您可以尝试以下方法:
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());
如果您的应用程序已在运行,并且您的
EventActivity
已位于堆栈顶部,则此代码:
this.eventID = getIntent().getIntExtra("eventID", -1);
将始终使用第一次启动事件活动的意图。
您应该重写onNewIntent()
,并从作为参数传递给该方法的Intent
中获取“额外”值。如果您的应用程序已在运行,并且您的EventActivity
已位于堆栈顶部,则此代码:
this.eventID = getIntent().getIntExtra("eventID", -1);
将始终使用第一次启动事件活动的意图。
您应该重写onNewIntent()
并从作为参数传递给该方法的Intent
中获取“extra”。尝试getIntent().getExtra().getInt(“eventID”)并移除您将null设置为Intents的内容,至少在完成通知之前不要设置它。感谢回复。我已经做过了,但仍然不起作用。我还有一个问题-当我不再需要变量时,将其值设置为null是个好主意?请尝试getIntent().getExtra().getInt(“eventID”)并删除您设置为null的内容,至少在完成通知之前不要设置它。感谢您的回复。我已经做过了,但仍然不起作用。我还有一个问题-当我不再需要变量时,将其值设置为null是个好主意?