http://www.hangge.com/blog/cache/detail_1851.html
三、一个简单的本地通知样例
1,效果图
(1)程序启动后会自动创建并发送一个 30 秒后的通知,接着我们便可以锁屏或者将应用切到后台。
(2)30 秒时间一到,如果当前是锁屏状态。通知会出现在屏幕横幅中。如果当前是在系统里的话,则会出现在屏幕顶部。当然通知中心里也会有这条通知。
![原文:Swift - UserNotifications框架使用详解2(发送本地通知)]()
![原文:Swift - UserNotifications框架使用详解2(发送本地通知)]()
2,样例代码
(1)首先我们在 AppDelegate.swift 中申请通知权限。当然写在其它地方也是可以的,写这里只是为了方便测试,让程序一启动就去申请权限。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
|
import UIKit
import UserNotifications
@UIApplicationMain
class AppDelegate : UIResponder , UIApplicationDelegate {
var window: UIWindow ?
func application(_ application: UIApplication , didFinishLaunchingWithOptions
launchOptions: [ UIApplicationLaunchOptionsKey : Any ]?) -> Bool {
//请求通知权限
UNUserNotificationCenter .current()
.requestAuthorization(options: [.alert, .sound, .badge]) {
(accepted, error) in
if !accepted {
print ( "用户不允许消息通知。" )
}
}
return true
}
func applicationWillResignActive(_ application: UIApplication ) {
}
func applicationDidEnterBackground(_ application: UIApplication ) {
}
func applicationWillEnterForeground(_ application: UIApplication ) {
}
func applicationDidBecomeActive(_ application: UIApplication ) {
}
func applicationWillTerminate(_ application: UIApplication ) {
}
}
|
(2)然后在程序页面加载完毕后(ViewController.swift)创建一条简单的通知消息(30 秒后触发)。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
|
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)。
1
2
3
4
5
6
|
//设置推送内容
let content = UNMutableNotificationContent ()
content.title = "hangge.com"
content.subtitle = "航歌(二级标题)"
content.body = "做最好的开发者知识平台"
content.badge = 2
|
效果图如下:
![原文:Swift - UserNotifications框架使用详解2(发送本地通知)]()
![原文:Swift - UserNotifications框架使用详解2(发送本地通知)]()
五、设置通知触发器
目前 UserNotifications 框架中一共提供了如下三种触发器。注意:触发器是只对本地通知而言的,远程推送的通知默认会在收到后立即显示。
1,一段时间后触发(UNTimeIntervalNotificationTrigger)
比如下面样例我们设置10秒钟后触发推送通知。
1
|
let trigger = UNTimeIntervalNotificationTrigger (timeInterval: 10, repeats: false )
|
2,指定日期时间触发(UNCalendarNotificationTrigger)
(1)下面代码我们设置2017年11月11日凌晨触发推送通知。
1
2
3
4
5
|
var components = DateComponents ()
components.year = 2017
components.month = 11
components.day = 11
let trigger = UNCalendarNotificationTrigger (dateMatching: components, repeats: false )
|
(2)下面代码我们设置每周一上午8点都会触发推送通知。
1
2
3
4
5
|
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 框架)
1
2
3
4
5
|
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 转载请保留原文链接:http://www.hangge.com/blog/cache/detail_1851.html