前提・実現したいこと
ローカル通知のカスタムアクションを使って画面のラベルのテキストの表示を変更したい。
発生している問題・エラーメッセージ
カスタムアクションが実行されていないのか、ラベルのテキストが変更されない
該当のソースコード
swift
1 2import UIKit 3import UserNotifications 4class ViewController: UIViewController,UNUserNotificationCenterDelegate { 5 6 @IBOutlet var colorView: UIView! 7 @IBOutlet var message: UILabel! 8 override func viewDidLoad() { 9 super.viewDidLoad() 10 } 11 12 override func didReceiveMemoryWarning() { 13 super.didReceiveMemoryWarning() 14 } 15 @IBAction func goPush(_ sender: Any) { 16 let center = UNUserNotificationCenter.current() 17 18 // 通知飛ばしていいかの許可 19 // getNotificationSettingsは通知の可否など設定項目を取得する 20 center.getNotificationSettings { (settings) in 21 if(settings.authorizationStatus == .authorized){ 22 //知らせる 23 self.push() 24 }else{ 25 center.requestAuthorization(options: [.sound,.badge,.alert], completionHandler: { (granted, error) in 26 if let error = error{ 27 print(error) 28 }else{ 29 if(granted){ self.push() } 30 } 31 }) 32 } 33 } 34 } 35 func push(){ 36 let content = UNMutableNotificationContent() 37 let center = UNUserNotificationCenter.current() 38 39 content.title = "色を選べ" 40 content.body = "何色だった?" 41 content.categoryIdentifier = "COLOR" 42 //5秒後に送信する 43 let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 5.0, repeats: false) 44 45 let notifcationRequet = UNNotificationRequest(identifier: "id1", content: content, trigger: trigger) 46 center.add(notifcationRequet) { (error) in 47 //エラー処理 48 if let error = error{ 49 print(error) 50 }else{ 51 print("成功") 52 } 53 } 54 let generalCategory = UNNotificationCategory(identifier: "GENERAL", actions: [], intentIdentifiers: [], options:.customDismissAction) 55 let blueAction = UNNotificationAction(identifier: "BLUE", title: "青だっけ?", options: .foreground) 56 let redAction = UNNotificationAction(identifier: "RED", title: "赤だっけ?", options: .foreground) 57 let colorCategory = UNNotificationCategory(identifier: "COLOR", actions: [blueAction,redAction], intentIdentifiers: [], options: []) 58 //上3つを登録 59 center.setNotificationCategories([generalCategory,colorCategory]) 60 } 61 func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) { 62 if response.notification.request.content.categoryIdentifier == "COLOR" { 63 if response.actionIdentifier == "BLUE" {saveText = "正解!!"} 64 else if response.actionIdentifier == "RED" {saveText = "残念!"} 65 } 66 } 67 68 } 69 70 71 72 73 74
試したこと
ViewControllerからAppDeligateにローカル通知周りを移してみようと思ったのですが、値の受け渡しがうまくいかず断念しました。
補足情報(FW/ツールのバージョンなど)
swift4.1.2
xcode9.4.1
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/06/17 17:45