Android notifications not working. What am I doing wrong ?

Hello

as title says, I can’t get my android app to send notifications.
I followed unity’s tutorial, and tried many variations. Here’s my current code :

using System.Collections;
using System.Collections.Generic;
using UnityEngine.Android;
using UnityEngine;
using Unity.Notifications.Android;
using UnityEngine.UI;

public class MainManager : MonoBehaviour
{
    public Text dateText;

    AndroidNotificationChannel channel;

    void Start()
    {
        dateText.text = System.DateTime.UtcNow.ToLocalTime().ToString("dd / MM yyyy");

        SetupChannel();
        DoNotif();
    }

    private void SetupChannel()
    {
        channel = new AndroidNotificationChannel()
        {
            Id = "default_channel",
            Name = "Default channel",
            Description = "",
            Importance = Importance.Default,

        };

        AndroidNotificationCenter.RegisterNotificationChannel(channel);

    }

    public void DoNotif()
    {
        var notification = new AndroidNotification();
        notification.Title = "SomeTitle";
        notification.Text = "SomeText";
        notification.FireTime = System.DateTime.Now.AddSeconds(5);

        var id = AndroidNotificationCenter.SendNotification(notification, "default_channel");
    }
}

It does not work. No notification is sent. I do not understand why, since I’ve followed the steps given in the tutorial.

I have also downloaded the demo project from this link :
https://github.com/Unity-Technologies/NotificationsSamples

and tested it on my phone. Tried to compose and send a notification but it didn’t work either.

I’m using Unity 2020.1.1f1, and my phone is a Xiami redmi 5 plus, android version 8.1.0.

Thanks

Run it and see if there is anything of use being output in the device console log, such as via adb logcat

I get this error :

E/NotificationService: No Channel found for pkg=com.DefaultCompany.MyApp, channelId=default_channel, id=1164390892, tag=null, opPkg=com.DefaultCompany.MyApp, callingUid=10150, userId=0, incomingUserId=0, notificationUid=10150, notification=Notification(channel=default_channel pri=0 contentView=null vibrate=null sound=null defaults=0x0 flags=0x0 color=0x00000000 vis=PRIVATE)

And what have you found so far by googling the above error?

This is just basic debugging / troubleshooting 101… even someone who has used these APIs every single day has to do this because they do change over time and suddenly stop working. It’s important to be ready to google stuff.

1 Like

I was googling this very error and got here. Thanks for nothing, I guess.

That’s how it’s done!

using UnityEngine;
using Unity.Notifications.Android;
using System;
public class Notificationmanager : MonoBehaviour
{
    public void Start()
    {
        var c = new AndroidNotificationChannel()
        {
            Id = "notif1",
            Name = "openreminder",
            Importance = Importance.High,
            Description = "Remind the play to open the app",
        };
        AndroidNotificationCenter.RegisterNotificationChannel(c);

        SendNotification();
        //SendAnotherNotification();
    }
    void SendNotification()
    {
        var notification = new AndroidNotification();
        notification.Title = "Bla Bla Bla";
        notification.Text = "Bla Bla Bla";
        notification.FireTime = DateTime.Now.AddSeconds(10);
        notification.LargeIcon = "icon_1";
        notification.SmallIcon = "icon_0";

        AndroidNotificationCenter.SendNotification(notification, "notif1");
    }
}

Hello.
I had the same issues. Notifications on the Android emulator were received with a delay of 75% of the time, e.g. set for 2 minutes and received in 3.5 minutes.
When testing on a real Android device, the notification didn’t appear until the app was launched again.
And I’ve found the solution:

  • go to “Project settings”
  • select “Mobile notifications”
  • in “Android” tab at “Schedule at exact time” select “Everything”

1 Like

Yes this works, however you run into issues when you try to publish this app in play store. Exact notification permissions are only allowed for calendar and clock applications.

Maybe it’s related to battery optimization issues that Xiaomi and some other phones have. You could read about this in the documentation: notifications not delivered on certain phones when app is closed.