2016-07-18 162 views
7

使用Firebase Cloud Messaging在後臺接收通知時,是否可以使Android設備發生振動?從Syntax Reference我已經看到並測試聲音支持,但沒有振動的信息。或者是否有可能爲此通知提供自定義實現,以便直接設置振動?Firebase Cloud Messaging通知振動

+1

我認爲您需要在構建通知時實施。你以前見過這個[post](http://stackoverflow.com/q/18253482/4625829)嗎? –

+2

FCM的事情是,當發送通知並且應用程序位於後臺時,[通知內部構建](https://firebase.google.com/docs/cloud-messaging/concept-options#notifications)。我認爲自定義通知的解決方案是僅提供數據負載,因此[保證我的服務的onMessageReceived()方法將被調用](https://firebase.google.com/docs/cloud-messaging/concept-options #notifications_and_data_messages),然後在方法實現中構建通知 – apidae

+0

另請參閱https://stackoverflow.com/questions/42555899/firebase-push-notifications-vibrate-in-background – Kato

回答

7

從我所測試的FCM通知消息的格式似乎是相當嚴格的,所以對於自定義通知,該解決方案似乎是:

  1. 確保該JSON消息的有效載荷發送下游包含data參數,但包含notification參數(來源:Message JSON SyntaxFCM Message Types
  2. 從應用程序的FirebaseMessagingService創建的覆蓋onMessageReceived(RemoteMessage remoteMessage)執行情況的通知(Example
+0

這只是通知**聲音**或還**振動**? – newbieguy

14

你應該標記自己是一個正確的答案。

在旁註,如果你發送通知和數據有效載荷,可使用

"notification":{ 
    "sound":"default" 
} 

當應用程序在後臺和設備設置聲音這將播放的聲音,它會玩如果設備設置爲振動,則會發生振動。

但是,就自定義而言,我認爲只有在您想要自定義振動模式時才需要使用數據有效載荷。

-1

array("title"=>"hello","body" =>"hello world",'sound' => 'default')

+0

將它存儲在一個變量中,然後使用此函數 –

+0

function send_notification($ tokens,$ notification) { $ url ='https://fcm.googleapis.com/fcm/send'; $ priority =「high」; $字段=陣列( 'registration_ids'=> $令牌, '通知'=> $通知 ); –

+0

將代碼添加到答案中 – Raviteja

相關問題