Java Android通知。Notify不做任何事情
我正在尝试使用标准android API显示本地通知:Java Android通知。Notify不做任何事情,java,android,xamarin,notifications,xamarin.android,Java,Android,Xamarin,Notifications,Xamarin.android,我正在尝试使用标准android API显示本地通知: var message = intent.GetStringExtra("message"); var title = intent.GetStringExtra("title"); var notIntent = new Intent(context, typeof(MainActivity)); var contentIntent = PendingIntent.GetActivity(
var message = intent.GetStringExtra("message");
var title = intent.GetStringExtra("title");
var notIntent = new Intent(context, typeof(MainActivity));
var contentIntent = PendingIntent.GetActivity(context, 0, notIntent, PendingIntentFlags.CancelCurrent);
var manager = NotificationManagerCompat.From(context);
var style = new NotificationCompat.BigTextStyle();
style.BigText(message);
int resourceId = Resource.Drawable.ic_launcher;
var wearableExtender = new NotificationCompat.WearableExtender()
.SetBackground(BitmapFactory.DecodeResource(context.Resources, resourceId));
//Generate a notification with just short text and small icon
var builder = new NotificationCompat.Builder(context)
.SetContentIntent(contentIntent)
.SetSmallIcon(Resource.Drawable.ic_launcher)
.SetContentTitle(title)
.SetContentText(message)
.SetStyle(style)
.SetWhen(Java.Lang.JavaSystem.CurrentTimeMillis())
.SetAutoCancel(true)
.Extend(wearableExtender);
var notification = builder.Build();
manager.Notify(0, notification);
我尝试了不同版本的通知创建,但都没有做任何事情。这就好像从来没有人叫过它,但它确实被人叫过。
我正在测试:
三星Galaxy s8
安卓8.0
目标SDK 8.1奥利奥
我是否遗漏了要添加到清单中的任何内容,或者是关于新通知通道的内容?您应该使用Oreo+设备API-26+上的通道 比如:
Notification ForegroundNotification(string title, string message)
{
using (var notificationManager = NotificationManager.FromContext(ApplicationContext))
{
var notificationBuilder = new Notification.Builder(ApplicationContext)
.SetContentTitle(title)
.SetContentText(message)
.SetSmallIcon(Resource.Drawable.ic_stat_notification_network_locked)
.SetContentIntent(pendingIntent);
if (Build.VERSION.SdkInt >= BuildVersionCodes.O)
{
NotificationChannel channel;
var channelName = ApplicationContext.PackageName;
channel = notificationManager.GetNotificationChannel(channelName);
if (channel == null)
{
channel = new NotificationChannel(channelName, channelName, NotificationImportance.Default)
{
LockscreenVisibility = NotificationVisibility.Public
};
notificationManager.CreateNotificationChannel(channel);
}
channel.Dispose();
notificationBuilder = notificationBuilder
.SetChannelId(channelName);
}
return notificationBuilder.Build();
}
}
因此,您的NotificationManager.Notify可能成为:
manager.Notify(0, ForegroundNotification("StackOverflow", "Totally Rocks"));
谢谢你,成功了!为什么他们不能为根本不想使用默认频道的开发人员自动创建一个默认频道。。。