0

我有兩個不同的通知。
其中一條是消息,另一條是通知其他內容。
我想分開通知。
例如,當我收到通知消息並點擊它時,會打開聊天室,而另一個會打開另一個活動。如何在Android上分開FCM通知?

+3

您可以通過鍵入自定義負載區分在FCM –

+0

打招呼的身體怎麼做到這一點 – Mohamed

+0

檢查我的回答 –

回答

4

以下是發送下行消息所需的一些基本屬性。

- 類型字符串 - (可選)[消息的接收者] 該值必須是一個單個註冊令牌,通知鍵,或主題。類型字符串數組 - - (可選)[消息的接收者] 多個註冊標記,分1最大值1000

優先發送到多個主題

registration_ids當不設置這個字段- 類型字符串 - (可選)[默認正常] 允許的值爲normal和high。

delay_while_idle- 類型布爾 - (可選)[默認值假] true表示直到設備變爲活動的消息不應被髮送。

time_to_live- 類型JSON數 - (可選)[默認值4周最多4周] 此參數指定消息應該多長時間(以秒爲單位)被保持在FCM存儲如果該設備是離線

數據- 類型JSON對象 指定消息有效負載的自定義鍵值對。 例如。{「POST_ID」:」 1234「,」 POST_TITLE」:」博客文章標題」}

在Android中你會接受她在onMessageReceived()作爲地圖數據...

public class FcmMessageService extends FirebaseMessagingService{ 
    @Override 
    public void onMessageReceived(RemoteMessage remoteMessage) { 
     //onMessageReceived will be called when ever you receive new message from server.. (app in background and foreground) 
     Log.d("FCM", "From: " + remoteMessage.getFrom()); 

     if(remoteMessage.getNotification()!=null){ 
     Log.d("FCM", "Notification Message Body: " + remoteMessage.getNotification().getBody()); 
     } 

     if(remoteMessage.getData().containsKey("post_id") && remoteMessage.getData().containsKey("post_title")){ 
      Log.d("Post ID",remoteMessage.getData().get("post_id").toString()); 
      Log.d("Post Title",remoteMessage.getData().get("post_title").toString()); 
      // eg. Server Send Structure data:{"post_id":"12345","post_title":"A Blog Post"} 
     } 
    } 
} 
+0

嗨哪裏是一個類型的通知(消息,通知符)告訴我的JSON感謝 – Mohamed

+0

這是ANS的例子,但你可以使用 {「type」:「type1」,「post_id」:「1234」,「post_title」:「博客帖子標題」} –

+0

非常匹配 – Mohamed

0

爲此,您應該使用click_action字段,該字段允許您指定用戶點擊通知時要啓動的活動。

所以在您的通知有效載荷:

click_action: "<intent to launch>" 

如果沒有定義,則click_action默認爲啓動意向書/活動。