iOS 10中不推荐使用UILocalNotification

鲁乌尔

这可能是一个预先提出的问题,但我想知道UILocalNotification在iOS 10中使用什么而不是使用UILocalNotification什么。

陈奕迅

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

有关更多信息,“远程通知”也会发生这种情况:此处

新的功能:

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

对于我们来说,将UILocalNotificationAPI转换为iOS 10用户通知框架API真的很容易,它们确实很相似。

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

例如,

使用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. 更新应用程序图标徽章编号

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

目标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. 更新应用程序图标徽章编号

    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”

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

iOS中不推荐使用NSYearCalendarUnit

来自分类Dev

在iOS 7.0中不推荐使用scrollViewTexturedBackgroundColor

来自分类常见问题

iOS 11.0中不推荐使用“ automaticallyAdjustsScrollViewInsets”

来自分类Dev

iOS 8中不推荐使用InterfaceOrientation

来自分类Dev

iOS 8.0中不推荐使用dataWithContentsOfMappedFile

来自分类Dev

在iOS 7.0中不推荐使用scrollViewTexturedBackgroundColor

来自分类Dev

在iOS 8.0中不推荐使用UIBarButton

来自分类Dev

不推荐使用Base64Encoding:在iOS 7.0中首先不推荐使用

来自分类Dev

使用什么代替iOS 7中不推荐使用的regionMonitoringAvailable?

来自分类Dev

在iOS 8中不推荐使用playerID:使用player

来自分类Dev

使用什么代替iOS 7中不推荐使用的regionMonitoringAvailable?

来自分类Dev

iOS7中不推荐使用:AudioSessionSetProperty

来自分类Dev

由于不推荐使用的方法,iOS 7中的性能问题

来自分类Dev

在iOS 8.0和更高版本中不推荐使用“ push”

来自分类Dev

由于不推荐使用的方法,iOS 7中的性能问题

来自分类Dev

在iOS 8.0和更高版本中不推荐使用“ push”

来自分类Dev

iOS 9.0中不推荐使用Swift NSURLConnection

来自分类Dev

在iOS 8中不推荐使用showFromTabBar方法吗?

来自分类Dev

如何找出iOS中不推荐使用功能的原因?

来自分类Dev

不推荐使用“ SSLClose”:在iOS 13.0中首次不推荐使用-不再受支持。使用Network.framework

来自分类Dev

不推荐使用'sizeWithFont:constrainedToSize:lineBreakMode:':在iOS 7.0中首次不推荐使用-boundingRectWithSize:options:attributes:context:

来自分类常见问题

UIAlertView首先不推荐使用IOS 9

来自分类Dev

ios 7不推荐使用initWithOverlay

来自分类Dev

iOS 14不推荐使用CLLocationManager的AuthorizationStatus

来自分类Dev

Base64Encoding已不推荐使用:iOS 7.0中第一次不推荐使用

来自分类Dev

我应该使用什么代替iOS7中不推荐使用的GKLeaderboardViewController?

来自分类Dev

如果不推荐使用UIAlertView,为什么它在iOS 8中可以使用?

来自分类Dev

Swift-在iOS 9.0中不推荐使用'init()':使用-initWithConcurrencyType:代替

来自分类Dev

在iOS 8.3中不推荐使用actionSheet:didDismissWithButtonIndex:。我现在必须使用什么新方法?

Related 相关文章

  1. 1

    iOS中不推荐使用NSYearCalendarUnit

  2. 2

    在iOS 7.0中不推荐使用scrollViewTexturedBackgroundColor

  3. 3

    iOS 11.0中不推荐使用“ automaticallyAdjustsScrollViewInsets”

  4. 4

    iOS 8中不推荐使用InterfaceOrientation

  5. 5

    iOS 8.0中不推荐使用dataWithContentsOfMappedFile

  6. 6

    在iOS 7.0中不推荐使用scrollViewTexturedBackgroundColor

  7. 7

    在iOS 8.0中不推荐使用UIBarButton

  8. 8

    不推荐使用Base64Encoding:在iOS 7.0中首先不推荐使用

  9. 9

    使用什么代替iOS 7中不推荐使用的regionMonitoringAvailable?

  10. 10

    在iOS 8中不推荐使用playerID:使用player

  11. 11

    使用什么代替iOS 7中不推荐使用的regionMonitoringAvailable?

  12. 12

    iOS7中不推荐使用:AudioSessionSetProperty

  13. 13

    由于不推荐使用的方法,iOS 7中的性能问题

  14. 14

    在iOS 8.0和更高版本中不推荐使用“ push”

  15. 15

    由于不推荐使用的方法,iOS 7中的性能问题

  16. 16

    在iOS 8.0和更高版本中不推荐使用“ push”

  17. 17

    iOS 9.0中不推荐使用Swift NSURLConnection

  18. 18

    在iOS 8中不推荐使用showFromTabBar方法吗?

  19. 19

    如何找出iOS中不推荐使用功能的原因?

  20. 20

    不推荐使用“ SSLClose”:在iOS 13.0中首次不推荐使用-不再受支持。使用Network.framework

  21. 21

    不推荐使用'sizeWithFont:constrainedToSize:lineBreakMode:':在iOS 7.0中首次不推荐使用-boundingRectWithSize:options:attributes:context:

  22. 22

    UIAlertView首先不推荐使用IOS 9

  23. 23

    ios 7不推荐使用initWithOverlay

  24. 24

    iOS 14不推荐使用CLLocationManager的AuthorizationStatus

  25. 25

    Base64Encoding已不推荐使用:iOS 7.0中第一次不推荐使用

  26. 26

    我应该使用什么代替iOS7中不推荐使用的GKLeaderboardViewController?

  27. 27

    如果不推荐使用UIAlertView,为什么它在iOS 8中可以使用?

  28. 28

    Swift-在iOS 9.0中不推荐使用'init()':使用-initWithConcurrencyType:代替

  29. 29

    在iOS 8.3中不推荐使用actionSheet:didDismissWithButtonIndex:。我现在必须使用什么新方法?

热门标签

归档