我正在尝试使用标准的Android API显示本地通知:
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 Android 8.0 目标SDK 8.1(奥利奥)
我是否会错过任何要在清单中添加的内容,或者是否有关于新通知渠道的内容?
答案 0 :(得分: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"));