Swift 本地通知 iOS8 和 iOS10

 

 

 

 

一。 iOS 8以上

import NotificationCenter

//注册通知

        if #available(iOS 8.0, *)

        {

            //通知类型

            let uns = UIUserNotificationSettings(types: [.alert,.badge,.sound], categories: nil)

            application.registerUserNotificationSettings(uns)

        }

 

 

// 创建并添加本地通知

     if #available(iOS 8.0, *)

        {

            //初始一个通知

            let localNoti = UILocalNotification()

            //通知上显示的内容

            localNoti.alertBody = "通知上显示的提示内容"

            //通知的触发时间

            let firedate = Date(timeIntervalSinceNow: 5)

            localNoti.fireDate = firedate

            //待机界面的滑动动作提示

            localNoti.alertAction = "打开应用"

 

            localNoti.userInfo = ["local":"huanying"]

            //应用程序图标右上角显示的消息数

            localNoti.applicationIconBadgeNumber = 222

            // 添加通知到系统队列中,系统会在指定的时间触发

            UIApplication.shared.scheduleLocalNotification(localNoti)

        }

 

 

     

二。iOS 10以上 

 

import UserNotifications

 实现代理 UNUserNotificationCenterDelegate

 

//注册通知

        if #available(iOS 10, *)

        {

            UNUserNotificationCenter.current().requestAuthorization(options: [UNAuthorizationOptions.alert,UNAuthorizationOptions.badge,UNAuthorizationOptions.sound], completionHandler: { (success, error) in

                

            })

            

        }

    

    if #available(iOS 10, *){

            UNUserNotificationCenter.current().delegate = self

        }

 

//初始化通知

    if #available(iOS 10, *)

        {

            //通知数据

            let content = UNMutableNotificationContent()

            content.title = "大标题"

            content.subtitle = "xiaobiaoti"

            content.body = "body"

            content.userInfo = [:]

            //时间闹钟

            let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 10, repeats: true)

            //通知请求,制定文本和时间闹钟

            let request = UNNotificationRequest(identifier: "identifier", content: content, trigger: trigger)

            //添加本地通知,系统会安排传递

            UNUserNotificationCenter.current().add(request, withCompletionHandler: nil)

        }

 

   

 @available(iOS 10.0, *)

    //通知响应

    func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {

        //获取通知数据,通过userInfo的内容 进行跳转等等

        //response.notification.request.content.userInfo

        completionHandler()

    }

 

    @available(iOS 10.0, *)

    //在应用内展示通知

    func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {

        

        completionHandler([.alert,.sound])

        

    }

 

三。点击通知触发

 

1.//    应用在正在运行(在前台或后台运行),在didReceive代理方法中实现

   

   func application(_ application: UIApplication, didReceive notification: UILocalNotification) {

        let state = UIApplication.shared.applicationState

        if state == .active{

            

        }else if state == .background{

            application.applicationIconBadgeNumber = 0

            let info = notification.userInfo as! [String:String]

            let  str = info["local"]

            let alert = UIAlertController(title: "欢迎", message: "打开", preferredStyle: .alert)

            let action = UIAlertAction(title: "kai", style: .cancel, handler: nil)

            alert.addAction(action)

            window?.rootViewController?.present(alert, animated: true, completion: nil)

        }

    }

 

 

2.//应用程序关闭时,点击通知后的响应,在didFinishLaunchingWithOptions代理方法中实现

  func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

    

       

        //应用程序关闭时,点击通知后的响应

        if launchOptions != nil{

            let  localNoti = launchOptions![UIApplicationLaunchOptionsKey.localNotification] as!UILocalNotification

            localNoti.applicationIconBadgeNumber = 1

        }

}

 

四。监控用户是否允许通知,并转到设置中,开启

  override func viewDidAppear(_ animated: Bool) {

        if UIApplication.shared.currentUserNotificationSettings?.types == UIUserNotificationType.init(rawValue: 0){

            let alert = UIAlertController(title: "前往后台开启通知", message: "开启通知", preferredStyle: .alert)

            let action = UIAlertAction(title: "开启", style: .default, handler: { (alert) in

                let url = URL(string: "prefs:root=NOTIFICATIONS_ID")

                if UIApplication.shared.canOpenURL(url!){

                    UIApplication.shared.openURL(url!)

                }

            })

            alert.addAction(action)

            

            present(alert, animated: true, completion: nil)

        }

    }

转载于:https://www.cnblogs.com/chenxiaopao/p/7807552.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值