Failed to post notification on created channel on Android TV

3

I need an app to create Notifications, which should be there after the app is closed, for another app should read them after some time passes.

I followed a tutorial: https://www.youtube.com/watch?v=UqR7YinI7k4

And later, found out, that I need another implementation, due to higher SDK version.

I came to find this answer, which states, that I need to use a NotificationChannel, to do this:

Failed to post notification on channel "null" Target Api is 26

But, I still got stuck, here is my code:

 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());

    };

Once, I press the button, a toast pops up, which states: enter image description here

And, unfortunately, I have no idea what to do next, can you help me?

Thank you for your time.

Edit1: Found this in log:

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: Once I press the button, the following error shows up:

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

Edit3: In the end, the code is working on mobile phones, and for it to be working on Android TVs, the app should be whitelisted on the system.

android
xamarin
notifications
asked on Stack Overflow Jun 24, 2020 by Elydasian • edited Jun 25, 2020 by Elydasian

1 Answer

1

Ok, you create notification channel, set its values, but I don't see notificationManager.createNotificationChannel(mChannel);
So it's like notificationManager doesn't have NotificationChannel attached.

We do it like this:

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);
    }

createChannels():

@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;
    }
answered on Stack Overflow Jun 24, 2020 by SkypeDogg • edited Jun 24, 2020 by SkypeDogg

User contributions licensed under CC BY-SA 3.0