Vibration on push-notification not working when ph

2020-07-24 03:47发布

The push-notifications to the app are always sent, that's not the problem. The problem is that the phone vibrates if it receives a new notifications from the app only if the app is open and the phone is not locked. If either the app is not open or the phone is locked, the phone will not vibrate on received notifications. Would there be a way to make the phone vibrate on received notifications even if it is locked or the app is not open? What could be the problem? From what I found, the notifications are created with the help of the following code:

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);
    }
}

The manifest has the vibrate and wake_lock permissions:

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

2条回答
地球回转人心会变
2楼-- · 2020-07-24 03:51

in the notification payload add this :

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

This will run sound if it's not silence mode and vibrate when it's vibration mode

查看更多
对你真心纯属浪费
3楼-- · 2020-07-24 03:57

There's a big difference between firebase data messages and firebase notification messages.

From the docs:

Notification messages are delivered to the notification tray when the app is in the background. For apps in the foreground, messages are handled by these callbacks:
onMessageReceived() on Android

And for data messages

On Android, a client app receives a data message in onMessageReceived()

Means you can't handle notification messages that are received in background. I would send a data message that just contains title and message and parse it manualy in your app. This allows you to enable vibrations.

Alternatively, if using the firebase console, you can enable "sound" in the expanded options when sending a message.

查看更多
登录 后发表回答