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.

Elydasian
  • 2,016
  • 5
  • 23
  • 41

1 Answers1

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;
    }
SkypeDogg
  • 1,000
  • 2
  • 13
  • 28
  • you were right, I added the line bellow mine NotificationChannel creation. Still, I dont see a notification on button click, mby you can help me with that? – Elydasian Jun 24 '20 at 12:15
  • That's interesting. It would be probably skipped without `contentTitle` and `contentText` attached. Now I'm not sure what to say. I mean skipped by skipped, not sent blank. Empty notifications are used usually to send data, so there's no need to display them. – SkypeDogg Jun 24 '20 at 12:34
  • Why you set `ContentIntent` twice? – SkypeDogg Jun 24 '20 at 12:37
  • Where did I set it twice? So it would be skipped without title and text, but i have those 2 – Elydasian Jun 24 '20 at 12:38
  • second method called is `SetContentIntent()` and then after all this you use `builder.setContentIntent()` – SkypeDogg Jun 24 '20 at 12:39
  • Removed it, but still skipping – Elydasian Jun 24 '20 at 12:41
  • I mean this shouldn't be the cause of your problem because it's just assigning the value, but I'm still reading this and trying to find what is causing this. – SkypeDogg Jun 24 '20 at 12:41
  • can i help you somehow? I found this https://medium.com/@igor.stebliy/android-tv-application-notifications-on-o-c6820ada2086 – Elydasian Jun 24 '20 at 12:42
  • Do I need to mention, that I am working on a Android TV? Or is that irelevant? – Elydasian Jun 24 '20 at 12:47
  • It looks like it is actually important. I've never been working with Android TV so this is new for me too. It seems you' re gonna have to follow the tutorial you've posted to achieve your goal. There is mentioned that TV with Android on board has special configurations for allowing notifications – SkypeDogg Jun 24 '20 at 12:51
  • fail, it looks rather complicated to be honest. Anyway, thank you for your time and effort – Elydasian Jun 24 '20 at 12:52