1

我與GCM的工作,這是我的onMessage功能:活動沒有得到正確的捆綁

@Override 
protected void onMessage(Context context, Intent intent) { 
    if (intent != null && intent.getExtras() != null) { 
     try { 
      String message = intent.getExtras().getString("message"); 
      String userId = intent.getExtras().getString("user_id"); 
      Log.i("","postda user id is:" + userId); 
      Log.i("","will enter here FRIENDS: "); 
      generateNotification(context, message, userId); 
     } catch (Exception e) { 
      Utils.appendLog("onMessage errror : " + e.getMessage()); 
      Log.i(TAG, e.getMessage(), e); 
     } 
    } 
} 

這是我CreateNotification功能:

private static void generateNotification(Context context, String message, String userID) { 
    Log.i("", "postda user message " + message + ".... userid: " + userID); 
    String title = context.getString(R.string.passenger_name); 
    Intent notificationIntent = new Intent(context, PSProfileActivity.class); 
    notificationIntent.putExtra("id", userID); 
    Log.i("", "postda ---------- userid: "+ userID); 
    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP| Intent.FLAG_ACTIVITY_NEW_TASK); 
    PendingIntent intent = PendingIntent.getActivity(context, 0, notificationIntent, 0); 
    Log.i("", "postda message: " + message + "....userID " + userID); 

    PSLocationCenter.getInstance().pref.setDataChanged(context, true); 
    PSLocationCenter.getInstance().pref.setDataChangedProfile(context, true); 

    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context); 
    mBuilder.setContentTitle(title).setContentText(message).setSmallIcon(R.drawable.notification_icon); 
    mBuilder.setContentIntent(intent); 
    Notification notification = mBuilder.build(); 
    notification.flags |= Notification.FLAG_AUTO_CANCEL; 

    NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); 
    notificationManager.notify(1, notification); 

    playTone(context); 
} 

這是PSProfileActivity的onCreate功能:

@Override 
protected void onCreate(Bundle savedInstanceState){ 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_profile); 
    ButterKnife.inject(this); 

    mHeader = new ProfileHeader(this, backFromHeader); 
    mData = new ProfileData(this); 
    mButtons = new ProfileViewPagerButtons(PSProfileActivity.this, firstPage); 

    Bundle bundle = getIntent().getExtras(); 
    if(bundle != null) id = bundle.getString("id"); 
    Log.i("", "postda in profile id is:" + id); 
    if(!id.contentEquals(String.valueOf(PSLocationCenter.getInstance().pref.getUserId(PSProfileActivity.this)))){ 
     findViewById(R.id.action_settings).setVisibility(View.INVISIBLE); 
    } 
    Log.i("", "postda in profile id is 2:" + id); 
} 

這是我logcat的響應:

04-17 15:33:53.650 9699-11695/nl.hgrams.passenger I/﹕ postda user id is:23 
04-17 15:33:53.651 9699-11695/nl.hgrams.passenger I/﹕ postda user message alin reddd accepted your friend request..... userid: 23 
04-17 15:33:53.651 9699-11695/nl.hgrams.passenger I/﹕ postda ---------- userid: 23 
04-17 15:33:53.652 9699-11695/nl.hgrams.passenger I/﹕ postda message: alin reddd accepted your friend request.....userID 23 
04-17 15:33:58.812 9699-9699/nl.hgrams.passenger I/﹕ postda in profile id is:28 
04-17 15:33:58.814 9699-9699/nl.hgrams.passenger I/﹕ postda in profile id is 2:28 

正如你所看到的,我通過包發送了一個ID,但是我在另一個頁面上得到的是一個完全不同的id(它實際上是本應該得到的最後一個ID) 。這真的很奇怪,有人知道爲什麼會發生這種情況嗎?

回答

2

使用獨特identitier在PendingIntent

​​
+0

試過這樣,但我仍然得到同樣的問題,即使我有一個uniqueID –

+0

你可以試試這個嗎? http://stackoverflow.com/a/29207788/1347366 –

+1

它沒有工作,謝謝 –

0

問題可能是您只使用額外功能來修改PendingIntent。嘗試將ID嵌入notificationIntent的數據字段中。

http://developer.android.com/reference/android/app/PendingIntent.html

一個常見的錯誤的人做是創建意圖,只有在他們的「額外」的內容有所不同倍數的PendingIntent對象,希望每次都得到一個不同的PendingIntent。這不會發生。用於匹配的Intent部分與Intent.filterEquals定義的部分相同。如果您使用兩個與Intent.filterEquals等效的Intent對象,那麼您將爲它們獲得相同的PendingIntent。

0

我認爲你必須使用這個標誌,FLAG_UPDATE_CURRENT。 PendingIntent.getActivity(context,0,intent,PendingIntent.FLAG_UPDATE_CURRENT);我們可以使用PendingIntent.getActivity(context,0,intent,PendingIntent.FLAG_UPDATE_CURRENT);我們可以使用PendingIntent.getActivity(context,0,intent,PendingIntent.FLAG_UPDATE_CURRENT);我們可以使用PendingIntent.getActivity(context,0,intent,PendingIntent.FLAG_UPDATE_CURRENT)

這將刷新舊意圖,並通過捆綁傳遞新的附加信息並獲取與ID匹配的正確數據。

1

您正在生成具有相同ID的intent。您需要傳遞唯一標識符作爲第二個參數,並且還需要添加標誌FLAG_UPDATE_CURRENT,以便附加信息得到更新,並且您始終可以獲得您傳遞的最新附加信息。

所以你要生成的意圖是這樣的:

PendingIntent intent = PendingIntent.getActivity(context, identifier , notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT); 

標識符可以是任何東西,唯一像自動遞增變量或當前時間戳