点击通知没有启动预期的活动?

我在我的应用程序中使用GCM,并且在收到GCM消息时也使用NotificationManager来创build通知。现在一切正常,GCM消息在通知区域正确显示,但是当我点击通知时,它应该开始一个活动我的应用程序将显示没有发生的消息细节。 每次我点击通知它不会启动任何活动,它保持原样。创build通知的代码是:

private void sendNotification(String msg) { SharedPreferences prefs = getSharedPreferences( DataAccessServer.PREFS_NAME, MODE_PRIVATE); mNotificationManager = (NotificationManager) this .getSystemService(Context.NOTIFICATION_SERVICE); Intent intent = new Intent(this, WarningDetails.class); Bundle bundle = new Bundle(); bundle.putString("warning", msg); bundle.putInt("warningId", NOTIFICATION_ID); intent.putExtras(bundle); // The stack builder object will contain an artificial back stack for // the // started Activity. // This ensures that navigating backward from the Activity leads out of // your application to the Home screen. TaskStackBuilder stackBuilder = TaskStackBuilder.create(this); // Adds the back stack for the Intent (but not the Intent itself) stackBuilder.addParentStack(WarningDetails.class); // Adds the Intent that starts the Activity to the top of the stack stackBuilder.addNextIntent(intent); PendingIntent contentIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT); NotificationCompat.Builder mBuilder = new NotificationCompat.Builder( this).setSmallIcon(R.drawable.weather_alert_notification) .setContentTitle("Weather Notification") .setStyle(new NotificationCompat.BigTextStyle().bigText(msg)) .setContentText(msg); String selectedSound = prefs.getString("selectedSound", ""); if (!selectedSound.equals("")) { Uri alarmSound = Uri.parse(selectedSound); mBuilder.setSound(alarmSound); } else { Uri alarmSound = RingtoneManager .getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); mBuilder.setSound(alarmSound); } if (prefs.getBoolean("isVibrateOn", false)) { long[] pattern = { 500, 500, 500, 500, 500, 500, 500, 500, 500 }; mBuilder.setVibrate(pattern); } mBuilder.setContentIntent(contentIntent); mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build()); } 

我更新了自己的代码,以便Preserving Navigation when Starting an Activity支持Preserving Navigation when Starting an Activity就像使用Android开发人员网站的Gmail应用程序中发生的那样,从此它停止工作。有人请指导我在此代码中丢失或做错的地方。

Solutions Collecting From Web of "点击通知没有启动预期的活动?"

我的问题得到解决我只需要添加PendingIntent.FLAG_ONE_SHOT标志,所以我replace:

 PendingIntent contentIntent = stackBuilder .getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT); 

 PendingIntent contentIntent = stackBuilder .getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_ONE_SHOT); 

我遇到了同样的问题,并通过将android:exported =“true”添加到AndroidManifest.xml中的活动声明中解决了这个问题。

在这里,你只是将你的意图传递给pendingintent:见下文

 Intent notificationIntent = new Intent(context, Login.class); PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0); and set this contentintent into your Notification: Notification noti = new NotificationCompat.Builder(context) .setSmallIcon(icon_small) .setTicker(message) .setLargeIcon(largeIcon) .setWhen(System.currentTimeMillis()) .setContentTitle(title) .setContentText(message) .setContentIntent(**contentIntent**) .setAutoCancel(true).build(); 

这可能会帮助你。

如果您使用操作string启动预期的活动不要忘记添加

 <intent-filter> <action android:name="YOUR ACTION STRING"/> <category android:name="android.intent.category.DEFAULT" /> </intent-filter> 

<activity></activity>标签内

您要启动的活动必须在您的清单中指定为LAUNCHER活动 – 否则将不会通过Pending Intent启动。 在AndroidManifext.xml中添加以下内容

 <activity ... android:exported="true"> <intent-filter> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> 

否则,您将需要使用已被指定为启动程序的活动(例如您的主要活动)

在generateNotification()方法上做这样的事情..

用Splash.Java类replace你的活动。

 /** * Issues a notification to inform the user that server has sent a message. */ @SuppressWarnings("deprecation") private static void generateNotification(Context context, String message) { int icon = R.drawable.ic_launcher; long when = System.currentTimeMillis(); //message = "vivek"; // Log.d("anjan", message.split("~")[0]); //Toast.makeText(context, message, Toast.LENGTH_LONG).show(); NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); Notification notification = new Notification(icon, message, when); String title = context.getString(R.string.app_name); Log.d("anjan1", title); String text_message = context.getString(R.string.title_activity_main); Log.d("anjan1", text_message); Intent notificationIntent = new Intent(context, Splash.class); // set intent so it does not start a new activity 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; // Play default notification sound notification.defaults |= Notification.DEFAULT_SOUND; // Vibrate if vibrate is enabled notification.defaults |= Notification.DEFAULT_VIBRATE; notificationManager.notify(0, notification); } 

试试这个而不是最后一行:

mNotificationManager.notify(0,mBuilder.getNotification());