等待通知不起作用

下面是我的代码块,当通知被点击时应该打开NotificationActivity 。 但它不工作。

 private void setNotification(String notificationMessage) { Uri alarmSound = getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); mNotificationManager = getApplication().getSystemService(Context.NOTIFICATION_SERVICE); Intent notificationIntent = new Intent(getApplicationContext(), NotificationActivity2.class); PendingIntent contentIntent = PendingIntent.getActivity(this, 0,notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT); NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(getApplicationContext()) .setSmallIcon(R.drawable.logo) .setContentTitle("My Notification") .setStyle(new NotificationCompat.BigTextStyle() .bigText(notificationMessage)) .setContentText(notificationMessage).setAutoCancel(true); mBuilder.setSound(alarmSound); mBuilder.setContentIntent(contentIntent); mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build()); } 

Solutions Collecting From Web of "等待通知不起作用"

尝试这个:

 private void setNotification(String notificationMessage) { //**add this line** int requestID = (int) System.currentTimeMillis(); Uri alarmSound = getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); mNotificationManager = getApplication().getSystemService(Context.NOTIFICATION_SERVICE); Intent notificationIntent = new Intent(getApplicationContext(), NotificationActivity2.class); //**add this line** notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP); //**edit this line to put requestID as requestCode** PendingIntent contentIntent = PendingIntent.getActivity(this, requestID,notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT); NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(getApplicationContext()) .setSmallIcon(R.drawable.logo) .setContentTitle("My Notification") .setStyle(new NotificationCompat.BigTextStyle() .bigText(notificationMessage)) .setContentText(notificationMessage).setAutoCancel(true); mBuilder.setSound(alarmSound); mBuilder.setContentIntent(contentIntent); mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build()); } 

您可能会使用等于0的通知ID。通知ID为0的已知问题。如果您将使用任何其他ID,它应该工作。

我添加了任务生成器和下面的块代码为我工作

 Intent intent = new Intent(context, HomeActivity.class); TaskStackBuilder stackBuilder = TaskStackBuilder.create(context); stackBuilder.addParentStack(SplashActivity.class); stackBuilder.addNextIntent(intent); PendingIntent pendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);