在Android TV上创建通知渠道时无法发布通知

3
我需要一个应用程序来创建通知,该通知应在应用程序关闭后仍然存在,另一个应用程序应在一段时间后读取它们。
我按照教程操作: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

不幸的是,我不知道接下来该怎么做,你能帮我吗?

感谢你的时间。

编辑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电视上运行,该应用程序应该被列入系统白名单。

请查看日志以获取更多详细信息...那么在日志中发布了什么? - SushiHangover
请查看编辑1,我刚刚自己找到了。 - Elydasian
1个回答

1

好的,您创建了通知渠道并设置了其值,但我没有看到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);
        }

获取通知管理器:
public static NotificationManager getNotificationManager(Context context) {
        return (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    }

创建频道函数:
@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;
    }

你是对的,我在我的NotificationChannel创建下面添加了一行代码。但是,当我点击按钮时,我仍然看不到通知,也许你可以帮助我解决这个问题? - Elydasian
为什么你要设置两次 ContentIntent - SkypeDogg
我在哪里设置了两次?所以它将被跳过,没有标题和文本,但我有这两个。 - Elydasian
我的意思是这不应该是你问题的原因,因为它只是分配了值,但我仍在阅读并尝试找出是什么导致了这个问题。 - SkypeDogg
失败了,说实话看起来相当复杂。无论如何,感谢您的时间和努力。 - Elydasian
显示剩余6条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接