ios – 从哪个UILocalNotification中检测到一个应用程序被打开

ios – 从哪个UILocalNotification中检测到一个应用程序被打开,第1张

概述在某些情况下,我的iOS应用程序必须同时触发多个UILocalNotification.我想决定用户点击的UILocalNotification.当用户单击UILocalNotification时,该应用程序处于非活动状态或在后台.问题是该方法 func application(application: UIApplication, didReceiveLocalNotification noti 在某些情况下,我的iOS应用程序必须同时触发多个UIlocalnotification.我想决定用户点击的UIlocalnotification.当用户单击UIlocalnotification时,该应用程序处于非活动状态或在后台.问题是该方法
func application(application: UIApplication,dIDReceivelocalnotification notification: UIlocalnotification) {

被称为每个触发的UIlocalnotification.所以当应用程序变得活跃时,这个方法被多次调用,因为我收到了多个UIlocalnotification.有没有办法确定哪个UIlocalnotification是应用程序打开的原因?对applicationState的检查不起作用,因为当应用程序处于非活动状态或在后台时,所有UIlocalnotification都已被接收.

非常感谢!

编辑:
作为一个很好的例子:当您从两个不同的组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];

你可以通过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;}
总结

以上是内存溢出为你收集整理的ios – 从哪个UILocalNotification中检测到一个应用程序被打开全部内容,希望文章能够帮你解决ios – 从哪个UILocalNotification中检测到一个应用程序被打开所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1112896.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-29
下一篇 2022-05-29

发表评论

登录后才能评论

评论列表(0条)

保存