Rob*_*ndl 15 notifications ios uilocalnotification swift
在某些情况下,我的iOS应用程序触发多个UILocalNotification在同一时间.我想决定UILocalNotification用户点击了哪个.当用户点击某个UILocalNotification应用程序处于非活动状态或后台时.问题是该方法
func application(application: UIApplication, didReceiveLocalNotification notification: UILocalNotification) {
Run Code Online (Sandbox Code Playgroud)
每次触发都会UILocalNotification被调用.因此,当应用程序变为活动状态时,此方法会被多次调用,因为我收到了多个UILocalNotification.有没有办法确定UILocalNotification应用程序打开的原因是什么?由于UILocalNotification应用程序处于非活动状态或后台时已收到所有应用程序,因此检查applicationState 不起作用.
非常感谢!
编辑:作为一个例子:当您从两个不同的组A和B收到WhatsApp消息并从组A中选择推送通知时,该应用程序将在应用程序打开后立即显示.WhatsApp和我的用例之间的区别在于我有本地通知.
在安排通知时,您可以为通知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];
Run Code Online (Sandbox Code Playgroud)
你可以从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"]);
}
}
Run Code Online (Sandbox Code Playgroud)
来自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;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1714 次 |
| 最近记录: |