gpt4 book ai didi

ios - UILocalNotification 在 iOS 10 中被弃用

转载 作者:IT王子 更新时间:2023-10-29 07:39:48 25 4
gpt4 key购买 nike

这可能是一个提前的问题,但我想知道在 iOS 10 中使用什么代替 UILocalNotification。我正在开发一个部署目标为 iOS 8 的应用程序,所以可以使用 UILocalNotification?

最佳答案

是的,您可以使用 UILocalNotification,旧的 API 也适用于 iOS 10,但我们最好改用 User Notifications 框架中的 API。还有一些新功能,您只能与 iOS 10 用户通知框架一起使用。

这也发生在远程通知上,有关更多信息:Here .

新功能:

  • 现在您也可以在 iOS 10 的应用程序处于前台时显示提醒、声音或增加角标(Badge)
  • 现在您可以在用户点击(或滑动)操作按钮时在一个地方处理所有事件,即使应用已被终止。
  • 支持 3D 触摸而不是滑动手势。
  • 现在您只需一行代码即可删除特定的本地通知。
  • 通过自定义 UI 支持富通知。

我们很容易将 UILocalNotification API 转换为 iOS 10用户通知框架 API,它们非常相似。

我在这里写了一个演示来展示如何同时使用新旧API:iOS 10 Adaptation Tips .

例如,

使用 Swift 实现:

  1. 导入用户通知

    ///    Notification become independent from UIKit
    import UserNotifications
  2. 请求本地通知授权

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

  4. 更新应用程序图标角标(Badge)编号

    @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. 请求本地通知授权

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

  4. 更新应用程序图标角标(Badge)编号

    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 = [UNTimeIntervalNotificationTrigger
    triggerWithTimeInterval:5.f
    repeats:NO];
    UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:@"FiveSecond"
    content:content
    trigger:trigger];
    /// 3. schedule localNotification
    UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
    [center addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {
    if (!error) {
    NSLog(@"add NotificationRequest succeeded!");
    }
    }];

已更新

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 - UILocalNotification 在 iOS 10 中被弃用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37938771/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com