2013-12-23 88 views
22

我在我的應用程序中使用GCM,並且在收到GCM消息時也使用NotificationManager創建通知。現在一切正常,GCM消息在通知區域正確顯示,但是當我點擊通知時,它應該啓動我的應用程序的活動將顯示未發生的消息細節。每一次我點擊通知它不會啓動任何活動,它仍然作爲創建通知is.My代碼:點擊通知沒有啓動預期的活動?

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就像它採用的是Android的Gmail應用程序發生開發人員網站,因爲它然後停止working.Some人請指導我什麼我失蹤或做錯在這個代碼。

回答

34

我的問題得到有效解決我只需要添加PendingIntent.FLAG_ONE_SHOT標誌一樣,所以我代替:

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

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

您在哪裏使用此代碼? – Saty

+0

@Saty我在sendNotification()方法中使用它發佈在我的問題 – user818455

+0

您好使用TaskStackBuilder需要我API 16作爲最低需要,我不想做...有沒有其他的選擇? – Saty

2

在這裏,您剛通過你的意圖進入的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(); 

這可能會幫助你。

+0

會在開始的時候這支持保留導航Gmail應用程序等活動? – user818455

-1

做一些這樣的事情上generateNotification()方法..

替換它Splash.Java類的活動。

/** 
    * 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);  

    } 
+0

您的代碼不支持開始活動時保留導航 – user818455

+0

您是否在generateNotification()方法中使用了我的代碼。如果是的話,你會得到什麼錯誤? – AndroidHacker

+0

問題是在啓動Activity後沒有保留BackStack – user818455

-1

嘗試而不是最後一行此:

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

+0

但NotificationCompat.Builder類型的方法getNotification()已棄用 – user818455

19

我遇到了同樣的問題,並通過添加解決它android:exported =「true」添加到AndroidManifest.xml中的活動聲明中。

+0

這也解決了我的問題。什麼也沒有發生。將該行添加到清單中可以工作:-)謝謝! – flipperweid

+0

你救了我的一天 – amit

+0

這也適用於我。非常感謝 ! :) – Yashasvi

0

如果您啓動使用操作字符串不要忘了計劃的活動增加

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

<activity></activity>標籤

0

要推出已被指定爲一個啓動活動的活動您清單 - 否則它不會通過Pending Intent啓動。以下內容添加到您的AndroidManifext.xml

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

否則,你將需要使用已指定爲LAUNCHER的活動(如您的主要活動)