iOS 10中不推荐使用UILocalNotification [英] UILocalNotification is deprecated in iOS 10

查看:687
本文介绍了iOS 10中不推荐使用UILocalNotification的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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?

推荐答案

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

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.

新功能:

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

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

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

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

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

例如,

使用Swift实现:

  1. 导入用户通知

  1. import UserNotifications

///    Notification become independent from UIKit
import UserNotifications

  • localNotification的请求授权

  • request authorization for localNotification

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

  • 安排localNotification

  • schedule localNotification

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

    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实现:

    Objective-C implementation:

    1. 导入用户通知

    1. import UserNotifications

    // Notifications are independent from UIKit
    #import <UserNotifications/UserNotifications.h>
    

  • localNotification的请求授权

  • 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];
                              }
                          }];
    

  • 安排localNotification

  • schedule localNotification

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

    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 = [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!");
        }
    }];
    

  • 已更新

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

    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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆