我可以找到一种方法从我的通知中向我的活动发送参数.
我有一个创建通知的服务.当用户点击通知时,我想用一些特殊参数打开我的主要活动.例如项目ID,因此我的活动可以加载并呈现特殊项目详细信息视图.更具体地说,我正在下载一个文件,当下载文件时,我希望通知有一个意图,当点击它时,它会以特殊模式打开我的活动.我试图使用putExtra我的意图,但似乎无法提取它,所以我认为我做错了.
我的服务中创建通知的代码:
// construct the Notification object.
final Notification notif = new Notification(R.drawable.icon, tickerText, System.currentTimeMillis());
final RemoteViews contentView = new RemoteViews(context.getPackageName(), R.layout.custom_notification_layout);
contentView.setImageViewResource(R.id.image, R.drawable.icon);
contentView.setTextViewText(R.id.text, tickerText);
contentView.setProgressBar(R.id.progress,100,0, false);
notif.contentView = contentView;
Intent notificationIntent = new Intent(context, Main.class);
notificationIntent.putExtra("item_id", "1001"); // <-- HERE I PUT THE EXTRA VALUE
PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
notif.contentIntent = contentIntent;
nm.notify(id, notif);
Run Code Online (Sandbox Code Playgroud)
我的Activity中的代码尝试从通知中获取额外参数:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Bundle extras = getIntent().getExtras();
if(extras != null){
Log.i( "dd","Extra:" + …Run Code Online (Sandbox Code Playgroud) 我通过以下代码在BroadcastReceiver中创建通知:
String ns = Context.NOTIFICATION_SERVICE;
NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(ns);
int icon = R.drawable.ic_stat_notification;
CharSequence tickerText = "New Notification";
long when = System.currentTimeMillis();
Notification notification = new Notification(icon, tickerText, when);
notification.defaults |= Notification.DEFAULT_VIBRATE;
long[] vibrate = {0,100,200,200,200,200};
notification.vibrate = vibrate;
notification.flags |= Notification.FLAG_AUTO_CANCEL;
CharSequence contentTitle = "Title";
CharSequence contentText = "Text";
Intent notificationIntent = new Intent(context, NotificationActivity.class);
notificationIntent.putExtra(Global.INTENT_EXTRA_FOO_ID, foo_id);
PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
int mynotification_id = 1;
mNotificationManager.notify(mynotification_id, notification);
Run Code Online (Sandbox Code Playgroud)
当我点击通知时,它会打开NotificationActivity,在Activity中我可以从Intent-Bundle中检索foo_id(例如1)
但是,如果触发另一个通知并再次单击它,则活动仍会从Intent-Bundle接收"旧"值(1).我试图用clear()清除包,但收到的效果相同.我觉得我的代码错了......
android android-notifications android-notification-bar android-pendingintent
我在我的应用程序中发生了一个警报,它会启动一个通知,然后在按下时启动一个活动.问题是,当我创建多个警报时,从通知启动的活动会获得与第一个相同的额外内容.我认为这个问题要么是我在未决意图中的意图,要么是在未决意图本身.我想我可能需要在其中一个上放一面旗帜,但我不知道哪一个.
Intent showIntent =new Intent(context, notificationreceiver.class);
showIntent.putExtra("details", alarmname);
PendingIntent contentIntent = PendingIntent.getActivity(context, 0,
showIntent, 0);
notification.setLatestEventInfo(context, "The event is imminent",
alarmname, contentIntent);
Run Code Online (Sandbox Code Playgroud)
和通知的接收者
Bundle b = getIntent().getExtras();
String eventname = b.getString("details");
details.setText(eventname);
Run Code Online (Sandbox Code Playgroud)
每次下次通知发生时,"详细信息"额外都是相同的,而不是具有不同的值.直到我设置意图,我确信正确的值转到"详细信息",因此每次按任何通知时都会获得第一个意图的问题.如何才能启动正确的意图呢?希望我尽可能清楚,谢谢!
我正在从具有额外整数意图的服务创建通知.出于某种原因,这并不像看起来那么容易.这就是我正在做的事情:
Notification notification = new Notification(R.drawable.notification_icon, "Title", 0);
Intent relaunchIntent = new Intent(this, SomeClass.class);
relaunchIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
relaunchIntent.putExtra("load", getId());
notification.setLatestEventInfo(this, "Title", "Tap here to open", PendingIntent.getActivity(this, 0, relaunchIntent, 0);
notificationManager.notify(1234, notification);
Run Code Online (Sandbox Code Playgroud)
然后从SomeClass,它读起来像这样......
if(getIntent().getExtras() != null)
load(getIntent().getExtras().getInt("load", -1));
Run Code Online (Sandbox Code Playgroud)
奇怪的是,当我从getInt()方法中读取值时,我得到了不一致的结果.有时我会得到我在之前的通知中设置的值的值.例如,当我将额外设置为4时,我得到值为3.我知道这非常令人困惑和奇怪,这就是为什么我想知道是否有人经历过这样的事情以及你可能做了些什么来修复它.谢谢!
以下代码显示通知并发送int数据,但在其他活动中getExtras()返回null.为什么?
int notificationID = 1;
NotificationManager nm = (NotificationManager) getSystemService(getApplicationContext().NOTIFICATION_SERVICE);
Intent i = new Intent(getApplicationContext(), DownlodResult.class);
i.putExtra("notificationID", 1);
PendingIntent contentIntent = PendingIntent.getActivity(getApplicationContext(), 0, i, 0);
CharSequence tickerText = "There are updates !";
long when = System.currentTimeMillis();
int icon = R.drawable.ic_launcher;
Notification notification = new Notification(icon,tickerText,when);
CharSequence contentTitle = "There are updates";
CharSequence contentText = "Please click here to view it";
notification.setLatestEventInfo(getApplicationContext(), contentTitle, contentText, contentIntent);
notification.vibrate = new long[] { 100, 250, 100, 500}; // Needs vibrate …Run Code Online (Sandbox Code Playgroud)