问题
I'm using Xamarin.Forms but I'm having a few problems handling push notifications. They only work fine when app is in foreground.
When the app is in background, all notifications are received and displayed correctly but I can't manage to get them to vibrate or even turn led on. I have found a lot of documentation but it's pretty old and most of the time, methods referred are deprecated.
Anyway, I have a FirebaseMessagingService to handle FCM events and this is the action that generates the push notification:
void SendNotification(RemoteMessage message)
{
var intent = new Intent(this, typeof(MainActivity));
intent.AddFlags(ActivityFlags.ClearTop);
intent.PutExtra("type", message.Data["type"]);
var pendingIntent = PendingIntent.GetActivity(this, 0, intent, PendingIntentFlags.OneShot);
NotificationManager notificationManager = (NotificationManager)ApplicationContext.GetSystemService(NotificationService);
NotificationCompat.Builder builder = null;
if (Android.OS.Build.VERSION.SdkInt >= Android.OS.BuildVersionCodes.O)
{
var importance = NotificationImportance.Default;
NotificationChannel notificationChannel = new NotificationChannel("ID", "Name", importance);
notificationManager.CreateNotificationChannel(notificationChannel);
builder = new NotificationCompat.Builder(ApplicationContext, notificationChannel.Id);
}
else
builder = new NotificationCompat.Builder(ApplicationContext);
builder = builder
.SetSmallIcon(Resource.Drawable.icon)
.SetDefaults((int)((int)NotificationDefaults.Sound | (int)NotificationDefaults.Vibrate))
.SetAutoCancel(true)
.SetContentTitle(message.GetNotification().Title)
.SetContentText(message.GetNotification().Body)
.SetContentIntent(pendingIntent)
.SetLights(65536, 1000, 500)
.SetVibrate(new long[] { 1000, 1000, 1000 })
.SetPriority((int)NotificationPriority.High);
PowerManager pm = (PowerManager)ApplicationContext.GetSystemService(PowerService);
WakeLock wl = pm.NewWakeLock(WakeLockFlags.Partial, "TAG");
wl.Acquire(15000);
notificationManager.Notify(0, builder.Build());
wl.Release();
}
I also added these lines in the Android.Manifest file:
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.VIBRATE"/>
It was pretty much simpler before but I've been adding new things trying to make it work, like the WAKE_LOCK section or the distinction of Android Version when creating the Builder object. It vibrates only when app is in foreground.
I even tried using the James Montemagno's Vibrate Plugin or using Android.Vibrator class and they didn't work either when app is in background.
I'm open to suggestions here. Thanks.
回答1:
On Oreo(+), you have to enable the vibration on the NotificationChannel via EnableVibration(true).
Oreo Example:
NotificationChannel channel;
channel = notificationManager.GetNotificationChannel(myUrgentChannel);
if (channel == null)
{
channel = new NotificationChannel(myUrgentChannel, channelName, NotificationImportance.High);
channel.EnableVibration(true);
channel.EnableLights(true);
channel.SetSound(
RingtoneManager.GetDefaultUri(RingtoneType.Notification),
new AudioAttributes.Builder().SetUsage(AudioUsageKind.Notification).Build()
);
channel.LockscreenVisibility = NotificationVisibility.Public;
notificationManager.CreateNotificationChannel(channel);
}
channel.Dispose();
回答2:
After reviewing everything over and over again all I needed was adding this to the payload. I didn't know this must be included in order for it to vibrate. I wasn't able to modify this behavior programmatically but I'm sure it's possible.
"notification":{
"sound":"default"
}
FOUND IT HERE Vibration on push-notification not working when phone locked/app not open
来源:https://stackoverflow.com/questions/49282741/xamarin-forms-fcm-push-notifications-wont-vibrate-if-app-is-in-background-in-a