我需要一个应用程序来创建通知,它应该在应用程序关闭后存在,因为另一个应用程序应该在一段时间后读取它们。
我遵循了一个教程:https://www.youtube.com/watch?v=UqR7YinI7k4
后来发现,由于更高的 SDK 版本,我需要另一个实现。
我找到了这个答案,其中指出我需要使用 NotificationChannel 来执行此操作:
Failed to post notification on channel "null" Target Api is 26
但是,我还是卡住了,这是我的代码:
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
Xamarin.Essentials.Platform.Init(this, savedInstanceState);
// Set our view from the "main" layout resource
SetContentView(Resource.Layout.activity_main);
var btnSend = FindViewById<Button>(Resource.Id.btnSend);
btnSend.Click += (s, e) =>
{
Bundle valueSend = new Bundle();
valueSend.PutString("sendContent", "STF content");
Intent intent = new Intent(this, typeof(SecondActivity));
intent.PutExtras(valueSend);
int NOTIFICATION_ID = 234;
NotificationManager notificationManager = (NotificationManager)this.GetSystemService(Context.NotificationService);
string CHANNEL_ID = "my_channel_01";
string name = "my_channel";
string Description = "This is my channel";
NotificationChannel mChannel = new NotificationChannel(CHANNEL_ID, name, NotificationManager.ImportanceHigh);
mChannel.Description = Description;
mChannel.EnableLights(true);
Android.Support.V4.App.TaskStackBuilder stackBuilder = Android.Support.V4.App.TaskStackBuilder.Create(this);
stackBuilder.AddParentStack(Java.Lang.Class.FromType(typeof(SecondActivity)));
stackBuilder.AddNextIntent(intent);
PendingIntent resultPendingIntent = stackBuilder.GetPendingIntent(0, (int)PendingIntentFlags.UpdateCurrent);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_ID)
.SetAutoCancel(true) // Dismiss the notification from the notification area when the user clicks on it
.SetContentIntent(resultPendingIntent) // Start up this activity when the user clicks the intent.
.SetContentTitle("Notifications") // Set the title
.SetSmallIcon(Resource.Drawable.navigation_empty_icon)
.SetContentText("STF Content text"); // the message to display.
notificationManager.Notify(NOTIFICATION_ID, builder.Build());
};
不幸的是,我不知道下一步该怎么做,你能帮帮我吗?
感谢您的宝贵时间。
Edit1:在日志中找到这个:
E NotificationService: No Channel found for
pkg=com.companyname.notifications_app, channelId=my_channel_01, id=234,
tag=null, opPkg=com.companyname.notifications_app, callingUid=10069,
userId=0, incomingUserId=0, notificationUid=10069,
notification=Notification(channel=my_channel_01 pri=0 contentView=null
vibrate=null sound=null defaults=0x0 flags=0x10 color=0x00000000 vis=PRIVATE)
Edit2:按下按钮后,出现以下错误:
TVNotifService: skipped notification
StatusBarNotification(pkg=com.companyname.notifications_app
user=UserHandle{0} id=234 tag=null
key=0|com.companyname.notifications_app|234|null|10069:
Notification(channel=my_channel_01 pri=0 contentView=null vibrate=null
sound=null defaults=0x0 flags=0x10 color=0x00000000 vis=PRIVATE)) userId: 0
编辑3: 最后,代码在手机上运行,要在 Android TV 上运行,应用程序应该在系统上列入白名单。
最佳答案
好的,您创建了通知 channel ,设置了它的值,但我没有看到 notificationManager.createNotificationChannel(mChannel);
所以它就像 notificationManager 没有附加 NotificationChannel。
我们这样做:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel notificationChannel = createChannels(); // here I just create channel and return it
NotificationManager notificationManager = NotificationHelper.getNotificationManager(context); // same with NotificationManager
notificationManager.createNotificationChannel(notificationChannel); //here I "set" channel for manager
notificationManager.notify(NotificationHelper.ALARM_TYPE_RTC, repeatedNotification);
} else {
NotificationHelper.getNotificationManager(context).notify(NotificationHelper.ALARM_TYPE_RTC, repeatedNotification);
}
getNotificationManager():
public static NotificationManager getNotificationManager(Context context) {
return (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
}
创建 channel ():
@RequiresApi(api = Build.VERSION_CODES.O)
private NotificationChannel createChannels() {
NotificationChannel nChannel = new NotificationChannel(
CHANNEL_ID,
CHANNEL_NAME,
NotificationManager.IMPORTANCE_HIGH);
nChannel.setDescription("MY channel");
nChannel.enableLights(true);
nChannel.enableVibration(true);
nChannel.setLightColor(Color.MAGENTA);
nChannel.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);
return nChannel;
}
关于android - 无法在 Android TV 上创建的 channel 上发布通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62554564/