ios10本地通知

2016-11-17  本文已影响1495人  金风细细

本地通知

需要加引用.加库

引用库

swift写法:

import UserNotifications

oc写法:

#import <UserNotifications/UserNotifications.h>

一. 注册通知

let center = UNUserNotificationCenter.current()
center.requestAuthorization(options: [.alert, .sound]) { (granted, error) in
    // Enable or disable features based on authorization.
}

//最好能立刻指定代理 ,该代理的文档说:delegate必须在app结束launch的时候设置,可以在appdelegate的
//application(_:willFinishLaunchingWithOptions:)或者 application(_:didFinishLaunchingWithOptions:)中设置
center.delegate = self
 center.getNotificationSettings { (UNNotificationSettings) in
      print("settsing is :\(UNNotificationSettings)")
 }

二. 发送通知

  1. UNCalendarNotificationTrigger : 日历闹钟,和NSDateComponents结合,可以做到在每天指定的时刻发送通知,官网例子:
let date = DateComponents()date.hour = 8date.minute = 30
 let trigger = UNCalendarNotificationTrigger(dateMatching: date, repeats: true)
  1. UNTimeIntervalNotificationTrigger: 定时闹钟
  2. UNLocationNotificationTrigger:地点闹钟,和CLLocationCoordinate2D结合,可以做到进入了某个固定区域时发送通知,官网例子:
let center = CLLocationCoordinate2D(latitude: 37.335400, longitude: -122.009201)
let region = CLCircularRegion(center: center, radius: 2000.0, identifier: "Headquarters")
region.notifyOnEntry = true
region.notifyOnExit = false
let trigger = UNLocationNotificationTrigger(region: region, repeats: false)

OK!不扯远了,开始干!

let content = UNMutableNotificationContent()
content.title = NSString.localizedUserNotificationStringForKey("Hello!", arguments: nil)
content.body = NSString.localizedUserNotificationStringForKey("Hello_message_body", arguments: nil)
content.sound = UNNotificationSound.default()
 // Deliver the notification in five seconds.
let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 5, repeats: false)
let request = UNNotificationRequest(identifier: "FiveSecond", content: content, trigger: trigger)
 // Schedule the notification.
let center = UNUserNotificationCenter.current()
center.add(request)

三. 代理方法

通知到达后,两个代理方法:

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

  completionHandler([UNNotificationPresentationOptions.alert, UNNotificationPresentationOptions.badge, UNNotificationPresentationOptions.sound])
        
  //completionHandler([])
}

注意:

2. 点击通知才会进入的方法didReceive
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Swift.Void){
   completionHandler()
}

**注意: **

四.其他要点

1. 注册通知写在何处?

官网推荐写在application:didFinishLaunchingWithOptions:
总之不能写在scheduling一个本地/远程通知之后.

2. 删除过期通知
3. Action和category

代码十分简单:

let generalCategory = UNNotificationCategory(identifier: "GENERAL",
actions: [],intentIdentifiers: [],options: .customDismissAction)

// Create the custom actions for the TIMER_EXPIRED category.
let snoozeAction = UNNotificationAction(identifier: "SNOOZE_ACTION",title: "Snooze",options: UNNotificationActionOptions(rawValue: 0))

let stopAction = UNNotificationAction(identifier:"STOP_ACTION",title: "Stop",options: .foreground)

let expiredCategory =UNNotificationCategory(identifier: "TIMER_EXPIRED",actions: [snoozeAction, stopAction],intentIdentifiers: [],options: UNNotificationCategoryOptions(rawValue: 0))

// Register the notification categories.
let center = UNUserNotificationCenter.current()

center.setNotificationCategories([generalCategory, expiredCategory])

在发送通知消息时,把UNMutableNotificationContent的categoryIdentifier赋值为category的identifier,就可以了

 let content = UNMutableNotificationContent()
        
content.title = "coffee"
content.body = "Time for another cup of coffee!"
content.sound = UNNotificationSound.default()
        
//**就是这里,要和上面的category的identifier一致**
content.categoryIdentifier = "GENERAL";
        
let trigger = UNTimeIntervalNotificationTrigger(timeInterval:3, repeats:false)

let request = UNNotificationRequest(identifier:"TIMER_EXPIRED", content:content,trigger:trigger)
        
UNUserNotificationCenter.current().add(request) { error in
    print("注册成功!")
}

效果:

Screen Shot 2016-11-15 at 下午2.23.32.png

点击之后的处理

func userNotificationCenter(_ center: UNUserNotificationCenter,

didReceive response: UNNotificationResponse,

withCompletionHandler completionHandler: @escaping () -> Void) {

if response.notification.request.content.categoryIdentifier == "TIMER_EXPIRED" {

// Handle the actions for the expired timer.

if response.actionIdentifier == "SNOOZE_ACTION" {

// Invalidate the old timer and create a new one. . .

}

else if response.actionIdentifier == "STOP_ACTION" {

// Invalidate the timer. . .

}

}

// Else handle actions for other notification types. . .

}
4. 官网还提供了类似"闹钟"的例子,每天七点闹醒你!
let content = UNMutableNotificationContent()

content.title = NSString.localizedUserNotificationString(forKey: "Wake up!", arguments: nil)

content.body = NSString.localizedUserNotificationString(forKey: "Rise and shine! It's morning time!",

arguments: nil)

// Configure the trigger for a 7am wakeup.

var dateInfo = DateComponents()

dateInfo.hour = 7

dateInfo.minute = 0

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

// Create the request object.

let request = UNNotificationRequest(identifier: "MorningAlarm", content: content, trigger: trigger)
5. 改变通知的声音

通知的声音是可以改变的.UNNotificationSound.default()只是系统铃声而已.
声音文件可以放在bundle里面,也可以在线下载,放在APP沙盒的Library/Sounds里.

content.sound = UNNotificationSound(named: "MySound.aiff")

demon
参考:官网

上一篇 下一篇

猜你喜欢

热点阅读