【Swift】UserNotification(ローカル通知)の使い方
完成図
↓destination == "Ken"の時
解説
※注意点
・今回はバックグラウンドでの実装
・”送信する”を押して、すぐにアプリを閉じないと、ローカル通知が発動しない
・発動すれば、画面上部 & 通知センターに表示される
ストーリーボード
ソースコード
import UIKit import UserNotifications class ViewController: UIViewController, UITextFieldDelegate { @IBOutlet weak var nameTextField: UITextField! @IBOutlet weak var honbunTextField: UITextView! //送り先を受け取る var destination = "" //本文を受け取る var message = "" //identifier用の変数 let NotificationIdentifier = "ID" override func viewDidLoad() { super.viewDidLoad() //デリゲートを取得 nameTextField.delegate = self } @IBAction func send(_ sender: Any) { //送り先を代入する destination = nameTextField.text! //本文を代入する message = honbunTextField.text! //メッセージを送信する gopush() } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } //ローカル通知の許可等 func gopush() { UNUserNotificationCenter.current().getNotificationSettings { (settings) in //もし通知が許可されているなら if settings.authorizationStatus == .authorized { //通知 self.push() //もし通知が許可されてないなら } else { UNUserNotificationCenter.current().requestAuthorization( options: [.sound,.badge,.alert],completionHandler: { (granted, error) in if let error = error { print(error) } else { if (granted) { self.push() } } }) } } } //ローカル通知内での処理 func push() { //指定した文章と同じかどうか if message == "" { //UserNotificationのインスタンス let content = UNMutableNotificationContent() //送り主で条件分岐 switch destination { case "Amy": //メッセージのタイトル content.title = "All right!!" //メッセージの本文 content.body = "I will put on my favorite clothes!" //通知に貼り付ける画像のURL if let url = Bundle.main.url(forResource: "Amy", withExtension: "png"),let attach = try? UNNotificationAttachment(identifier: NotificationIdentifier, url: url, options: nil) { content.attachments.append(attach) } case "Karen": content.title = "OK!" content.body = "I'm looking forward to watching a movie with everyone!" if let url = Bundle.main.url(forResource: "Karen", withExtension: "png"), let attach = try? UNNotificationAttachment(identifier: NotificationIdentifier, url: url, options: nil) { content.attachments.append(attach) } case "Ken": content.title = "Oh Yes!" content.body = "Let's release the stress accumulated in the exam tomorrow at once!" if let url = Bundle.main.url(forResource: "Ken", withExtension: "png"), let attach = try? UNNotificationAttachment(identifier: NotificationIdentifier, url: url, options: nil) { content.attachments.append(attach) } case "Mike": content.title = "I want to go early" content.body = "Finally I can get out of boring lessons and I can not wait for tomorrow's event" if let url = Bundle.main.url(forResource: "Mike", withExtension: "png"), let attach = try? UNNotificationAttachment(identifier: NotificationIdentifier, url: url, options: nil) { content.attachments.append(attach) } default: content.title = "It is a mistake email." content.body = "Who are you? It is fine if sales" } //5秒後に通知 let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 3.0, repeats: false) //トリガーと内容を内包 let notificationrequest = UNNotificationRequest(identifier: NotificationIdentifier, content: content, trigger: trigger) //エラー処理 UNUserNotificationCenter.current().add(notificationrequest) { (error) in if let error = error { print(error) } else { print("通知されました") } } } else { print("もう一度やり直してください。") } } }