Unity Android Notifications NullReference Exception

Hi there! I’m facing a really weird issue that I can’t seem to solve at all.I’m currently trying to implement notifications for my game, and after testing it out with only one of them, I am getting the following error :

NullReferenceException: Object reference not set to an instance of an object
Unity.Notifications.Android.AndroidNotificationCenter.CheckScheduledNotificationStatus (System.Int32 id) (at Library/PackageCache/com.unity.mobile.notifications@1.0.4-preview.5/Runtime/Android/AndroidNotificationCenter.cs:896)
NotificationManager.Start () (at Assets/Scripts/Manager/NotificationManager.cs:41)

This is the code that I’m using :

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

public class NotificationManager : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
        var default_channel = new AndroidNotificationChannel() // Used for push notifications, events, discounts etc
        {
            Id = "default_channel_id",
            Name = "Default Channel",
            Importance = Importance.Default,
            Description = "Used for normal notifications",
        };
        AndroidNotificationCenter.RegisterNotificationChannel(default_channel);

        var reminder_channel = new AndroidNotificationChannel() // Used for reminders like daily reward or checking back to play
        {
            Id = "reminder_channel_id",
            Name = "Reminder Channel",
            Importance = Importance.High,
            Description = "Notifications to remind the player",
        };
        AndroidNotificationCenter.RegisterNotificationChannel(reminder_channel);

        #region Daily Rewards

        var DailyReward_Reminder = new AndroidNotification();
        DailyReward_Reminder.Title = "Daily Reward";
        DailyReward_Reminder.Text = "Come and claim your Daily Reward!";
        DailyReward_Reminder.FireTime = DateTime.Now.AddSeconds(120);
        DailyReward_Reminder.RepeatInterval = TimeSpan.FromMinutes(2);
        Debug.Log("Default Daily Reward Reminder Notification Sent");

        var DailyReward_ID = AndroidNotificationCenter.SendNotification(DailyReward_Reminder, "reminder_channel_id");

        var DailyReward_notificationStatus = AndroidNotificationCenter.CheckScheduledNotificationStatus(DailyReward_ID);


        Debug.Log("Starting Daily Reward Reminder Notification Checks..");
        if (DailyReward_notificationStatus == NotificationStatus.Scheduled)
        {
            // Replace the scheduled notification with a new notification.
            AndroidNotificationCenter.UpdateScheduledNotification(DailyReward_ID, DailyReward_Reminder, "reminder_channel_id");
            Debug.Log("Daily Reward Notification scheduled, rescheduling..");
        }
        else if (DailyReward_notificationStatus == NotificationStatus.Delivered)
        {
            // Remove the previously shown notification from the status bar.
            AndroidNotificationCenter.CancelNotification(DailyReward_ID);
            Debug.Log("Daily Reward Notification already delivered, removing the notification..");
        }
        else if (DailyReward_notificationStatus == NotificationStatus.Unknown)
        {
            // Resend the notification if not scheduled or delivered
            AndroidNotificationCenter.SendNotification(DailyReward_Reminder, "reminder_channel_id");
            Debug.Log("Daily Reward Notification Status Unknown, resending the notification..");
        }

        #endregion
    }

}

Is there anything wrong that I can’t seem to catch, or what’s happening over here?
I used the Unity documentation for an example, and they did it exactly like this.
I tried both the normal ( 1.0.3 ) and preview packages ( 1.0.4 preview-5 ).

Can anyone help me solve this? Due to this issue I’m currently stuck with my notifications system progress.

Thanks for the help! :slight_smile:

Use some Debug.Log()'s. Check what about line 41 is null.

1 Like

Well I did just that and it seems that the ID might be assigned wrongly I guess? I’m not sure if the ID of a notification can be smaller than 0. The Debug.Log shows the ID as being -1.

Here :

        var DailyReward_ID = AndroidNotificationCenter.SendNotification(DailyReward_Reminder, "reminder_channel_id");
        Debug.Log(DailyReward_ID + " Notification ID");

        var DailyReward_notificationStatus = AndroidNotificationCenter.CheckScheduledNotificationStatus(DailyReward_ID);

        Debug.Log(DailyReward_notificationStatus + " Notification Status");

The 2nd Debug.Log doesn’t show up because it throws the error at the line before ( aka line 41 ).

The ID technically isn’t null if it can be negative.

Edit : I manually assigned a notification ID ( 10000 ) to the notification as it’s apparently possible this way as well, and it’s still not working at that line and throwing the exact same error.Is this Unity being faulty about that function inside their namespace?

I doubt anybody here is either, since that’s not Unity so much as it is Android.

Have you familiarized yourself with that portion of this particular API? I know when interoperating with a closed ecosystem like iOS or Android, there are often many different requirements to get something like this to work, such as authorization keys, entitlements, manifest permissions, etc. All of that should be clearly laid out in the docs for you to check over. Generally if you miss any one of these parts, things will just not work.

Also, you might have more luck asking platform specific questions like this over in the Android platform group, as they are more likely to be in Android-specific stuff.

As for the null reference itself, that SHOULD be fixable by you.

Some notes on how to fix a NullReferenceException error in Unity3D

  • also known as: Unassigned Reference Exception
  • also known as: Missing Reference Exception

http://plbm.com/?p=221

The basic steps outlined above are:

  • Identify what is null
  • Identify why it is null
  • Fix that.

Expect to see this error a LOT. It’s easily the most common thing to do when working. Learn how to fix it rapidly. It’s easy. See the above link for more tips.

This is the kind of mindset and thinking process you need to bring to this problem:

https://discussions.unity.com/t/814091/4

Step by step, break it down, find the problem.

1 Like

I usually fix the problems by myself, but right now I don’t understand what’s wrong, it’s literally their example copy-pasted but with the variable names changed.What reference is it missing? Everything is referenced properly. Are you sure this is fixable by me in this case?

I am not really familiarized with the notifications package provided by Unity, but how I did it seems the way to do this.

That’s why I headed to the forums, I can’t seem to be able to identify how that is null when it’s clearly declared in the code and referenced properly.

Also, currently there is no other scripts communicating/modifying this one so it can’t be caused by other scripts.

Gotcha… well, there may be other non-code requirements, such as permissions or entitlements, but I’m just not directly currently familiar with it.

I definitely agree with your assessment that ID being -1 as a result of line 39 is likely your problem. I don’t know why it would be -1, but look into that call and see all the requirements for it, such as specifying that channel in Google Play, getting entitlements, etc.

Sorry I can’t offer more insight than this, but anytime you reach outside of the basic “unity cross platform stuff,” things like this get ticklish. Hopefully someone else can chime in here, or else I certainly still think it would be good to pose the question over in the android platform group.

There really isn’t , I did one more horror game a while ago at somewhat the beginning of 2020, ( you also answered me some questions back then, thanks! :slight_smile: ) and the notifications worked just like this.

I will be sure to post this inside the android platform group as well.Maybe someone over there can help me out with this weird issue that doesn’t seem to be based on my input but actually being an issue inside the package as it points me inside the AndroidNotificationCenter and it’s namespace.

Maybe it might be the case that someone from Unity needs to look into this, but we’ll see.

Thank you for the help anyways!

One more question though, kinda off-topic but I see that you are way more experienced than me.Are you aware of any coding standards that would be good to be followed or anything about automated analysis?

Boy, that’s a huge open-ended discussion! And it varies a lot from application to application. If I’m doing a one-hour GameJam with some friends, I am going to code one style and approach. If I am working on a AAA game where I am contributing only a small subsystem or fixing only a small problem, I’ll take another approach.

About the most useful thing I can suggest is to be able to read and do things many different ways. If you’re going to do software engineering professionally, there will come a day when you have to work in someone else’s codebase, so it’s always good to be able to quickly understand how they did stuff, why they chose their ways, the strengths and weaknesses of each approach, etc. And over time you will be able to apply those same constraints to your own code.

So ask lotsa questions, and more importantly be good at creating experiments (in the code) to figure out what is going wrong. Such as with your specific problem above, go back to the early 2020 game you had this working in and see what all you might have done, then look for other examples of the above API being used. There must be at least a few examples posted out there, and try to understand how they differ. It might offer a clue as to why yours is failing.

1 Like

With interactive software, especially with Unity, automated analysis has very little value in my book. When I am fixing bugs in our codebase, 95% of it is not the code but rather how the scene was dragged together, or a missing or incorrect reference, etc., or a piece of UI slightly overlapping a button when played on a certain size or shape screen. Devising meaningful automated analysis for that is pretty impossible.

1 Like

I looked at it already, and it’s exactly the same code as the one from the documentation.I don’t know why it’s not working anymore right now.I’m currently trying to apply the Continuous Code Inspection method that I read about a few weeks ago while I’m coding, and so far it has worked and I almost never have any reference or other kinds of issues when I move on to a new system or script.

Honestly since I started using unity back then, I made 2-3 projects that I scrapped because of bad ideas, but now things got way easier, in terms of identifying problems and fixing them.Thanks for the help in the early days haha

I’m also looking towards a code style that prefers scalability as I’m starting to go towards slightly more complicated games with daily rewards, skin systems, level systems, randomized generation and other things like the one I’m currently working on.

1 Like

Has anyone found a fix for this issue? I’m having the same problem.