天天看點

iOS本地推送通知的基本使用

iOS10以前本地通知(UILocalNotification)

使用步驟:

  1. 建立一個UILocalNotification對象
  2. 設定觸發時間及标題、内容
  3. 注冊并安排通知
// 1. 建立一個UILocalNotification對象
let localNotification = UILocalNotification()

// 2. 設定觸發時間及标題、内容
localNotification.fireDate = Date(timeIntervalSinceNow: 3)
localNotification.alertTitle = "Title"
localNotification.alertBody = "alertBodyalertBodyalertBodyalertBody"

// 0. 注冊通知(一般在程式剛啟動時注冊通知)
UIApplication.shared.registerUserNotificationSettings(UIUserNotificationSettings(types: [.badge, .alert, .sound], categories: nil))

// 3. 安排通知
UIApplication.shared.scheduleLocalNotification(localNotification)           
  • UILocalNotification的其他屬性
    • applicationIconBadgeNumber

      :應用程式圖示上的數字标記
    • repeatInterval

      :重複間隔(按照年、月、日、時、分重複)
    • soundName

      :發出通知時的提示音,使用UILocalNotificationDefaultSoundName或者指定的音頻檔案名
    • userInfo

      :與通知相關的額外的字典,使用者在通知上看不到此資料

應用程式處理收到的通知

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
    // ......
    
    // 點選通知啟動程式(程式不在前台也不在背景,即程式退出時),在此可擷取被點選的通知并處理
    if let localNotification = launchOptions?[.localNotification] {
        print(localNotification)
    }
    
    return true
}

// 應用程式收到通知時,在此方法中處理收到的通知
func application(_ application: UIApplication, didReceive notification: UILocalNotification) {
    print(notification)
}           

iOS10+使用通知請求(UNNotificationRequest)建立本地通知

使用步驟

  1. 請求授權
  2. 建立通知内容
  3. 建立通知觸發時間
  4. 使用唯一辨別字元串、内容、觸發器建立通知請求
  5. 将通知請求加到通知中心
// 1. 建立通知内容
let content = UNMutableNotificationContent()
// 标題
content.title = NSString.localizedUserNotificationString(forKey: "Hello!", arguments: nil)
// 内容
content.body = NSString.localizedUserNotificationString(forKey: "Hello_message_body", arguments: nil)
// 通知提示音
content.sound = .default
 
// 2. 建立通知觸發器
// Deliver the notification in five seconds.
let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 5, repeats: false)


// 3. 使用唯一辨別字元串、内容、觸發器建立通知請求
let uuidString = UUID().uuidString
let request = UNNotificationRequest(identifier: uuidString, content: content, trigger: trigger)


// 擷取目前程式的通知中心
let notificationCenter = UNUserNotificationCenter.current()
// 設定代理,用來處理收到的通知
notificationCenter.delegate = self
// 0. 請求授權(一般在程式剛啟動時請求通知授權)
notificationCenter.requestAuthorization(options: [.alert, .badge, .sound]) { (granted, error) in
    
}

// 4. 将通知請求加到通知中心
notificationCenter.add(request) { (error) in
    if error != nil {
       // Handle any errors.
    }
}           
  • UNMutableNotificationContent

    的其他常用屬性
    • subtitle

      :子标題
    • badge

    • userInfo

  • UNNotificationTrigger

    常見的通知觸發器
    • UNTimeIntervalNotificationTrigger

      : 幾秒後觸發,如果要設定可重複觸發需要大于60
    • UNCalendarNotificationTrigger

      :某年某月某日某天某時某分某秒觸發
    • UNLocationNotificationTrigger

      :在某個位置觸發
  • 處理接收到的通知(使用UNUserNotificationCenterDelegate中的兩個方法)
// Asks the delegate to process the user's response to a delivered notification.
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
    // 處理代碼
    ......
    completionHandler()
}

// 應用程式運作在前台時,此方法處理收到的通知
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
    // 處理代碼
    ......
    completionHandler(.sound)
}           

繼續閱讀