5

On Android 10, when I receive push notification, the vibration only works if app is opened. If it's in background or closed, vibration doesn't work (but notification get's through and sound works). Is this a bug? Couldn't find it in Google's bug-tracker though.

I send the push notification from our server with data payload, which ensures onMessageReceived() gets called even if app is in background. And it does, I can debug it.

Here's how notification channel is created:

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

And the notification itself:

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);
5
  • which all devices you have tried? Jul 13, 2020 at 7:37
  • I was able to reproduce this on Samsung Galaxy Note 10 and on Huawei P30.
    – c0dehunter
    Jul 13, 2020 at 8:28
  • Is the phone setting Apps > your app > Battery Life > Background Activity enabled for the app?
    – DrKaoliN
    Aug 10, 2020 at 4:58
  • Yes, it's enabled: prntscr.com/twxkic and prntscr.com/twxky2
    – c0dehunter
    Aug 10, 2020 at 5:14
  • are you sent vibrate parameters in notification object??? try your notification builder in on HandleIntent || (IntentHandler) Aug 10, 2020 at 12:28

5 Answers 5

2

You need to call the vibration service.

vibrator = (Vibrator)getSystemService(VIBRATOR_SERVICE);
vibrator.vibrate(2000);
0
1
+50

if Your device set Automatically miscellaneous notification channel in notification channel you can try Firebase default Chanel public static String NOTIFICATION_CHANNEL_ID ="fcm_fallback_notification_channel";

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

my 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)
        {
            //
        }
    }
}
5
  • I want to use my custom notification channel and I don't believe this affects vibration.
    – c0dehunter
    Aug 14, 2020 at 2:59
  • i got the same issue and right now i am using code it will make the behavior same for foreground and background push notification. i think you have to try this i will work for you. Aug 14, 2020 at 4:08
  • Can you show me your alarmAttributes? I think the key is there in setUsage() function. If wrong usage, it will not work in background.
    – c0dehunter
    Aug 14, 2020 at 4:15
  • I just found out the real problem, also thanks to your code. The problem happens if you use USAGE_NOTIFICATION_EVENT in AudtioAttributes's .setUsage()˙, but USAGE_NOTIFICATION works well!
    – c0dehunter
    Aug 14, 2020 at 4:24
  • now i have posted my full code i think it will help you.@PrimožKralj Aug 14, 2020 at 4:25
0

By the reference of Firebase, when your app is in the background, the notification is delivered to the device’s system tray. A user tap on a notification opens the app launcher by default.

Messages with both notification and data payload, when received in the background. In this case, the notification is delivered to the device’s system tray, and the data payload is delivered in the extras of the intent of your launcher Activity. That time vibration could not happen.

You can try to wake up the application when u received a notification. That might help you.

Firebase Reference

Android Pie And Newer version have some limitations. Check this reference

1
  • We already send "high" priority data messages, which always wake up the device. I also made sure to check there is no "vibrate" parameter in the HTTP REST API itself. You are supposed to do this on device - and I do. But it just won't work when app not in foreground, on Android 10 only.
    – c0dehunter
    Aug 12, 2020 at 5:10
0

Suggesting to question here: https://eu.community.samsung.com/t5/galaxy-s10e-s10-s10-s10-5g/s10-has-stopped-vibrating-when-reciving-notifications/m-p/1567092

Try heading to: Settings > Sound and vibration > Vibration intensity > Turn all options to max.

1
  • I don't have this specific options, but I made sure that vibration settings are on (I get vibration when app is in foreground).
    – c0dehunter
    Aug 14, 2020 at 3:00
0

I've now noticed this in the Logcat:

Ignoring incoming vibration as process with uid = 10587 is background, usage = USAGE_NOTIFICATION_EVENT

To make vibration work even when app in background, you need to use AudioAttributes.USAGE_NOTIFICATION (USAGE_NOTIFICATION_EVENT won't work!) AND also setVibrationPattern on channel:

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

Now vibration works when app in background too.

Thanks to ijigarsolanki's answer which helped me realize this.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.