前提・実現したいこと
勉強の為ローカル通知を学習していたのですが、通知許可は出るのですが肝心の通知が出ません。
発生している問題・エラーメッセージ
エラーは出ないのですが、シミュレーターでビルドしても通知が出ません。
該当のソースコード
import UIKit // UserNotificationsをimport import UserNotifications @UIApplicationMain class AppDelegate: UIResponder, UIApplicationDelegate { var window: UIWindow? func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { let center = UNUserNotificationCenter.current() center.requestAuthorization(options: [.alert, .badge, .sound]) { (granted, error) in if granted { print("許可されました") } else { print("許可されませんでした") } } return true }
import UIKit // UserNotificationsをimport import UserNotifications class ViewController: UIViewController { @IBAction func okButton(_ sender: Any) { // 通知を設定 let content = UNMutableNotificationContent() content.title = "タイトル" content.body = "こんにちは" content.sound = UNNotificationSound.default() let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 5, repeats: false) let request = UNNotificationRequest(identifier: "Identifier", content: content, trigger: trigger) let center = UNUserNotificationCenter.current() center.add(request) }
試したこと
最初に試した頃、「繰り返し通知する場合は60秒以上開けてください」のようなログがあったので、調べたところ、
let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 5, repeats: false)
のrepeatsの所がtrueになっていたので、falseにした所、「繰り返し通知する場合は60秒以上開けてください」のようなエラーのログがなくなったのですが、
通知が出なくなってしまいました。解決方法を調べていますが、今現在解決していません。
初心者なので質問の情報に至らない点がある場合があるかと思います。すみません。もし至らない点があれば迅速に情報を追加します。
よろしくお願いします。
補足情報(FW/ツールのバージョンなど)
Swift4です。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/09/01 12:56
2018/09/02 09:52
2018/09/05 05:42