UILocalNotification在iOS10中已弃用 [英] UILocalNotification is deprecated in iOS10

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

问题描述

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

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

推荐答案

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

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

这也适用于远程通知,有关详细信息:这里

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

新功能:


  1. 现在,你可以使用iOS 10在应用程序处于前台时显示警报,声音或增加徽章

  2. 现在你可以在一个地方处理所有事件当用户点击(或滑动)动作按钮时,即使应用程序已经被杀死。

  3. 支持3D触摸而不是滑动手势。

  4. 现在,您只需按行代码删除特定的本地通知。

  5. 支持使用自定义UI的Rich Notification。

  1. Now you can either present alert, sound or increase badge while the app is in foreground too with iOS 10
  2. 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.
  3. Support 3D touch instead of sliding gesture.
  4. Now you can remove specifical local notification just by one row code.
  5. Support Rich Notification with custom UI.

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

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

我在这里写了一个Demo来展示如何同时使用新旧API: iOS10AdaptationTips

I write a Demo here to show how to use new and old APIs at the same time: iOS10AdaptationTips .

例如,

使用Swift实现:


  1. import UserNotifications

  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 five 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. import UserNotifications

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


  • 返回此处获取更多信息: iOS10AdaptationTips

    Go to here for more information: iOS10AdaptationTips.


    由于未捕获的异常'NSInternalInconsistencyException'而终止应用程序,原因:'如果重复'

    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)
    

    这篇关于UILocalNotification在iOS10中已弃用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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