-1

我正在使用不同的msg id在我的應用程序中顯示通知,當我點擊notificaion時,如何獲取每個通知的有效載荷或味精以向用戶顯示相關信息。如何獲取每個通知的有效載荷?

multiple notificaion http://trip38.com/images/noti.png

一個很好的參考或解決方法會更有幫助:)我寫

代碼是

NotificationManager notifier = (NotificationManager) GCMIntentService.this. 
      getSystemService(Context.NOTIFICATION_SERVICE); 
    Notification notification = new Notification(drawable, msg_alert, System.currentTimeMillis()); 

    //Setup the Intent to open this Activity when clicked 
    Intent toLaunch = new Intent(GCMIntentService.this, MainActivity.class); 
    toLaunch.putExtra("msg", msg); 

    PendingIntent contentIntent = PendingIntent.getActivity(GCMIntentService.this, 0, toLaunch, 0); 

    //Set the Notification Info 
    notification.tickerText = ticker_msg; 
    notification.setLatestEventInfo(GCMIntentService.this, noti_title, noti_msg, contentIntent); 

    //Setting Notification Flags 
    notification.flags |= Notification.FLAG_AUTO_CANCEL; 
    notification.defaults |= Notification.DEFAULT_SOUND; 
    notification.defaults |= Notification.DEFAULT_VIBRATE; 

    notifier.notify(msg_id, notification); 

編輯代碼

public class GCMIntentService extends GCMBaseIntentService { 

private static final String TAG = "GCMIntentService"; 
private SharedPreferences mPreferences; 

public GCMIntentService() { 
    super(AppConstants.SENDER_ID); 
} 


@Override 
protected void onRegistered(Context context, String registrationId) { 
    mPreferences = context.getApplicationContext().getSharedPreferences(AppConstants.PREFS_FILE_NAME, 0); 

    SharedPreferences.Editor mEditor = mPreferences.edit(); 
    mEditor.putString(AppConstants.REGISTRATION_KEY_STR, registrationId); 
    mEditor.apply(); 

    //saved reg id in server 
} 

@Override 
protected void onUnregistered(Context arg0, String arg1) { 
    Logger.d(TAG, "unregistered = " + arg1); 
} 

@Override 
protected void onMessage(Context arg0, Intent arg1) { 
    Logger.d(TAG, "new message received: " + arg1.getStringExtra("message")); 
    showNotification(arg1.getStringExtra("message")); 
} 

@Override 
protected void onError(Context arg0, String errorId) { 
    Logger.d(TAG, "Received error: " + errorId); 
} 

@Override 
protected boolean onRecoverableError(Context context, String errorId) { 
    return super.onRecoverableError(context, errorId); 
} 

private void showNotification(String msg) { 
    String msg_alert = getResources().getString(R.string.app_name); //alert notification title 
    String noti_title = ""; //noti_title from msg 
    String noti_msg = ""; //noti_msg from msg 
    String ticker_msg = ""; //ticker_msg from msg 
    String msg_icon = ""; //msg_icon from msg 
    int msg_id; //msg_id from msg 

    //Get the icon for the notification 
    int drawable = R.drawable.ic_launcher; 
    if (!msg_icon.equals("")) 
     drawable = getResources().getIdentifier(msg_icon, "drawable", getPackageName()); 
    if (drawable == 0) { 
     drawable = R.drawable.ic_launcher; 
    } 
    NotificationManager notifier = (NotificationManager) GCMIntentService.this. 
      getSystemService(Context.NOTIFICATION_SERVICE); 
    Notification notification = new Notification(drawable, msg_alert, System.currentTimeMillis()); 

    //Setup the Intent to open this Activity when clicked 
    Intent toLaunch = new Intent(GCMIntentService.this, MainActivity.class); 
    toLaunch.putExtra("msg_type", msg_type); 
    toLaunch.putExtra("trip_id", trip_id); 
    toLaunch.putExtra("booking_id", booking_id); 
    toLaunch.putExtra("noty_ticket_id", ticketId); 
    toLaunch.putExtra("msg", msg); 

    PendingIntent contentIntent = PendingIntent.getActivity(GCMIntentService.this, 0, toLaunch, 0); 

    //Set the Notification Info 
    notification.tickerText = ticker_msg; 
    notification.setLatestEventInfo(GCMIntentService.this, noti_title, noti_msg, contentIntent); 

    //Setting Notification Flags 
    notification.flags |= Notification.FLAG_AUTO_CANCEL; 
    notification.defaults |= Notification.DEFAULT_SOUND; 

    notifier.notify(msg_id, notification); 
} 

}

+0

你添加未決意圖您的通知建設者請把。?一些代碼 – Rohit5k2

+0

谷歌不建議顯示這樣的通知:http://developer.android.com/design/patterns/notifications.html – Groco

+0

@ Rohit5k2增加了代碼供參考 – Devishankar

回答

0

你的問題是在這裏:

PendingIntent contentIntent = PendingIntent.getActivity(GCMIntentService.this, 0, 
             toLaunch, 0); 

如果要創建幾個PendingIntent小號需要同時活動,你需要確保它們是「獨特的」。最簡單的方法是在撥打PendingIntent.getActivity()時使用不同的(唯一的)requestCode參數。就像這樣:

PendingIntent contentIntent = PendingIntent.getActivity(GCMIntentService.this, msg_id, 
             toLaunch, 0); 

(假設msg_id是每個消息獨特。如果不是的話,你可以使用一些其他的唯一編號

+0

哇,它完美的工作 – Devishankar

+0

如果應用程序已經打開時通知被點擊是否有任何方式來從待定意圖獲得有效載荷? – Devishankar

+1

是的。添加'Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP'到你放入'Notification'('toLaunch')的'Intent'。 'onNewIntent()'方法將在'MainActivity'中調用,這個'Intent'應該有你的有效載荷(extras)。 –

0

這是您將如何接收活動MainActivity中的數據的方式。你會得到從通知到意圖的有效載荷。

String msgFromNotification = getIntent().getStringExtra("msg") 

編輯

可能是msg值是靜態的。看你的代碼。如果您希望收到的通知顯示的消息

變化

toLaunch.putExtra("msg", msg); 

toLaunch.putExtra("msg", noti_msg); 
+0

完全相同,但在'msgFromNotification'我只是得到Title6的有效載荷,即使我點擊了Title1。 – Devishankar

+0

請參閱我的編輯 – Rohit5k2

+0

'String msg = arg1.getStringExtra(「message」)' 這就是我從'onMessage(Context arg0,Intent arg1)獲取味精的方法' – Devishankar

相關問題