应用程序关闭时推送通知不会振动

Pri*_*alj 5 android push-notification android-notifications firebase-cloud-messaging

在 Android 10 上,当我收到推送通知时,振动仅在应用程序打开时有效。如果它在后台或关闭,振动不起作用(但通知通过并且声音起作用)。这是一个错误吗?但是在 Google 的错误跟踪器中找不到它。

我从我们的服务器发送带有数据负载的推送通知,这确保onMessageReceived()即使应用程序在后台也能被调用。确实如此,我可以调试它。

以下是通知渠道的创建方式:

NotificationChannel mChannelUp = new NotificationChannel(CHANNEL_ID_ALERTS_UP, getApplicationContext().getString(R.string.alerts_up), NotificationManager.IMPORTANCE_HIGH);
mChannelUp.enableLights(true);
mChannelUp.enableVibration(true);
//        mChannelUp.setVibrationPattern(new long[]{500, 250, 500, 250}); // doesn't help
mChannelUp.setLightColor(Color.BLUE);

AudioAttributes audioAttributesUp = new AudioAttributes.Builder()
        .setUsage(AudioAttributes.USAGE_NOTIFICATION_EVENT)
        .build();

mChannelUp.setSound(Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://" + getPackageName() + "/" + R.raw.notif_sound_up), audioAttributesUp);
notificationManager.createNotificationChannel(mChannelUp);
Run Code Online (Sandbox Code Playgroud)

和通知本身:

NotificationCompat.Builder mBuilder =
        new NotificationCompat.Builder(this, CHANNEL_ID_ALERTS_UP)
                .setAutoCancel(true)
                .setSmallIcon(R.drawable.ic_notif)
                .setColor(ContextCompat.getColor(this, R.color.colorPrimary))
                .setLargeIcon(logo)
                .setVibrate(new long[]{250, 500, 250, 500})
                .setLights(Color.parseColor("#039be5"), 500, 500)
                .setContentTitle("some title")
                .setContentText("some content")
                .setSound(Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://" + getPackageName() + "/" + R.raw.notif_sound_up));

mBuilder.setContentIntent(resultPendingIntent);

Notification notif = mBuilder.build();

NotificationManager mNotifyMgr = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
mNotifyMgr.notify(notificationId, notif);
Run Code Online (Sandbox Code Playgroud)

Pri*_*alj 0

我现在在 Logcat 中注意到了这一点:

忽略传入的振动,因为 uid = 10587 的进程是后台,usage = USAGE_NOTIFICATION_EVENT

要使振动即使在应用程序处于后台时也能工作,您需要使用AudioAttributes.USAGE_NOTIFICATIONUSAGE_NOTIFICATION_EVENT不会工作!)并且还在setVibrationPattern频道上使用:

AudioAttributes audioAttributes = new AudioAttributes.Builder()
        .setUsage(AudioAttributes.USAGE_NOTIFICATION)
        .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
        .build();

NotificationChannel mChannelUp = new NotificationChannel(CHANNEL_ID_ALERTS_UP, getApplicationContext().getString(R.string.alerts_up), NotificationManager.IMPORTANCE_HIGH);
mChannelUp.enableLights(true);
mChannelUp.enableVibration(true);
mChannelUp.setVibrationPattern(new long[]{0, 300, 250, 300, 250, 300});
mChannelUp.setLightColor(Color.BLUE);
mChannelUp.setSound(Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://" + getPackageName() + "/" + R.raw.notif_sound_up), audioAttributes);
notificationManager.createNotificationChannel(mChannelUp);
Run Code Online (Sandbox Code Playgroud)

现在,当应用程序在后台运行时,振动也可以工作。

感谢ijigarsolanki 的回答帮助我意识到了这一点。