Reputation: 1092
In some cases my iOS application has to trigger multiple UILocalNotification
at the same time. I want to decide which UILocalNotification
the user clicked. When a user is clicking on a UILocalNotification
the application was inactive or in the background. The problem is that the method
func application(application: UIApplication, didReceiveLocalNotification notification: UILocalNotification) {
is called for each triggered UILocalNotification
. So when the app becomes active this method is called multiple times since I received multiple UILocalNotification
's. Is there a way to determine which UILocalNotification
was the cause for the app to be opened? A check of applicationState is not working since all UILocalNotification
's have been received when the application was inactive or in the background.
Thanks a lot!
Edit: As an far example: When you receive a WhatsApp message from two different groups A and B and select push notification from group A this one will immediately displayed after the application opens itself. The difference between WhatsApp and my use case is that I have local notifications.
Upvotes: 15
Views: 2029
Reputation: 680
In Swift 3.x use the following code in AppDelegate's didFinishLaunchingWithOptions
method.
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// See if the application was launched from a local notification.
if let options = launchOptions {
if let notification = options[UIApplicationLaunchOptionsKey.localNotification] as? UILocalNotification {
print(notification.userInfo ?? "No info attached.")
}
}
return true
}
Upvotes: 0
Reputation: 7245
Since you use swift, I understand your app is probably running for iOS 8 and later.
If you use iOS8, you can provide actions to your notification (clicking on the notification is an action itself).
So you'll have this methods triggered through the UIApplicationDelegate
:
application(_:handleActionWithIdentifier:forLocalNotification:completionHandler:)
And
application(_:handleActionWithIdentifier:forLocalNotification:withResponseInfo:completionHandler:)
Both this methods give you a UILocalNotification
which contains a userInfo
property that you can fill when you create you notification and then put some kind of identifier for you to know which one is which.
Upvotes: 2
Reputation: 1776
Provide some unique information like id or something in the UILocalNotification
userInfo property as NSDictionary
when scheduling your notification. And when receive it either in didFinishLaunchingWithOptions
or didReceiveLocalNotification
take out the user info
dictionary from notification instance and do your work accordingly.
Upvotes: 2
Reputation: 271
While sheduling the notification you can set the some unique id for notification userinfo.
UILocalNotification *notif = [[UILocalNotification alloc] init];
notif.fireDate = [NSDate dateWithTimeIntervalSinceNow:10];
notif.timeZone = [NSTimeZone defaultTimeZone];
// set the your data with unique id
NSMutableDictionary *dict=[NSMutableDictionary new];
[dict setObject:Id forKey:@"id"];
// assignt the dictionary to user info
notif.userInfo=dict;
notif.alertBody = @"test Notification";
notif.soundName = UILocalNotificationDefaultSoundName;
[[UIApplication sharedApplication] scheduleLocalNotification:notif];
you can get the userinfo from didReceiveLocalNotification like that
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
if ([[notification.userInfo valueForKey:@"id"] isEqualToString:@"1"])
{
NSLog(@"notification id %@",[notification.userInfo valueForKey:@"id"]);
}
else if ([[notification.userInfo valueForKey:@"id"] isEqualToString:@"2"])
{
NSLog(@"notification id %@",[notification.userInfo valueForKey:@"id"]);
}
////// or /////
if ([notification.userInfo valueForKey:@"id"] )
{
NSLog(@"id of notification %@",[notification.userInfo valueForKey:@"id"]);
}
}
from didFinishLaunchingWithOptions
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
if ([launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey])
{
UILocalNotification *notif=[launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];
NSLog(@"notif.userInfo %@",notif.userInfo);
// notif.userInfo {
// id = 2;
// }
}
return YES;
}
Upvotes: 9
Reputation: 147
Just a little addition to @pbush25, you can assign a dictionary object to property notification.userInfo = [:]
just like that, then you can get it this way as well and use however you like!
Upvotes: 1
Reputation: 5248
You can use the launch options
to get access to the dictionary that was passed with the notification and from there, depending on what data you give the local notification when you're setting it up, you can check the dictionary and see which notification the method is responding to.
Upvotes: 2