2
  1. 發送的定期通知(在JSON體「通知」,而不是一個自定義的數據通知)
  2. 有應用程序在後臺這樣onMessageReceived()的應用程序實現沒有觸發,系統負責顯示通知。
  3. 當通知被點擊時,MainActivity被觸發。似乎沒有辦法檢索Acivity中的通知標題和正文(notification JSON部分),只能從Bundle中檢索data部分。

我的MainActivity:獲得身體和定期通報標題活動在Android

Intent intent = getIntent(); 
if (intent.getExtras() != null) { 
    debug("Intent with extras"); 

    Bundle b = getIntent().getExtras(); 
    for (String key : b.keySet()) { 
     Object value = b.get(key); 
     Log.d(TAG, String.format("%s %s (%s)", key, 
       value.toString(), value.getClass().getName())); 
    } 
} 

日誌輸出:

意圖藉助額外google.sent_time 1487153876625(java.lang.Long中) 的OpenURL http://192.168.29.121:8083/v1/messages/87046D17-6470-427C-A046-2E1C92E21D23/open (java.lang.String中)customProperties {「big_icon_urll」:「link」} (java.lang.String)from 814199820217(java.lang.String) google.message_id 0 :1487153876640504%a38586a6a38586a6 (java.lang.String中)collapse_key的com.kumuluz.ccm.android.sample (java.lang.String中)

正如你所看到的,只有數據部分被賦予(的OpenURL, customProperties裏面......)。

如果我手動處理自定義通知,這不是一個問題,因爲我可以將標題和正文推到extras中,然後它們會出現在Bundle中。但是,當應用程序處於後臺時,如何從系統處理的通知中獲得標題和正文?

我在Intent上沒有看到任何合適的方法來檢索這個。

+0

你找到一個解決方案@cen –

+0

@ G.hakim沒有,我不得不使用定製的通知 – cen

+0

我得到了同樣的問題。似乎沒有辦法從已啓動的活動訪問標題和正文:( –

回答

-1

試試這個,

@Override 
    protected void onMessage(final Context context, final Intent intent) 
    { 
     try { 
      final String message = intent.getExtras().getString("message"); 
      if (message != null) { 
       JSONObject obj = new JSONObject(message); 
       String body = obj.getString("body"); 
       String title = obj.getString("title"); 
       Log.i(TAG, "body:" + body + " title:" + title); 
      } 

     } 
     catch (JSONException e) { 
     e.printStackTrace(); 
     } 
    } 
+1

我正在使用FCM,因此我假設您的意思是FirebaseMessagingService中的onMessageReceived?當應用程序處於後端時發送reguar通知時,不會調用此方法。 – cen