前提・実現したいこと
swiftで、毎日同じ時間にローカル通知が行き、
その時間から一定間隔でまた通知が行く(スヌーズを押せば同様に一定間隔でまた通知が行き、ストップを押せばその日は通知が行かない)
というiOSアプリを作りたいです。
該当のソースコード
swift
1 func applicationDidEnterBackground(_ application: UIApplication) { 2 // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. 3 // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits. 4 5 // 通知設定に必要なクラスをインスタンス化 6 let trigger: UNNotificationTrigger 7 let content = UNMutableNotificationContent() 8 var notificationTime = DateComponents() 9 10 11 // トリガー設定 12 notificationTime.hour = 23 13 notificationTime.minute = 0 14 trigger = UNCalendarNotificationTrigger(dateMatching: notificationTime, repeats: true) 15 16 17 let snooze = UNNotificationAction(identifier: ActionIdentifier.snooze.rawValue, title: "スヌーズ", options: []) 18 let stop = UNNotificationAction(identifier: ActionIdentifier.stop.rawValue, title: "ストップ", options: []) 19 20 let category = UNNotificationCategory(identifier: "alarm", 21 actions: [snooze, stop], 22 intentIdentifiers: [], 23 hiddenPreviewsBodyPlaceholder:"アラーム", 24 options: []) 25 UNUserNotificationCenter.current().setNotificationCategories([category]) 26 27 //通知内容を設定 28 content.title = "" 29 content.body = "" 30 content.sound = UNNotificationSound.default 31 32 // categoryIdentifierを設定 33 content.categoryIdentifier = "alarm" 34 35 // 通知スタイルを指定 36 let request = UNNotificationRequest(identifier: "timeRequest", content: content, trigger: trigger) 37 38 39 // 通知をセット 40 UNUserNotificationCenter.current().add(request, withCompletionHandler: nil) 41 42 43 } 44 45 enum ActionIdentifier: String { 46 case snooze 47 case stop 48 } 49 50 @available(iOS 10.0, *) 51 func userNotificationCenter(_ center: UNUserNotificationCenter, 52 didReceive response: UNNotificationResponse, 53 withCompletionHandler completionHandler: () -> Void) { 54 55 56 switch response.actionIdentifier { 57 case ActionIdentifier.snooze.rawValue: 58 print("スヌーズ") 59 case ActionIdentifier.stop.rawValue: 60 print("ストップ") 61 default: 62 () 63 } 64 completionHandler() 65 }
わからない点
トリガーをUNTimeIntervalNotificationTriggerにした場合のように一定間隔で通知を行いたいのですが、その通知の始まりを指定した時間にしたいです。
そのようなことは可能でしょうか。
可能であれば、どうすれば良いかを教えていただきたいです。
アクション付き通知にしているので、アクションによってfunc applicationDidEnterBackgroundの中のnotificationTimeの変更を行うこと、UNTimeIntervalNotificationTriggerによる通知を行わせる(またはやめさせる)ことを試みましたができませんでした。
全く違うやり方でも構いません。
初心者ですがよろしくお願い致します。
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/02/20 08:51
2019/02/21 02:49