本文介绍了Xamarin形式:推送通知在Android 7.1.2上不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
推送通知在版本号为7.1.2的Android设备上无法正常运行,但在版本9上可以正常运行.以下是我用于显示通知的代码.
Push notification is not working on Android device having a version number 7.1.2, but working fine on version 9. Following is my code for showing the notification.
if (Build.VERSION.SdkInt < BuildVersionCodes.O)
{
var intent = new Intent(this, typeof(MainActivity));
intent.AddFlags(ActivityFlags.ClearTop);
var pendingIntent = PendingIntent.GetActivity(this, 0, intent, PendingIntentFlags.OneShot);
var notificationBuilder = new Android.App.Notification.Builder(this, Utils.CHANNEL_ID)
.SetContentTitle(Header)
.SetSmallIcon(Resource.Drawable.icon)
.SetContentText(body)
.SetAutoCancel(true)
.SetContentIntent(pendingIntent);
var notificationManager = NotificationManager.FromContext(this);
notificationManager.Notify(0, notificationBuilder.Build());
}
else
{
var intent = new Intent(this, typeof(MainActivity));
intent.AddFlags(ActivityFlags.ClearTop);
var pendingIntent = PendingIntent.GetActivity(this, 0, intent, PendingIntentFlags.OneShot);
var notificationBuilder = new Android.App.Notification.Builder(this, Utils.CHANNEL_ID)
.SetContentTitle(Header)
.SetSmallIcon(Resource.Drawable.icon)
.SetContentText(body)
.SetAutoCancel(true)
.SetContentIntent(pendingIntent)
.SetChannelId(Utils.CHANNEL_ID);
if (Build.VERSION.SdkInt < BuildVersionCodes.O)
{
return;
}
var channel = new NotificationChannel(Utils.CHANNEL_ID, "FCM Notifications", NotificationImportance.High)
{
Description = "Firebase Cloud Messages appear in this channel"
};
var notificationManager = (NotificationManager)GetSystemService(NotificationService);
notificationManager.CreateNotificationChannel(channel);
notificationManager.Notify(0, notificationBuilder.Build());
}
有人可以为此提出建议吗?
Can anyone please suggest a solution for this?
推荐答案
我从生产项目中发布了SendNotification方法.
I post my SendNotification method from my production project.
按照我的工作代码并更改您的方法.
Following my working code and change your method.
void SendNotification (string messageBody, string title)
{
var intent = new Intent (this, typeof (SplashActivity));
intent.AddFlags (ActivityFlags.ClearTop);
var pendingIntent = PendingIntent.GetActivity (this, 0, intent, PendingIntentFlags.OneShot);
//if i want more than one notification ,different unique value in every call
Random u = new Random ();
if (Build.VERSION.SdkInt >= BuildVersionCodes.O) {
string channelName = Resources.GetString (Resource.String.channel_name);
NotificationCompat.Builder notificationBuilder;
notificationBuilder = new NotificationCompat.Builder (this, channelName)
.SetContentTitle (title)
.SetSmallIcon (Resource.Drawable.ic_stat_g)
.SetContentText (messageBody)
.SetAutoCancel (true)
.SetContentIntent (pendingIntent);
var notificationManager = GetSystemService (Context.NotificationService) as NotificationManager;
NotificationChannel channel = new NotificationChannel (channelName, "notification channel", NotificationImportance.Default);
notificationManager.CreateNotificationChannel (channel);
notificationManager.Notify (u.Next(), notificationBuilder.Build ());
}
else
{
NotificationCompat.Builder notificationBuilder;
notificationBuilder = new NotificationCompat.Builder (this)
.SetContentTitle (title)
.SetSmallIcon (Resource.Drawable.ic_stat_g)
.SetContentText (messageBody)
.SetAutoCancel (true)
.SetContentIntent (pendingIntent);
var notificationManager = GetSystemService (Context.NotificationService) as NotificationManager;
notificationManager.Notify (u.Next(), notificationBuilder.Build ());
}
}
首先不使用Android.App.Notification.Builder. NotificationCompat.Builder是新的.
First of all don't use Android.App.Notification.Builder is deprecated. The NotificationCompat.Builder is the new one.
也许这是您的新代码
var intent = new Intent(this, typeof(MainActivity));
intent.AddFlags(ActivityFlags.ClearTop);
var pendingIntent = PendingIntent.GetActivity(this, 0, intent, PendingIntentFlags.OneShot);
if (Build.VERSION.SdkInt < BuildVersionCodes.O)
{
var notificationBuilder = new NotificationCompat.Builder(this)
.SetContentTitle(Header)
.SetSmallIcon(Resource.Drawable.icon)
.SetContentText(body)
.SetAutoCancel(true)
.SetContentIntent(pendingIntent);
var notificationManager = GetSystemService (Context.NotificationService) as NotificationManager;
notificationManager.Notify(0, notificationBuilder.Build());
}
else
{
var notificationBuilder = new NotificationCompat.Builder(this, Utils.CHANNEL_ID)
.SetContentTitle(Header)
.SetSmallIcon(Resource.Drawable.icon)
.SetContentText(body)
.SetAutoCancel(true)
.SetContentIntent(pendingIntent);
var notificationManager = GetSystemService (Context.NotificationService) as NotificationManager;
NotificationChannel channel = new NotificationChannel (Utils.CHANNEL_ID, "FCM Notifications", NotificationImportance.Default);
notificationManager.CreateNotificationChannel (channel);
notificationManager.Notify (0, notificationBuilder.Build ());
}
这篇关于Xamarin形式:推送通知在Android 7.1.2上不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!