質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.49%
Xcode

Xcodeはソフトウェア開発のための、Appleの統合開発環境です。Mac OSXに付随するかたちで配布されています。

Swift

Swiftは、アップルのiOSおよびOS Xのためのプログラミング言語で、Objective-CやObjective-C++と共存することが意図されています

Q&A

解決済

1回答

626閲覧

ローカル通知のカスタムアクションが実行されない

ebiryu

総合スコア16

Xcode

Xcodeはソフトウェア開発のための、Appleの統合開発環境です。Mac OSXに付随するかたちで配布されています。

Swift

Swiftは、アップルのiOSおよびOS Xのためのプログラミング言語で、Objective-CやObjective-C++と共存することが意図されています

0グッド

1クリップ

投稿2018/06/17 13:43

前提・実現したいこと

ローカル通知のカスタムアクションを使って画面のラベルのテキストの表示を変更したい。

発生している問題・エラーメッセージ

カスタムアクションが実行されていないのか、ラベルのテキストが変更されない

該当のソースコード

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

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

実行されないのはUNUserNotificationCenterDelegateselfに設定する処理が抜けているからですね。

swift

1 2override func viewDidLoad() { 3 super.viewDidLoad() 4 5 let center = UNUserNotificationCenter.current() 6 center.delegate = self 7}

投稿2018/06/17 14:11

_Kentarou

総合スコア8490

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

ebiryu

2018/06/17 17:45

ありがとうございました! 無事解決できました。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.49%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問