Upgrade to Pro — share decks privately, control downloads, hide ads and more …

アプリ道場サロン 第1回LTスライド

Avatar for Nekokichi Nekokichi
January 27, 2021

 アプリ道場サロン 第1回LTスライド

Swiftのローカル通知をバックグラウンドに対応

Avatar for Nekokichi

Nekokichi

January 27, 2021

More Decks by Nekokichi

Other Decks in Programming

Transcript

  1. func setupUNNotification() { // ௨஌಺༰ let content = UNMutableNotificationContent() content.sound

    = .default content.title = "௨஌Λड৴" content.body = "f2fwegewgewgewgewg" // 3ඵޙʹ௨஌ let timeIntervalTrigger = UNTimeIntervalNotificationTrigger(timeInterval: 3, repeats: false) // ௨஌ͷϦΫΤετ let request = UNNotificationRequest(identifier: UNNotificationValues.localNotificationIdentifier, content: content, trigger: timeIntervalTrigger) // ௨஌Λొ࿥ let center = UNUserNotificationCenter.current() center.add(request) { (error: Error?) in if let error = error { print(error.localizedDescription) } } } ϩʔΧϧ௨஌ͷ࡞੒
  2. extension AppDelegate: UNUserNotificationCenterDelegate { // ΞϓϦ্(ϑΥΞάϥ΢ϯυ)Ͱ௨஌Λड৴ͨ͠৔߹ func userNotificationCenter( _ center:

    UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {} // ௨஌Λλοϓͨ͠৔߹ func userNotificationCenter( _ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {} } ϩʔΧϧ௨஌ͷϝιου
  3. func sceneDidBecomeActive(_ scene: UIScene) { // ௨஌͞Εͨ௨஌Λશͯऔಘ // notifications:[UNNotification] UNUserNotificationCenter.current().getDeliveredNotifications

    { (notifications) in if !notifications.isEmpty { for notification in notifications { // ϩʔΧϧ௨஌ͷϓϩύςΟΛऔΓग़ͤΔ print(notification.request.identifier) print(notification.request.content.title) print(notification.request.content.body) } } // ड৴ॲཧ͕׬ྃͨ͠ͷͰ࡟আ UNUserNotificationCenter.current().removeDeliveredNotifications(withIdentifiers: ["ϩʔΧϧ௨஌ͷidenfier"]) } }
  4. func sceneWillEnterForeground(_ scene: UIScene) { // ௨஌͞Εͨ௨஌Λશͯऔಘ // notifications:[UNNotification] UNUserNotificationCenter.current().getDeliveredNotifications

    { (notifications) in if !notifications.isEmpty { for notification in notifications { // ϩʔΧϧ௨஌ͷϓϩύςΟΛऔΓग़ͤΔ print(notification.request.identifier) print(notification.request.content.title) print(notification.request.content.body) } } // ड৴ॲཧ͕׬ྃͨ͠ͷͰ࡟আ UNUserNotificationCenter.current().removeDeliveredNotifications(withIdentifiers: ["ϩʔΧϧ௨஌ͷidenfier"]) } }