Solution 1 :
You need to call the vibration service.
vibrator = (Vibrator)getSystemService(VIBRATOR_SERVICE);
vibrator.vibrate(2000);
Solution 2 :
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)
{
//
}
}
}
Solution 3 :
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.
Android Pie And Newer version have some limitations. Check this reference
Solution 4 :
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.
Solution 5 :
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.
Problem :
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);
Comments
Comment posted by Manoj Perumarath
which all devices you have tried?
Comment posted by c0dehunter
I was able to reproduce this on Samsung Galaxy Note 10 and on Huawei P30.
Comment posted by DrKaoliN
Is the phone setting
Comment posted by prntscr.com/twxkic
Yes, it’s enabled:
Comment posted by Shahriyar Aghajani
are you sent vibrate parameters in notification object??? try your notification builder in on HandleIntent || (IntentHandler)
Comment posted by c0dehunter
I want to use my custom notification channel and I don’t believe this affects vibration.
Comment posted by ijigarsolanki
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.
Comment posted by c0dehunter
Can you show me your
Comment posted by c0dehunter
I just found out the real problem, also thanks to your code. The problem happens if you use
Comment posted by ijigarsolanki
now i have posted my full code i think it will help [email protected]žKralj
Comment posted by HTTP REST API itself
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
Comment posted by c0dehunter
I don’t have this specific options, but I made sure that vibration settings are on (I get vibration when app is in foreground).