close

依照我之前的推播文章,很容易發現App在前景的時候會收不到推播

明明都按照 firebase 的測試發送的。

最近公司二月也剛好在整理 gcm/ fcm的部分,就趁機研究了下。

 

對於App來說,能收到資訊就算是成功了,無論App是否在前景。

因為都是透過 Server 發送給App。

App收到資料才會做事。

那為什麼App 在前景的時候會收不到資料呢?

 

問題思考方向可以轉為

為什麼沒有轉給App -> 什麼因素造成差異 -> server 發送推播的設定與格式問題嗎?

我當時就思考到這邊,開始找後端發送推播的方式有哪些,有什麼差異

並survey一下 fcm server 的相關資訊。

 

訊息的有效期 28 天(四周)

FCM 不保證每個device都可以即時收到訊息,如沒網路的裝置。

而 FCM 會儲存並嘗試傳遞消息的最長時間限制。

 

訊息長度限制

https://firebase.google.com/docs/cloud-messaging/concept-options#notifications_and_data_messages

就是 4KB, 約 2048 的中文字,若我計概學得沒錯的話。

若是透過 Firebase 後台的話,是1024的字元

 

*訊息發送格式 (重點)

Notification message

Use notification messages when you want FCM to handle displaying a notification on your client app's behalf.

基本上, 可以由service 定義 notification 的style, 相關 key 可以看 這裡

App 方面呼叫方式

在前景時

onMessageReceived 會呼叫

在背景時

onMessageReceived 不會呼叫

 

Data message

Use data messages when you want to process the messages on your client app.

在前景時

onMessageReceived 會呼叫

在背景時

onMessageReceived 會呼叫

 

簡單總結

這問題其實就是 server 傳送的格式

使用 data mesage 格式就可以 在前/被景都可以收到通知

 

 

參考資料

https://firebase.google.com/docs/cloud-messaging/concept-options

https://medium.com/%E7%A8%8B%E5%BC%8F%E8%A3%A1%E6%9C%89%E8%9F%B2/android-fcm-notification-message-vs-data-message-368dc0d4c1b4

arrow
arrow
    文章標籤
    Android notification 推播
    全站熱搜

    Owen Chen 發表在 痞客邦 留言(0) 人氣()