Android:创建系统通知。 - 您可以在通知CLICK上检索参数吗?

时间:2013-05-08 22:21:42

标签: android android-notifications

我已经创建了一个通知。当用户在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;
}

1 个答案:

答案 0 :(得分:0)

您可以将用于启动Intent课程的Activity_Send中的任何内容添加为附加内容。例如:

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

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

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

然后使用PendingIntentNotification上设置此setContentIntent()

onCreate()的{​​{1}}中,您可以像这样获得额外内容:

Activity_Send