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
在某些情况下,我的iOS应用程序必须同时触发多个UILocalNotification。我想决定用户点击哪个UILocalNotification。当用户单击UILocalNotification时,应用程序处于非活动状态或处于后台状态。问题是该方法
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.
为每个触发的UILocalNotification调用。因此,当应用程序变为活动状态时,由于我收到了多个UILocalNotification,因此多次调用此方法。有没有办法确定哪个UILocalNotification是应用程序打开的原因?由于在应用程序处于非活动状态或后台时已收到所有UILocalNotification,因此对applicationState的检查无效。
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.
编辑:作为一个例子:当您从两个不同的组A和B收到WhatsApp消息并从组A中选择推送通知时,该应用程序将在应用程序打开后立即显示。 WhatsApp和我的用例之间的区别在于我有本地通知。
6 个解决方案
#1
8
While sheduling the notification you can set the some unique id for notification userinfo.
在安排通知时,您可以为通知userinfo设置一些唯一ID。
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
你可以从didReceiveLocalNotification那样获取userinfo
- (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
来自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;
}
#2
2
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.
您可以使用启动选项来访问随通知传递的字典,并从那里开始,根据您在设置时提供本地通知的数据,您可以检查字典并查看方法的通知正在回应。
#3
2
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.
在安排通知时,在UILocalNotification userInfo属性中提供一些唯一的信息,如id或其他东西,如NSDictionary。当在didFinishLaunchingWithOptions或didReceiveLocalNotification中接收它时,从通知实例中取出用户信息字典并相应地完成工作。
#4
2
Since you use swift, I understand your app is probably running for iOS 8 and later.
由于您使用swift,我了解您的应用程序可能正在运行iOS 8及更高版本。
If you use iOS8, you can provide actions to your notification (clicking on the notification is an action itself).
如果您使用iOS8,则可以对通知提供操作(单击通知本身就是一个操作)。
So you'll have this methods triggered through the UIApplicationDelegate
:
所以你将通过UIApplicationDelegate触发这些方法:
application(_:handleActionWithIdentifier:forLocalNotification:completionHandler:)
应用程序(_:handleActionWithIdentifier:forLocalNotification:completionHandler :)
And
和
application(_:handleActionWithIdentifier:forLocalNotification:withResponseInfo:completionHandler:)
应用程序(_: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.
这两种方法都为您提供了一个UILocalNotification,其中包含一个userInfo属性,您可以在创建通知时填写该属性,然后为您提供某种标识符以了解哪个属性。
#5
1
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!
只是@ pbush25的一点点添加,您可以将属性notification.userInfo = [:]的字典对象分配给那样,那么您也可以这样使用它,然后使用您喜欢的!
#6
0
In Swift 3.x use the following code in AppDelegate's didFinishLaunchingWithOptions
method.
在Swift 3.x中,在AppDelegate的didFinishLaunchingWithOptions方法中使用以下代码。
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
}
#1
8
While sheduling the notification you can set the some unique id for notification userinfo.
在安排通知时,您可以为通知userinfo设置一些唯一ID。
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
你可以从didReceiveLocalNotification那样获取userinfo
- (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
来自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;
}
#2
2
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.
您可以使用启动选项来访问随通知传递的字典,并从那里开始,根据您在设置时提供本地通知的数据,您可以检查字典并查看方法的通知正在回应。
#3
2
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.
在安排通知时,在UILocalNotification userInfo属性中提供一些唯一的信息,如id或其他东西,如NSDictionary。当在didFinishLaunchingWithOptions或didReceiveLocalNotification中接收它时,从通知实例中取出用户信息字典并相应地完成工作。
#4
2
Since you use swift, I understand your app is probably running for iOS 8 and later.
由于您使用swift,我了解您的应用程序可能正在运行iOS 8及更高版本。
If you use iOS8, you can provide actions to your notification (clicking on the notification is an action itself).
如果您使用iOS8,则可以对通知提供操作(单击通知本身就是一个操作)。
So you'll have this methods triggered through the UIApplicationDelegate
:
所以你将通过UIApplicationDelegate触发这些方法:
application(_:handleActionWithIdentifier:forLocalNotification:completionHandler:)
应用程序(_:handleActionWithIdentifier:forLocalNotification:completionHandler :)
And
和
application(_:handleActionWithIdentifier:forLocalNotification:withResponseInfo:completionHandler:)
应用程序(_: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.
这两种方法都为您提供了一个UILocalNotification,其中包含一个userInfo属性,您可以在创建通知时填写该属性,然后为您提供某种标识符以了解哪个属性。
#5
1
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!
只是@ pbush25的一点点添加,您可以将属性notification.userInfo = [:]的字典对象分配给那样,那么您也可以这样使用它,然后使用您喜欢的!
#6
0
In Swift 3.x use the following code in AppDelegate's didFinishLaunchingWithOptions
method.
在Swift 3.x中,在AppDelegate的didFinishLaunchingWithOptions方法中使用以下代码。
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
}