【问题标题】:Push Notifications don't vibrate when app is closed应用关闭时推送通知不振动
【发布时间】:2020-07-13 05:47:33
【问题描述】:

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

还有通知本身:

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

【问题讨论】:

  • 你尝试过哪些设备?
  • 我能够在三星 Galaxy Note 10 和华为 P30 上重现这一点。
  • 手机设置Apps > 你的应用 > Battery Life > Background Activity 为应用启用了吗?
  • 是的,它已启用:prntscr.com/twxkicprntscr.com/twxky2
  • 你是否在通知对象中发送了振动参数???在 HandleIntent 上尝试您的通知生成器 || (IntentHandler)

标签: android push-notification firebase-cloud-messaging android-notifications


【解决方案1】:

您需要调用振动服务。

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

【讨论】:

    【解决方案2】:

    如果您的设备在通知通道中设置了自动杂项通知通道 你可以试试 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 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)
            {
                //
            }
        }
    }
    

    【讨论】:

    • 我想使用我的自定义通知渠道,我认为这不会影响振动。
    • 我遇到了同样的问题,现在我正在使用代码,它将使前台和后台推送通知的行为相同。我想你必须试试这个我会为你工作。
    • 你能告诉我你的alarmAttributes吗?我认为关键在于 setUsage() 函数。如果使用错误,它将无法在后台运行。
    • 我刚刚发现了真正的问题,也感谢您的代码。如果您在 AudioAttributes 的 .setUsage()˙ 中使用 USAGE_NOTIFICATION_EVENT,则会出现问题,但 USAGE_NOTIFICATION 效果很好!
    • 现在我已经发布了我的完整代码,我认为它会对你有所帮助。@PrimožKralj
    【解决方案3】:

    通过 Firebase 的引用,当您的应用处于后台时,通知会发送到设备的系统托盘。默认情况下,用户点击通知会打开应用启动器。

    在后台接收时带有通知和数据负载的消息。在这种情况下,通知被传递到设备的系统托盘,数据有效负载在启动器 Activity 的意图的附加内容中传递。那时的振动不可能发生。

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

    Firebase Reference

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

    【讨论】:

    • 我们已经发送了“高”优先级数据消息,这些消息总是会唤醒设备。我还确保检查HTTP REST API itself 中没有“振动”参数。您应该在设备上执行此操作 - 我会这样做。但是,当应用不在前台时,它就无法工作,仅在 Android 10 上。
    【解决方案4】:

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

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

    【讨论】:

    • 我没有这个特定选项,但我确保振动设置已打开(当应用程序处于前台时我会收到振动)。
    【解决方案5】:

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

    忽略传入的振动作为 uid = 10587 的进程是背景, 用法 = 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);
    

    现在,当应用在后台时,振动也会起作用。

    感谢ijigarsolanki's answer 帮助我实现了这一点。

    【讨论】:

      猜你喜欢
      • 2017-01-03
      • 2022-08-11
      • 2014-08-10
      • 1970-01-01
      • 2017-09-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多