It may be a question in advance but I wonder what to use instead of UILocalNotification in iOS 10. I am working on an app which has deployment target iOS 8 so will it be ok to use UILocalNotification?

解决方案

Yes, you can use UILocalNotification, old APIs also works fine with iOS 10, but we had better use the APIs in the User Notifications framework instead. There are also some new features, you can only use with iOS 10 User Notifications framework.

This also happens to Remote Notification, for more information: Here.

New Features:

  • Now you can either present alert, sound or increase badge while the app is in foreground too with iOS 10
  • Now you can handle all event in one place when user tapped (or slided) the action button, even while the app has already been killed.
  • Support 3D touch instead of sliding gesture.
  • Now you can remove specific local notifications with just one line of code.
  • Support Rich Notification with custom UI.

It is really easy for us to convert UILocalNotification APIs to iOS 10 User Notifications framework APIs, they are really similar.

I wrote a demo here to show how to use new and old APIs at the same time:  iOS 10 Adaptation Tips .

For example,

With Swift implementation:

  1. import UserNotifications

    ///    Notification become independent from UIKit
    import UserNotifications
    
  2. request authorization for localNotification

        let center = UNUserNotificationCenter.current()center.requestAuthorization(options: [.alert, .sound]) { (granted, error) in// Enable or disable features based on authorization.}
    
  3. schedule localNotification

  4. update application icon badge number

    @IBAction  func triggerNotification(){let content = UNMutableNotificationContent()content.title = NSString.localizedUserNotificationString(forKey: "Elon said:", arguments: nil)content.body = NSString.localizedUserNotificationString(forKey: "Hello Tom!Get up, let's play with Jerry!", arguments: nil)content.sound = UNNotificationSound.default()content.badge = UIApplication.shared().applicationIconBadgeNumber + 1;content.categoryIdentifier = "com.elonchan.localNotification"// Deliver the notification in 60 seconds.let trigger = UNTimeIntervalNotificationTrigger.init(timeInterval: 60.0, repeats: true)let request = UNNotificationRequest.init(identifier: "FiveSecond", content: content, trigger: trigger)// Schedule the notification.let center = UNUserNotificationCenter.current()center.add(request)
    }@IBAction func stopNotification(_ sender: AnyObject) {let center = UNUserNotificationCenter.current()center.removeAllPendingNotificationRequests()// or you can remove specifical notification:// center.removePendingNotificationRequests(withIdentifiers: ["FiveSecond"])
    }
    

Objective-C implementation:

  1. import UserNotifications

    // Notifications are independent from UIKit
    #import <UserNotifications/UserNotifications.h>
    
  2. request authorization for localNotification

    UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
    [center requestAuthorizationWithOptions:(UNAuthorizationOptionBadge | UNAuthorizationOptionSound | UNAuthorizationOptionAlert)completionHandler:^(BOOL granted, NSError * _Nullable error) {if (!error) {NSLog(@"request authorization succeeded!");[self showAlert];}}];
    
  3. schedule localNotification

  4. update application icon badge number

    UNMutableNotificationContent *content = [[UNMutableNotificationContent alloc] init];
    content.title = [NSString localizedUserNotificationStringForKey:@"Elon said:"arguments:nil];
    content.body = [NSString localizedUserNotificationStringForKey:@"Hello Tom!Get up, let's play with Jerry!"arguments:nil];
    content.sound = [UNNotificationSound defaultSound];// 4. update application icon badge number
    content.badge = [NSNumber numberWithInteger:([UIApplication sharedApplication].applicationIconBadgeNumber + 1)];
    // Deliver the notification in five seconds.
    UNTimeIntervalNotificationTrigger *trigger = [UNTimeIntervalNotificationTriggertriggerWithTimeInterval:5.frepeats:NO];
    UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:@"FiveSecond"content:contenttrigger:trigger];
    /// 3. schedule localNotification
    UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
    [center addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {if (!error) {NSLog(@"add NotificationRequest succeeded!");}
    }];
    

updated

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'time interval must be at least 60 if repeating'

let trigger = UNTimeIntervalNotificationTrigger.init(timeInterval: 60, repeats: true)
  • 机译

这可能是一个预先提出的问题,但我想知道在iOS 10中使用什么代替UILocalNotification.我正在开发具有部署目标iOS 8的应用程序,因此可以使用UILocalNotification吗?

解决方案

是的,您可以使用UILocalNotification,旧的API也可以在iOS 10上正常工作,但是我们最好在用户通知框架中使用这些API.还有一些新功能,您只能与iOS 10用户通知框架一起使用.

"远程通知"也会发生这种情况,以获取更多信息:此处.

新功能:

  • 现在,在iOS 10中,您也可以在应用程序处于前台状态时显示警报,声音或增加徽章
  • 现在,即使用户已经杀死应用程序,只要轻按(或滑动)操作按钮,您就可以在一个地方处理所有事件.
  • 支持3D触摸而不是滑动手势.
  • 现在您只需一行代码即可删除特定的本地通知.
  • 通过自定义UI支持Rich Notification.

我们很容易将UILocalNotification API转换为iOS 10 用户通知框架API,它们确实很相似.

我在此处编写了一个演示,以演示如何同时使用新旧API: iOS 10适应技巧 .

例如,

使用Swift实现:

  1. 导入用户通知

    ///    Notification become independent from UIKit
    import UserNotifications
    
  2. localNotification的请求授权

        let center = UNUserNotificationCenter.current()center.requestAuthorization(options: [.alert, .sound]) { (granted, error) in// Enable or disable features based on authorization.}
    
  3. 安排localNotification

  4. 更新应用程序图标的徽章编号

    @IBAction  func triggerNotification(){let content = UNMutableNotificationContent()content.title = NSString.localizedUserNotificationString(forKey: "Elon said:", arguments: nil)content.body = NSString.localizedUserNotificationString(forKey: "Hello Tom!Get up, let's play with Jerry!", arguments: nil)content.sound = UNNotificationSound.default()content.badge = UIApplication.shared().applicationIconBadgeNumber + 1;content.categoryIdentifier = "com.elonchan.localNotification"// Deliver the notification in 60 seconds.let trigger = UNTimeIntervalNotificationTrigger.init(timeInterval: 60.0, repeats: true)let request = UNNotificationRequest.init(identifier: "FiveSecond", content: content, trigger: trigger)// Schedule the notification.let center = UNUserNotificationCenter.current()center.add(request)
    }@IBAction func stopNotification(_ sender: AnyObject) {let center = UNUserNotificationCenter.current()center.removeAllPendingNotificationRequests()// or you can remove specifical notification:// center.removePendingNotificationRequests(withIdentifiers: ["FiveSecond"])
    }
    

Objective-C实现:

  1. 导入用户通知

    // Notifications are independent from UIKit
    #import <UserNotifications/UserNotifications.h>
    
  2. localNotification的请求授权

    UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
    [center requestAuthorizationWithOptions:(UNAuthorizationOptionBadge | UNAuthorizationOptionSound | UNAuthorizationOptionAlert)completionHandler:^(BOOL granted, NSError * _Nullable error) {if (!error) {NSLog(@"request authorization succeeded!");[self showAlert];}}];
    
  3. 安排localNotification

  4. 更新应用程序图标的徽章编号

    UNMutableNotificationContent *content = [[UNMutableNotificationContent alloc] init];
    content.title = [NSString localizedUserNotificationStringForKey:@"Elon said:"arguments:nil];
    content.body = [NSString localizedUserNotificationStringForKey:@"Hello Tom!Get up, let's play with Jerry!"arguments:nil];
    content.sound = [UNNotificationSound defaultSound];// 4. update application icon badge number
    content.badge = [NSNumber numberWithInteger:([UIApplication sharedApplication].applicationIconBadgeNumber + 1)];
    // Deliver the notification in five seconds.
    UNTimeIntervalNotificationTrigger *trigger = [UNTimeIntervalNotificationTriggertriggerWithTimeInterval:5.frepeats:NO];
    UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:@"FiveSecond"content:contenttrigger:trigger];
    /// 3. schedule localNotification
    UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
    [center addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {if (!error) {NSLog(@"add NotificationRequest succeeded!");}
    }];
    

已更新

由于未捕获的异常"NSInternalInconsistencyException"而终止应用程序,原因:"如果重复,则时间间隔必须至少为60"

let trigger = UNTimeIntervalNotificationTrigger.init(timeInterval: 60, repeats: true)

iOS 10中不推荐使用UILocalNotification(UILocalNotification is deprecated in iOS 10)相关推荐

  1. Android多个imei如何获取,如何在Android 10中获取IMEI号,这是获取在Android 10及以下Android 10中获取IMEI号的代码...

    如何在android 10中获取imei编号,这是获取在android 10及以下android 10中获取imei编号的代码. if (android.os.Build.VERSION.SDK_IN ...

  2. ios开发text kit_第9章 iOS 7中文字排版和渲染引擎——Text Kit

    第 9 章 iOS 7中文字排版和渲染引擎--Text Kit 在iOS 7之前,应用中字体的大小用户是不能设置的,而且开发人员要想实现多种样式的文字排版是件非常麻烦的事情.在iOS 7之后,这些问题 ...

  3. Android 10 中有关限制非 SDK 接口的更新

    Android 10 中有关限制非 SDK 接口的更新 目录 浅灰和深灰列表的命名发生变化 非 SDK 接口的代码注释 在 Android 10 中授予对非 SDK 接口的访问权限 Android 1 ...

  4. 如何在Windows 10中管理UAC的教程

    Windows 10中的UAC或用户帐户控制是一种内置功能,可防止游戏,应用程序和许多其他程序对您的操作系统进行更改,在最坏的情况下可能会损坏您的操作系统,使其无法修复并被迫安装备份副本或只是重新安装 ...

  5. html计算器重置按钮,在Windows 10中重置和重新安装计算器 | MOS86

    Windows 10中的新计算器应用程序运行了一个干净的用户界面,并且在触摸设备上也很出色. 除了默认启动计算器应用程序默认设置的标准模式之外,还可以在Calculator应用程序中使用程序员和科学模 ...

  6. 在桌面上添加或删除计算机网络等图标,如何在Windows 10中添加或删除默认桌面图标...

    我们将看到如何在Windows 10中添加或 让我们开始吧: 在Windows 10设备中,选择"开始"按钮,然后选择"设置". 如何在Windows 10中添 ...

  7. 在 Windows 10 中,在安全模式下启动电脑

    在 Windows 10 中,在安全模式下启动电脑 适用于: Windows 10 通过使用一组有限的文件和驱动程序,安全模式可使 Windows 在基本状态下启动.如果在安全模式下没有出现问题,说明 ...

  8. 使用 Swift 在 iOS 10 中集成 Siri —— SiriKit 教程

    使用 Swift 在 iOS 10 中集成 Siri -- SiriKit 教程 转载地址:http://swift.gg/2016/06/28/adding-siri-to-ios-10-apps- ...

  9. 关于 iOS 10 中 ATS 的问题

    WWDC 15 提出的 ATS (App Transport Security) 是 Apple 在推进网络通讯安全的一个重要方式.在 iOS 9 和 OS X 10.11 中,默认情况下非 HTTP ...

最新文章

  1. Java打造一款SSH客户端,而且已开源
  2. php artisan实现机制,laravel 原理机制及几个重要功能
  3. 以下选项不是python打开方式的是-以下选项中,不是Python对文件的打开模式的是...
  4. 【NLP】10000亿参数!英伟达用3072块A100训出史上最大最贵GPT!
  5. Python高级特性:列表生成式
  6. java代码自动生成的插件_如何使用插件生成自定义Java 8代码
  7. python连接oracle报错tns_Python3操作oracle数据库及遇到的报错
  8. JavaScript学习(八十二)—JavaScript的组成
  9. android双usb麦克风,USB麦克风24bit192K单麦芯片方案-SSS1630
  10. 细胞亚器互作之细胞代谢调节液泡与线粒体的接触位Cellular Metabolism Regulates Contact Sites between Vacuoles and Mitochondria
  11. 【C语言】break,continue的区别
  12. Ubuntu wine QQ卡死
  13. 银行会计凭证粗略整理
  14. kubectl exec
  15. 有效括号 python_1111. 有效括号的嵌套深度
  16. LS文法构图算法(3) Hilbert-Peano曲线
  17. Cesium环境 Primitives加载广告牌出现Cannot read property ‘globe‘ of undefined
  18. uniapp(H5) + signalr 制作的简单的卡牌游戏
  19. mysql导入数据报错ERROR 1290 (HY000): The MySQL server is running with the --secure-file-priv option so it
  20. 【松岩论道】调整如期而至,锁定低吸主线!(干货到位!)

热门文章

  1. python和node爬虫_node可以做爬虫吗?
  2. Microsoft Excel 教程:如何在 Excel 图表中添加趋势线?
  3. tyvj p1030 乳草的入侵
  4. linux运行C语言程序出现空白,无论输入什么都没有反应
  5. xv6 源码调试环境搭建
  6. MacBook如何选择
  7. grib1文件解析 python_grib文件解析
  8. Java 锁机机制——浅析 Synchronized
  9. 使用CMD命令行跳转到指定文件或者文件夹
  10. 习题6-7 简单计算器 (20 分)