1

我创建了一个通知。当用户在 android 操作系统中单击通知时,它会成功启动所需的活动。它在我的 Activity_Send.class 上调用 OnCreate。我的问题是,我可以访问有关该通知的任何信息吗?通知的标题,或 ID,甚至是否可以传递我在创建通知时可以设置的参数,当它调用我的活动时我可以检索这些参数?

下面的代码是我用来创建我的通知的代码......修订后的代码

private static void notice(String msgfrom, String msg) {
Intent intent = null;
    String title;

TaskStackBuilder stackBuilder = TaskStackBuilder.create(appctx);

title = "New message from " + msgfrom;
SendUser = msgfrom;     // Who we sending message to?
intent = new Intent( appctx, Activity_Send.class);  
intent.putExtra("from", msgfrom);
intent.putExtra("id",Integer.toString(NoticeCount));
if (whoscreen != null) { stackBuilder.addNextIntent(whoscreen); }
stackBuilder.addNextIntent(intent);

//PendingIntent pintent = PendingIntent.getActivity(appctx,NoticeCount,intent, 0);
PendingIntent pintent = stackBuilder.getPendingIntent(NoticeCount, PendingIntent.FLAG_UPDATE_CURRENT);

NotificationCompat.Builder nb =   new NotificationCompat.Builder(appctx);
nb.setSmallIcon(R.drawable.pp);
    nb.setContentTitle(title);
nb.setContentText(modmsg);
nb.setAutoCancel(true);
nb.setContentIntent(pintent);


    Notification notification = nb.build();
    NotificationManager NM = (NotificationManager) appctx.getSystemService(NOTIFICATION_SERVICE);
    NM.notify(NoticeCount, notification);   
    NoticeCount = NoticeCount +1;
}
4

1 回答 1

0

你可以把任何你想要的东西放在Intent用来开始你的Activity_Send课程的东西中作为额外的东西。例如:

intent.putExtra("notificationTitle", title);
intent.putExtra("notificationId", NoticeCount);

您尚未发布用于创建的代码pintent,但它应该类似于:

pintent = PendingIntent.getActivity(this, 0, intent, 0);

然后PendingIntentNotificationusing上设置setContentIntent()

你可以像这样得到额外的东西onCreate()Activity_Send

String notificationTitle = getIntent().getStringExtra("notificationTitle");
int notificationId = getIntent().getIntExtra("notificationId", -1);
于 2013-05-08T22:49:32.877 回答