通知投掷意图没有它的额外

时间:2011-11-30 13:53:09

标签: android notifications android-intent

我正在尝试从通知中启动一个意图,但由于某种原因,我不知道额外的内容并不总是与意图一起发送。 这是我创建意图的地方:

NotificationManager notificationManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(R.drawable.icon,
                    mContext.getString(R.string.text), 0);

Intent intents = new Intent(mContext, DataAppTabs.class);
intents.putExtra(DataAppTabs.REQUEST, DataAppTabs.REQUEST_DMTAB);
intents.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent launchIntent =    PendingIntent.getActivity(mContext.getApplicationContext(), DataAppTabs.REQUEST_DMTAB, intents, PendingIntent.FLAG_UPDATE_CURRENT);
notification.setLatestEventInfo(mContext, "Info", mContext.getString(R.string.enginedmnotification_rebootneeded), launchIntent);
notification.flags = Notification.FLAG_NO_CLEAR;
notification.flags |= Notification.FLAG_ONLY_ALERT_ONCE;
notification.defaults = Notification.DEFAULT_ALL;
notificationManager.notify(DataAppTabs.REQUEST_DMTAB, notification);

在DataAppTabs类中,我重写了onCreate()和onNewIntent()。当通知出现时,如果我点击它,它会打开正确的Activity(DataAppTabs,这是一个TabActivity),如果活动已经启动,它会触发onCreate或onNewIntent,但是并不总是设置额外的“REQUEST”(getExtras()有时候是空的。)

我在这里找到了同样的问题,答案告诉我必须使用“FLAG_ACTIVITY_SINGLE_TOP”标志,但它仍然不能一直工作,我不能每次都重现这个问题。

我做错了吗? 感谢

1 个答案:

答案 0 :(得分:-1)

    // Displaying Notification
              NotificationManager manager = (NotificationManager)this.getSystemService(Context.NOTIFICATION_SERVICE);
               Notification notification = new Notification(R.drawable.icon, "MyNotification", System.currentTimeMillis());
                PendingIntent contentIntent = PendingIntent.getActivity(getApplicationContext(), 0, new Intent(getApplicationContext(), Sample.class), 0);
                notification.setLatestEventInfo(getApplicationContext(), "Notification Title", "Notification Data", contentIntent);
                notification.flags = Notification.FLAG_INSISTENT;

                            int NOTIFICATION_ID=(int)System.currentTimeMillis();
                manager.notify(NOTIFICATION_ID, notification);

// Sample.class is the activity to which you want to go when you click on Notification