2017-08-09 55 views
3

應用程序的推送通知始終發送,但這不是問題。問題是,只有當應用程序打開且手機未鎖定時,手機纔會收到來自應用程序的新通知。 如果應用程序未打開或手機已鎖定,手機將不會收到通知時震動。 是否有辦法使手機在收到通知時發生振動即使它被鎖定或應用程序未打開?可能是什麼問題?從我發現,通知與下面的代碼的幫助下創建:當手機鎖定/應用程序未打開時,推送通知中的振動無法工作

public class NotifyFirebaseMessagingService extends FirebaseMessagingService { 
    @Override 
    public void onMessageReceived(RemoteMessage remoteMessage) { 
     NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); 
     manager.notify(
       remoteMessage.getMessageId(), 
       1, 
       new NotificationCompat.Builder(this) 
         .setSmallIcon(R.drawable.ic_paperplane) 
         .setContentTitle(remoteMessage.getNotification().getTitle()) 
         .setContentText(remoteMessage.getNotification().getBody()) 
         .setVibrate(new long[] { 150, 300, 150, 600}) 
         .setAutoCancel(true) 
         .build()); 
     super.onMessageReceived(remoteMessage); 
    } 
} 

清單有震動和WAKE_LOCK權限:

<uses-permission android:name="android.permission.WAKE_LOCK" /> 
<uses-permission android:name="android.permission.VIBRATE"/

回答

2

通知有效載荷補充一點:

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

這聲音運行,如果它不在沉默模式和振動時,它的振動模式

+0

完美工作,感謝您的幫助。有沒有辦法使聲音和振動? – Cctest

0

有火力點的數據信息和火力之間有很大的區別通知消息。

docs:當所述應用程序是在後臺

通知消息被傳遞到通知托盤。在前臺應用程序,消息由這些回調處理:
onMessageReceived()在Android

和用於數據消息

在Android上,一個客戶端應用程序在onMessageReceived接收的數據消息()

表示您無法處理在後臺收到的通知消息。我會發送一個包含標題和消息的數據消息,並在您的應用程序中手動解析它。這使您可以啓用振動。

或者,如果使用Firebase控制檯,則可以在發送消息時在擴展選項中啓用「聲音」。

相關問題