检测从UILocalNotification应用程序打开
在某些情况下,我的iOS应用程序触发多个UILocalNotification
在同时。我想决定用户点击哪个UILocalNotification
。当用户点击UILocalNotification
时,该应用程序处于非活动状态或在后台。问题是,该方法检测从UILocalNotification应用程序打开
func application(application: UIApplication, didReceiveLocalNotification notification: UILocalNotification) {
被调用每个触发UILocalNotification
。所以当应用程序变得活跃时,这个方法会被多次调用,因为我收到了多个UILocalNotification
's。有没有办法确定哪个UILocalNotification
是应用程序打开的原因?由于应用程序处于非活动状态或在后台时收到了所有UILocalNotification
,因此applicationState检查不起作用。
非常感谢!
编辑: 作为一个很好的例子:当您收到来自两个不同的组A和B的WhatsApp消息,并从组A中选择推送通知时,在应用程序自行打开后会立即显示此消息。 WhatsApp和我的用例之间的区别是我有本地通知。
您可以使用launch options
访问随通知一起传递的字典,并根据您在设置本地通知时提供的本地通知的数据,检查字典并查看哪些该方法正在响应通知。
嗨pbush25,谢谢你的回应。不幸的是,这并不能帮助我找出用户自从应用程序点击了哪个UILocalNotification:didReceiveLocalNotification被用户同时接收到的每个UILocalNotification。 –
此外,启动选项仅在应用程序第一次启动而不是从后台启动时发送。 –
除了@ pbush25之外,你可以像这样分配一个字典对象到属性notification.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];
你可以从didReceiveLocalNotification获得用户信息这样的
- (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"]);
}
}
从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;
}
调度时提供像在UILocalNotification
USERINFO属性ID或东西一些独特的信息NSDictionary
您通知。当收到它在didFinishLaunchingWithOptions
或didReceiveLocalNotification
从通知实例中取出user info
字典并相应地做你的工作。
既然您使用了swift,我知道您的应用程序可能运行于iOS 8及更高版本。
如果您使用iOS8,则可以为您的通知提供操作(单击通知本身就是操作)。
所以你也会有这样的方法,通过UIApplicationDelegate
触发:
application(_:handleActionWithIdentifier:forLocalNotification:completionHandler:)
而且
application(_:handleActionWithIdentifier:forLocalNotification:withResponseInfo:completionHandler:)
这两种方法给你一个UILocalNotification
这contains a userInfo
property,你可以填写你的时候创建你的通知,然后给你一些标识符,以便知道哪一个是哪个。
在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
}
根据我的经验(我只是写了一个小的测试应用程序,日程安排3个UILocalNotifications与当应用程序被关闭,以测试这同样fireDate),当用户点击他们的通知画面指定警报,从而启动应用程序,传递给UIApplication的didReceiveNotifications方法的唯一UILocalNotification是用户点击的方法。所以我不太了解你的问题。 – vaticRite
它不回答你的问题,但是只有一个本地通知(即取消以前的通知)。如果这样做,则可以跟踪通知的userInfo字典,以便在应用程序变为活动状态时以后使用。如果你仍然需要多个本地通知,是否可以将它分组成一个单一的选项?如果是,您可以准备一些用户信息字典数组,以便在应用程序变为活动状态时使用。 – atxe
不能使用用户信息字典找出哪些通知已打开,并检查didRecieveLocalNotif, –