手机锁定/应用程序未打开时推送通知的振动不起作用

时间:2017-08-09 10:51:28

标签: android push-notification

始终发送应用程序的推送通知,这不是问题。问题是,只有在应用程序打开且手机未锁定的情况下,如果手机从应用程序收到新通知,则手机会振动。 如果应用未打开或手机已锁定,手机将不会因收到的通知而振动。 有没有办法让手机在收到的通知上振动 ,即使它被锁定或应用程序未打开?可能是什么问题?根据我的发现,通过以下代码创建通知:

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

清单具有vibrate和wake_lock权限:

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

2 个答案:

答案 0 :(得分:4)

通知有效负载中的

添加:

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

如果它不是静音模式并且在振动模式下振动,则会发出声音

答案 1 :(得分:0)

firebase数据消息和firebase通知消息之间存在很大差异。

来自docs

  

当应用在后台时,通知消息将传递到通知托盘。对于前台的应用程序,消息由这些回调处理:
  Android上的onMessageReceived()

对于数据消息

  

在Android上,客户端应用程序在onMessageReceived()

中接收数据消息

表示您无法处理在后台接收的通知消息。我会发送一条包含标题和消息的数据消息,并在您的应用中手动解析它。这允许您启用振动。

或者,如果使用firebase控制台,则可以在发送消息时在展开的选项中启用“声音”。