Swift - UserNotifications框架使用详解2(发送本地通知)

三、一个简单的本地通知样例

1,效果图

(1)程序启动后会自动创建并发送一个 30 秒后的通知,接着我们便可以锁屏或者将应用切到后台。

(2)30 秒时间一到,如果当前是锁屏状态。通知会出现在屏幕横幅中。如果当前是在系统里的话,则会出现在屏幕顶部。当然通知中心里也会有这条通知。

2,样例代码

(1)首先我们在 AppDelegate.swift 中申请通知权限。当然写在其它地方也是可以的,写这里只是为了方便测试,让程序一启动就去申请权限。

import UIKit

import UserNotifications

@UIApplicationMain

class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?

   func application(_ application: UIApplication, didFinishLaunchingWithOptions

        launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {

        //请求通知权限

        UNUserNotificationCenter.current()

            .requestAuthorization(options: [.alert, .sound, .badge]) {

                (accepted, error) in

                if !accepted {

                    print("用户不允许消息通知。")

                }

        }


        return true

    }

}

(2)然后在程序页面加载完毕后(ViewController.swift)创建一条简单的通知消息(30 秒后触发)。

import UIKit

import UserNotifications

class ViewController: UIViewController {

    override func viewDidLoad() {

        super.viewDidLoad()

       //设置推送内容

        let content = UNMutableNotificationContent()

        content.title = "hangge.com"

        content.body = "做最好的开发者知识平台"


        //设置通知触发器

        let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 30, repeats: false)


        //设置请求标识符

        let requestIdentifier = "com.hangge.testNotification"


        //设置一个通知请求

        let request = UNNotificationRequest(identifier: requestIdentifier,

                                            content: content, trigger: trigger)


        //将通知请求添加到发送中心

        UNUserNotificationCenter.current().add(request) { error in

            if error == nil {

                print("Time Interval Notification scheduled: \(requestIdentifier)")

            }

        }

    }


    override func didReceiveMemoryWarning() {

        super.didReceiveMemoryWarning()

    }

}

四、设置推送内容

上面的样例中我们只设置了推送通知的标题(title)和内容(body),其实还可以设置子标题(subtitle)和应用图标标记(badge)。

//设置推送内容

let content = UNMutableNotificationContent()

content.title = "hangge.com"

content.subtitle = "航歌(二级标题)"

content.body = "做最好的开发者知识平台"

content.badge = 2

效果图如下:

五、设置通知触发器

目前 UserNotifications 框架中一共提供了如下三种触发器。注意:触发器是只对本地通知而言的,远程推送的通知默认会在收到后立即显示。

1,一段时间后触发(UNTimeIntervalNotificationTrigger)

比如下面样例我们设置10秒钟后触发推送通知。

1let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 10, repeats: false)

2,指定日期时间触发(UNCalendarNotificationTrigger)

(1)下面代码我们设置2017年11月11日凌晨触发推送通知。

var components = DateComponents()

components.year = 2017

components.month = 11

components.day = 11

let trigger = UNCalendarNotificationTrigger(dateMatching: components, repeats: false)

(2)下面代码我们设置每周一上午8点都会触发推送通知。

var components = DateComponents()

components.weekday = 2 //周一

components.hour = 8 //上午8点

components.second = 30 //30分

let trigger = UNCalendarNotificationTrigger(dateMatching: components, repeats: true)

3,根据位置触发(UNLocationNotificationTrigger)

该触发器支持进入某地触发、离开某地触发、或者两种情况均触发。下面代码设置成当手机进入到指定点(纬度:52.10,经度:51.11)200 米范围内时会触发推送通知。(注意:这里我们需要 import CoreLocation 框架)

let coordinate = CLLocationCoordinate2D(latitude: 52.10, longitude: 51.11)

let region = CLCircularRegion(center: coordinate, radius: 200, identifier: "center")

region.notifyOnEntry = true //进入此范围触发

region.notifyOnExit = false //离开此范围不触发

let trigger = UNLocationNotificationTrigger(region: region, repeats: true)

原文出自:www.hangge.com  转载请保留原文链接:https://www.hangge.com/blog/cache/detail_1851.html

你可能感兴趣的:(Swift - UserNotifications框架使用详解2(发送本地通知))