【问题标题】:Vibration on push-notification not working when phone locked/app not open当手机锁定/应用程序未打开时,推送通知的振动不起作用
【发布时间】:2017-08-09 19:20:51
【问题描述】:

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

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

清单具有振动和唤醒锁定权限:

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

【问题讨论】:

    标签: android push-notification


    【解决方案1】:

    在通知负载中添加:

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

    如果不是静音模式,它会发出声音,当它是振动模式时会振动

    【讨论】:

    • 完美运行,感谢您的帮助。有没有办法让它发出声音和振动?
    【解决方案2】:

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

    来自docs

    当应用程序在后台时,通知消息会发送到通知托盘。对于前台应用,消息由以下回调处理:
    Android 上的onMessageReceived()

    对于数据消息

    在 Android 上,客户端应用在 onMessageReceived() 中接收数据消息

    意味着您无法处理在后台收到的通知消息。我会发送一条仅包含标题和消息的数据消息,并在您的应用程序中手动解析它。这使您可以启用振动。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-09-13
      • 2013-09-04
      • 1970-01-01
      • 2012-10-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多