我需要一个应用程序来创建通知,该通知应在应用程序关闭后仍然存在,另一个应用程序应在一段时间后读取它们。
我按照教程操作:https://www.youtube.com/watch?v=UqR7YinI7k4 稍后,我发现需要另一种实现方式,因为SDK版本更高。
我找到了这个答案,它说我需要使用NotificationChannel来做到这一点:Failed to post notification on channel "null" Target Api is 26 但是,我的代码仍然卡住了,这是我的代码:
编辑3: 最终,代码可以在手机上运行,并且要使其在Android电视上运行,该应用程序应该被列入系统白名单。
我按照教程操作: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());
};
当我按下按钮时,会弹出一个提示框,上面写着:
![enter image description here](https://istack.dev59.com/vQeip.webp)
不幸的是,我不知道接下来该怎么做,你能帮我吗?
感谢你的时间。
编辑1:在日志中找到了这个:
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)
编辑2:当我点击按钮后,会出现以下错误:
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电视上运行,该应用程序应该被列入系统白名单。