Simple C# iOS Notification

My device asks me if I allow notifications, but even though I click “accept”, the app still doesn’t display notifications a minute after I startup the app.

Is there a certain Xcode/Unity Player Preferences setting I have to do in order to make notifications work, or…?

Edit: I’ve turned Notifications on in Xcode, btw.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using LocalNotification = UnityEngine.iOS.LocalNotification;

public class NotificationSystem : MonoBehaviour {

    UnityEngine.iOS.LocalNotification notif = new UnityEngine.iOS.LocalNotification();
    LocalNotification newNotif;

    // Use this for initialization
    void Start () {
        if (PlayerPrefs.GetInt ("New") == 0) //0 = New
            RegisterForNotifications ();
        else
            PlayerPrefs.SetInt ("New", 1); //1 = Returning

        //Set new one 1 minute from now:
        newNotif.fireDate = System.DateTime.Now.AddMinutes(1);
        notif.alertBody = "Come back! Any time is the right time to practice your speaking skills!";
        UnityEngine.iOS.NotificationServices.ScheduleLocalNotification (newNotif);
    }

    void RegisterForNotifications () {
        UnityEngine.iOS.NotificationServices.RegisterForNotifications (UnityEngine.iOS.NotificationType.Alert |
        UnityEngine.iOS.NotificationType.Badge |
        UnityEngine.iOS.NotificationType.Sound);
    }
  

}

You have 2 LocalNotification fields: notif and newNotif. newNotif is never instantiated so presumably you get a null reference exception at line 19?

newNotif actually is instantiated at Line 9. (so no error)

Fyi: there are no errors except for 4 clear-able errors about InitWrapper and a NullRef at line 26 (fireDate)…

newNotif.fireDate = System.DateTime.Now.AddMinutes(1);

What is wrong with this ^ ?

This alone:

    LocalNotification newNotif;

Does not instantiate newNotif. It’s still null at this point.

So if you were to try to access fireDate through newNotif (which is null), you get a NullRef error.

You need to instantiate it first (just like you did with notif in the line above).

So I instantiated it like you said above, but it still gives me NullRefExcep for Line 26 (27 in this case)
Updated Code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using LocalNotification = UnityEngine.iOS.LocalNotification;
//using UnityEngine.iOS;

public class NotificationSystem : MonoBehaviour {

    UnityEngine.iOS.LocalNotification notif = new UnityEngine.iOS.LocalNotification();
    LocalNotification newNotif = new LocalNotification();

    // Use this for initialization
    void Start () {
        Screen.sleepTimeout = SleepTimeout.NeverSleep; //Stop Dimming

        if (PlayerPrefs.GetInt ("New") == 0) //0 = New
            RegisterForNotifications ();
        else
            PlayerPrefs.SetInt ("New", 1); //1 = Returning

        if (UnityEngine.iOS.NotificationServices.localNotificationCount > 0 || UnityEngine.iOS.NotificationServices.scheduledLocalNotifications.Length > 0) { //If open app & Notification Scheduled
            UnityEngine.iOS.NotificationServices.CancelAllLocalNotifications(); //Clear it
        }

        //Set new one 1 minute from now:
        newNotif.fireDate = System.DateTime.Now.AddMinutes(1);
        notif.alertBody = "Come back! Any time is the right time to practice your speaking skills!";
        UnityEngine.iOS.NotificationServices.ScheduleLocalNotification (newNotif);
    }

    void RegisterForNotifications () {
        UnityEngine.iOS.NotificationServices.RegisterForNotifications (UnityEngine.iOS.NotificationType.Alert |
        UnityEngine.iOS.NotificationType.Badge |
        UnityEngine.iOS.NotificationType.Sound);
    }
   

}

For variables that are part of the class like that, you may need to reset it (or remove and re-add) the component to make it take effect. Or, you can just initialize it in Start() and be sure.

Aha okay! So I solved the error on Line 27 by doing Initialization at Start().

However, I’m still getting this error: (Maybe it’s because I’m on Mac , when Notif is for iOS?)
3128561--236962--Screen Shot 2017-06-30 at 11.52.25 AM.png

Try moving the initialization for “notif” into Start, as well (and remove it from the class body).

That fixed all the errors! Thanks!!!