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

Push Notifications don't vibrate when app is closed

在Android10,当我收到推送通知时,只有打开应用程序时振动才会起作用。如果它在后台或关闭,则振动不起作用(但通知通过并且声音有效)。这是一个错误吗?虽然无法在 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);

以及通知本身:

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

根据 Firebase 的参考,当您的应用程序在后台时,通知会传送到设备的系统托盘。用户点击通知会默认打开应用程序启动器。

在后台接收时同时包含通知和数据负载的消息。在这种情况下,通知会发送到设备的系统托盘,并且数据有效负载会在启动器 Activity 的额外内容中发送。那个时候振动是不可能发生的。

您可以尝试在收到通知时唤醒应用。这可能对你有帮助。

Firebase Reference

Android Pie 和更新版本有一些限制。检查这个 reference

您需要调用震动服务

vibrator = (Vibrator)getSystemService(VIBRATOR_SERVICE);
vibrator.vibrate(2000);

建议在这里提问:https://eu.community.samsung.com/t5/galaxy-s10e-s10-s10-s10-5g/s10-has-stopped-vibrating-when-reciving-notifications/m-p/1567092

尝试前往:设置 > 声音和振动 > 振动强度 > 将所有选项调到最大。

if Your device set Automatically miscellaneous notification channel in 通知渠道 你可以试试 Firebase 默认的 Chanel public static String NOTIFICATION_CHANNEL_ID ="fcm_fallback_notification_channel";

频道

  NotificationManager notificationManager = mContext.GetSystemService(Context.NotificationService) as NotificationManager;

            if (global::Android.OS.Build.VERSION.SdkInt >= global::Android.OS.BuildVersionCodes.O)
            {
                NotificationImportance importance = global::Android.App.NotificationImportance.High;
               

                NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, title, importance);
                notificationChannel.EnableLights(true);
                notificationChannel.EnableVibration(true);
                notificationChannel.SetSound(sound, alarmAttributes);
                notificationChannel.SetShowBadge(true);
                
                notificationChannel.Importance = NotificationImportance.High;
                notificationChannel.SetVibrationPattern(new long[] { 100, 200, 300, 400, 500, 400, 300, 200, 400 });

                if (notificationManager != null)
                {
                    mBuilder.SetChannelId(NOTIFICATION_CHANNEL_ID);
                    notificationManager.CreateNotificationChannel(notificationChannel);
                }
            }

            notificationManager.Notify(0, mBuilder.Build());

我的class:

class NotificationHelper : INotification
{
    private Context mContext;
    private NotificationManager mNotificationManager;
    private NotificationCompat.Builder mBuilder;       
    public static String NOTIFICATION_CHANNEL_ID = "fcm_fallback_notification_channel";

    public NotificationHelper()
    {
        mContext = global::Android.App.Application.Context;
    }
    public void CreateNotification(String title, String message)
    {
        try
        {
            var intent = new Intent(mContext, typeof(MainActivity));
            intent.AddFlags(ActivityFlags.ClearTop);
            intent.PutExtra(title, message);
            var pendingIntent = PendingIntent.GetActivity(mContext, 0, intent, PendingIntentFlags.OneShot);

            var sound = global::Android.Net.Uri.Parse(ContentResolver.SchemeAndroidResource + "://" + mContext.PackageName + "/"+Resource.Raw.notification);//add sound
          

            // Creating an Audio Attribute
            var alarmAttributes = new AudioAttributes.Builder()
                .SetContentType(AudioContentType.Sonification)             
                .SetUsage(AudioUsageKind.Notification).Build();

            mBuilder = new NotificationCompat.Builder(mContext);
            mBuilder.SetSmallIcon(Resource.Drawable.INH_ICON1);
            mBuilder.SetContentTitle(title)
                    .SetSound(sound)
                    .SetAutoCancel(true)
                    .SetContentTitle(title)
                    .SetContentText(message)
                    .SetChannelId(NOTIFICATION_CHANNEL_ID)
                    .SetPriority((int)NotificationPriority.High)
                    .SetLights(65536, 1000, 500)
                    //.SetColor(Resource.Color.)
                    .SetVibrate(new long[] { 100, 200, 300, 400, 500, 400, 300, 200, 400 })
                    .SetDefaults((int)NotificationDefaults.Sound | (int)NotificationDefaults.Vibrate)
                    .SetVisibility((int)NotificationVisibility.Public)
                    .SetSmallIcon(Resource.Drawable.INH_ICON1)
                    .SetContentIntent(pendingIntent);

           

            NotificationManager notificationManager = mContext.GetSystemService(Context.NotificationService) as NotificationManager;

            if (global::Android.OS.Build.VERSION.SdkInt >= global::Android.OS.BuildVersionCodes.O)
            {
                NotificationImportance importance = global::Android.App.NotificationImportance.High;
               

                NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, title, importance);
                notificationChannel.EnableLights(true);
                notificationChannel.EnableVibration(true);
                notificationChannel.SetSound(sound, alarmAttributes);
                notificationChannel.SetShowBadge(true);
                
                notificationChannel.Importance = NotificationImportance.High;
                notificationChannel.SetVibrationPattern(new long[] { 100, 200, 300, 400, 500, 400, 300, 200, 400 });

                if (notificationManager != null)
                {
                    mBuilder.SetChannelId(NOTIFICATION_CHANNEL_ID);
                    notificationManager.CreateNotificationChannel(notificationChannel);
                }
            }

            notificationManager.Notify(0, mBuilder.Build());
        }
        catch (Exception ex)
        {
            //
        }
    }
}

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

Ignoring incoming vibration as process with uid = 10587 is background, 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);

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

感谢 帮助我实现了这一点。