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