API 27 中未显示本地通知
Posted
技术标签:
【中文标题】API 27 中未显示本地通知【英文标题】:Local Notifications are not showing in API 27 【发布时间】:2019-02-15 07:36:59 【问题描述】:通知适用于 Api 26 及以下版本,但不适用于 API 27。
这是我创建通知通道的代码:
private void CreateNotificationChannel()
try
if (Build.VERSION.SdkInt < BuildVersionCodes.O)
return;
var notificationManager = (NotificationManager)GetSystemService(NotificationService);
NotificationChannel mChannel = notifManager.GetNotificationChannel("1");
if (mChannel == null)
mChannel = new NotificationChannel("1", "Chat Application", android.App.NotificationImportance.High);
mChannel.EnableVibration(true);
mChannel.SetVibrationPattern(new long[] 100, 200, 300, 400, 500, 400, 300, 200, 400 );
notifManager.CreateNotificationChannel(mChannel);
catch (Exception exception)
LoggingManager.Error(exception);
我的通知服务是:
var activity = Forms.Context as Activity;
Intent intent = new Intent(activity, typeof(MainActivity));
intent.AddFlags(ActivityFlags.ClearTop);
Random random = new Random();
int pushCount = random.Next(9999 - 1000) + 1000; //for multiplepushnotifications
intent.AddFlags(ActivityFlags.ClearTop);
var pendingIntent = PendingIntent.GetActivity(activity, pushCount, intent, PendingIntentFlags.Immutable);
// Instantiate the builder and set notification elements:
NotificationCompat.Builder builder = new NotificationCompat.Builder(Forms.Context,"1")
.SetContentTitle(messageTitle)
.SetDefaults(1|2)
.SetContentText(Message)
.SetContentIntent(pendingIntent)
.SetAutoCancel(true)
.SetChannelId("1")
.SetPriority(1);
builder.SetSmallIcon(Resource.Drawable.icon);
// Build the notification:
Notification notification = builder.Build();
// Get the notification manager:
NotificationManager notificationManager = Forms.Context.GetSystemService(Context.NotificationService) as NotificationManager;
// Publish the notification:
notificationManager.Notify(5, notification);
请帮助我或给我一些建议,我该如何解决这个问题。
【问题讨论】:
notifManager
中的notifManager.GetNotificationChannel("1");
是什么NotificationManager
?
@G.hakim,是的,它是一个 NotificationManager
您将通知渠道代码放在哪里?
@G.hakim,我已将它放在我的 MainActivity.cs 文件中
【参考方案1】:
我感觉你的频道创建有问题,你可以在下面检查我的工作代码。
var mChannel = new NotificationChannel(CHANNEL_ID, "Chat Application", Android.App.NotificationImportance.High)
Description = "Firebase Cloud Messages appear in this channel"
;
mChannel.EnableVibration(true);
mChannel.SetVibrationPattern(new long[] 100, 200, 300, 400, 500, 400, 300, 200, 400 );
var notificationManager = (NotificationManager) GetSystemService(NotificationService);
notificationManager.CreateNotificationChannel(channel);
【讨论】:
【参考方案2】:你能试试吗 notificationManager.Notify(new Random().Next(), notification);
而不是 notificationManager.Notify(5, 通知);
【讨论】:
,我也试过了,但仍然没有显示通知以上是关于API 27 中未显示本地通知的主要内容,如果未能解决你的问题,请参考以下文章
在某些设备中未显示 React 本机 Firebase 通知提示