我的应用程序中有一个通知,代码如下:
//Notification Start
notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
int icon = R.drawable.n1;
CharSequence tickerText = "Call Blocker";
long when = System.currentTimeMillis(); //now
Notification notification = new Notification(icon, tickerText, when);
Intent notificationIntent = new Intent(context, Main.class);
PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
Context context = getApplicationContext();
CharSequence title = "Call Blocker";
text = "Calls will be blocked while driving";
notification.setLatestEventInfo(context, title, text, contentIntent);
notification.flags |= Notification.FLAG_ONGOING_EVENT;
notification.flags |= Notification.FLAG_SHOW_LIGHTS;
notificationManager.notify(1, notification);
}
我的通知触发得很好,但我的问题是,当我点击通知中心的通知时,它不会启动我的应用程序。
基本上,点击我的通知后什么也不会发生!单击通知后,我应该做些什么才能开始我的主要活动。谢谢。
发布于 2012-12-05 13:19:35
请参见下面的代码。我正在使用它,它正在打开我的HomeActivity。
NotificationManager notificationManager = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(icon, message, when);
Intent notificationIntent = new Intent(context, HomeActivity.class);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
| Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent intent = PendingIntent.getActivity(context, 0,
notificationIntent, 0);
notification.setLatestEventInfo(context, title, message, intent);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notificationManager.notify(0, notification);
发布于 2015-08-04 17:30:04
下面是使用最新版本的NotificationCompact.Builder类构建通知的示例。
private void startNotification() {
Log.i("NextActivity", "startNotification");
// Sets an ID for the notification
int mNotificationId = 001;
// Build Notification , setOngoing keeps the notification always in status bar
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ldb)
.setContentTitle("Stop LDB")
.setContentText("Click to stop LDB")
.setOngoing(true);
// Create pending intent, mention the Activity which needs to be
//triggered when user clicks on notification(StopScript.class in this case)
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
new Intent(this, StopScript.class), PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.setContentIntent(contentIntent);
// Gets an instance of the NotificationManager service
NotificationManager mNotificationManager =
(NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
// Builds the notification and issues it.
mNotificationManager.notify(mNotificationId, mBuilder.build());
}
发布于 2012-12-05 13:15:12
看起来你漏掉了这部分
notification.contentIntent = pendingIntent;
试着添加这个,它应该可以工作。
https://stackoverflow.com/questions/13716723
复制相似问题