iPhone - NotificationServices Not Working?

Hello,

I’m pretty new to developing on the iPhone, and I’m trying to get the alert message to popup using the ‘NotificationServices’. But it just doesn’t seem to be doing anything?

function Start(){

	var notif = new LocalNotification();
	notif.alertBody = "Hello!";
	NotificationServices.PresentLocalNotificationNow(notif);
}


function Update(){

    if (NotificationServices.localNotificationCount > 0) {
        Debug.Log(NotificationServices.localNotifications[0].alertBody);
        NotificationServices.ClearLocalNotifications();
    }
}

Nothing pops up at all, nothing! Although, within the xcode debugger, I do see the ‘Hello!’ message:

Loading /var/mobile/Applications/109EED84-4427-4361-A38A-0FE9FE7A9712/sandboxiSO.app/Data/Managed/Assembly-UnityScript.dll into Unity Child Domain
- Completed reload, in  0.050 seconds
-> applicationDidBecomeActive()
-> force accelerometer registration
Hello!
UnityEngine.Debug:Internal_Log(Int32, String, Object)
UnityEngine.Debug:Log(Object)
SceneController:Update()
 
(Filename: /Applications/buildAgent/work/812c4f5049264fad/Runtime/ExportGenerated/iPhonePlayer-armv7/UnityEngineDebug.cpp Line: 43)

Any reason why this isn’t working? Thanks

I think you’re doing it wrong.

The main flow is:

  1. Create a LocalNotification instance.
  2. Set the parameters of that instance, most notably the fireDate must be set at the point in future time when you wish the notification to be fired. I guess you can set it to “now” or past time of course, and the local notification would be fired “immediately”.
  3. Schedule the instance. This is something you haven’t done in your code. Looks like you should change NotificationServices.PresentLocalNotificationNow(notif) to NotificationServices.ScheduleLocalNotification(notif) really.
  4. I’m not sure here but your update looks fine, however you will probably need to make sure that you add this script to a persistent GameObject on some scene. I’m fairly new to Unity itself; I have 4-5 years experience from iOS local notifications however.

Some code example for scheduling:

	LocalNotification notification = new LocalNotification();
	notification.fireDate = datetimeWhenyouwantittofire; // System.Datetime
	notification.alertBody = "This is my message to you!";
	NotificationServices.ScheduleLocalNotification(notification);

call RegisterForNotifications