1

我正在使用com.google.firebase:firebase-messaging:10.0.1進行Android和嘗試使設備在應用程序處於後臺並收到推送通知時進行振動。 我發送以下JSON來FCM服務器:Firebase推送通知在後臺振動

to: 'topics/...', 
notification: { 
    title: 'New Message', 
    body: 'top security body', 
    sound: 'mysound', 
    action_click: 'top_security' 
} 

當應用程序在前臺,被觸發的FirebaseMessagingServiceonMessageReceived方法,我可以添加震動。但是,當應用程序在後臺,onMessageReceived不叫,我無法控制振動。我的第一個想法是使用data塊代替背景和前景,但如果沒有notification塊,iOS客戶端不會在後臺接收到推送。

那麼如何添加振動來推送通知,當應用程序在後臺? P.S.當我在設備上打開振動模式時,推送通知會導致振動而不是播放聲音。

+0

,因爲您需要發送無聲通知,按照這個[鏈接](http://stackoverflow.com/questions/37570200/firebase-silent-apns-notification) –

+0

請參閱我的回答在這個鏈接,http://stackoverflow.com/a/42505362/1404798 – Thirumalvalavan

+0

@mahmoudmoustafa我正在使用android,看標籤 – IlyaEremin

回答

0

使用data有效載荷而不是notification有效載荷,因爲即使應用程序是背景或前景,數據有效載荷也會觸發onMessageReceived。您的要求將這個樣子

data: { 
    title: 'New Message', 
    body: 'top security body', 
    sound: 'mysound', 
    action_click: 'top_security' 
} 

data有效載荷獲取裏面的數據onMessageReceived,叫remoteMessage.getData();將在一個Map<String,String>

返回結果。例如:

Map<String, String> data = remoteMessage.getData(); 
String title = data.get("title"); 
String body = data.get("body"); 

然後定製根據您的需要通知該數據。

希望這有助於:)

+0

我們使用推送通知android和ios。如果沒有'notification'塊,iOS客戶端不會在後臺顯示推送通知。 – IlyaEremin

+0

您還可以在一個請求中組合兩個有效負載(通知和數據)。你試過了嗎? @IlyaEremin – Wilik