我已经在发出通知的位置设置了我的应用程序,当它被点击时,我想在程序中知道这一点。每次单击它时,主要活动都会打开,但我时不时会遇到一个错误,即单击通知时不会调用 onNewIntent(Intent intent)。这是我的 onNewIntent:
@Override
public void onNewIntent(Intent intent) //when notification is clicked
{
Log.i("mydebug","Notification clicked.");
}
这里是创建通知的地方:
Intent notificationIntent = new Intent(context, MainActivity.class);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
builder.setWhen(System.currentTimeMillis())
.setContentTitle(schedname)
.setContentText("Click to deactivate this silence schedule.")
.setContentIntent(pendingIntent)
.setSmallIcon(R.drawable.ic_launcher);
//notification.flags |= Notification.FLAG_NO_CLEAR; //Notification cannot be clearned by user
NotificationManager mNotificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
// Uses the default lighting scheme
builder.build().defaults |= Notification.DEFAULT_LIGHTS;
builder.setAutoCancel(true);
builder.setOngoing(true);
builder.setContentIntent(pendingIntent);
// Will show lights and make the notification disappear when the presses it
builder.build().flags |= Notification.FLAG_AUTO_CANCEL | Notification.FLAG_SHOW_LIGHTS;
mNotificationManager.notify(1, builder.build());