c0dehunter
c0dehunter

Reputation: 6150

Push Notifications don't vibrate when app is closed

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

Upvotes: 5

Views: 2080

Answers (5)

c0dehunter
c0dehunter

Reputation: 6150

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.

Upvotes: 0

ijigarsolanki
ijigarsolanki

Reputation: 131

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)
        {
            //
        }
    }
}

Upvotes: 1

Axbor Axrorov
Axbor Axrorov

Reputation: 2806

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.

Upvotes: 0

MitchHS
MitchHS

Reputation: 353

You need to call the vibration service.

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

Upvotes: 2

Yabaze Cool
Yabaze Cool

Reputation: 601

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

Upvotes: 0

Related Questions