Is it possible to trigger an event when a user clicks a local notification in iOS?
I am letting the user know that it is their turn in a turn-based game, using a LocalNotification with NotificationServices. I want the user to be able to click on the notification, and have the game load the latest turn information.
Andy, thanks for the quick response. I’m having a really hard time to get my mind around it.
See, I have this snippet… I could add the dictionary to notif01 and notif02 too… ok, the data is there… but how do I get that back? I really apologize if I’m missing something obvious, but I just don’t understand it.
If I relaunch the app from the app icon, the notification is still on the notifications list, so just checking the list does not give me 100% certainty that the app was launched from the notification, right?
void OnApplicationPause( bool pauseStatus )
{
if(pauseStatus)
{
Debug.Log("got paused");
// schedule notification to be delivered in 10 seconds
LocalNotification notif01 = new LocalNotification();
notif01.fireDate = DateTime.Now.AddSeconds(5);
notif01.alertBody = "Hello 5s!";
NotificationServices.ScheduleLocalNotification(notif01);
// schedule notification to be delivered in 10 seconds
LocalNotification notif02 = new LocalNotification();
notif02.fireDate = DateTime.Now.AddSeconds(10);
notif02.alertBody = "Hello 10s!";
NotificationServices.ScheduleLocalNotification(notif02);
}
else
{
Debug.Log("got active");
if (NotificationServices.localNotificationCount > 0)
{
Debug.Log("alert body:");
Debug.Log(NotificationServices.localNotifications[0].alertBody);
NotificationServices.ClearLocalNotifications();
}
}
}
Truste me, I looked into that… btw, you helped me on an years old topic about the subject. Thanks for that
We must be precise on something: what does “receive” mean? Is it resuming the application and checking for Notifications.localNotifications[0]? Because if it is, I tested that, and there is really no difference if I either open the app from the notification, or form the icon. Specially if I have several notifications scheduled and they are not necessarily ordered in any manner, right?
“Receive” is also a word that I don’t get… I could receive a callback or something, but all that I can do is testing the scheduled notifications and see what notification is in each position on the array. It does not give me certainty that it was a specific notification that lead the user to the app though…
I do apologize, I feel I’m missing something very simple… I just don’t see the difference of getting in the app through the notification, or through the app icon.
When I say receive I’m just talking about a notification in the Notifications.localNotifications array.
I have a test app for local notifications and I just tried scheduling a few. If a notification arrives while the app is open then there’s nothing visual, only the size of the array increases by 1. If the app is closed and you click the notification then the app opens and again the size of the array increases by 1. If the app is closed and you don’t click the notification then the size of the array does not increase when you open the app.
If you clear all received before leaving the app and cancel any scheduled when returning to the app then when the user interacts with a notification and your app opens the only one you can query will be that notification I think.
Andy, brilliant. I re-created a simple app here myself, and it seems to work fine now… I might have overlooked the part of clearing and cancelling pending notifications…