Skip to content

UserNotification(LocalNotification)

   

UserNotification is used to issue a notification to a user.
There are two main types of UserNotifications, RemoteNotification (notifications like Line and Twitter) and LocalNotification (notifications that are issued internally, like clock apps).
This section deals with LocalNotification.

UserNotification sample

First of all, the sound data (.caf file) to be used for issuing the original notification sound is registered in the project.
Add the .caf file to your project and make sure that it is registered with BundleResouces.
Here's the CAF file I also used.
As a specification of UserNotification, if the original notification sound used is defective, the default notification sound will be played back.

alt

import UIKit
import UserNotifications
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
//通知の許可を出す
let center = UNUserNotificationCenter.current()
center.requestAuthorization(options: [.alert, .sound]) { (granted, error) in
}
// MARK: - 10秒後に着火するボタン
let tenSecondBtn = UIButton()
tenSecondBtn.frame = CGRect(x: 50, y: 50, width: 250, height: 40)
tenSecondBtn.backgroundColor = UIColor.gray
tenSecondBtn.addTarget(self, action: #selector(tenSecondBtnClicked(sender:)), for:.touchUpInside)
tenSecondBtn.setTitle("10秒後に通知", for: UIControlState.normal)
tenSecondBtn.setTitleColor(UIColor.white, for: UIControlState.normal)
self.view.addSubview(tenSecondBtn)
// MARK: - 時間日時をしてして通知する
let setDayAndTimeBtn = UIButton()
setDayAndTimeBtn.frame = CGRect(x: 50, y: 120, width: 250, height: 40)
setDayAndTimeBtn.backgroundColor = UIColor.gray
setDayAndTimeBtn.addTarget(self, action: #selector(setDayAndTimeBtnClicked(sender:)), for:.touchUpInside)
setDayAndTimeBtn.setTitle("日時と時間を指定して通知", for: UIControlState.normal)
setDayAndTimeBtn.setTitleColor(UIColor.white, for: UIControlState.normal)
self.view.addSubview(setDayAndTimeBtn)
// MARK: - 通知音を変更する
let setOriginalSoundBtn = UIButton()
setOriginalSoundBtn.frame = CGRect(x: 50, y: 190, width: 250, height: 40)
setOriginalSoundBtn.backgroundColor = UIColor.gray
setOriginalSoundBtn.addTarget(self, action: #selector(setOriginalSoundBtnClicked(sender:)), for:.touchUpInside)
setOriginalSoundBtn.setTitle("オリジナルの通知音を設定", for: UIControlState.normal)
setOriginalSoundBtn.setTitleColor(UIColor.white, for: UIControlState.normal)
self.view.addSubview(setOriginalSoundBtn)
}
//10秒後に着火するボタンが終われたら呼ばれる
internal func tenSecondBtnClicked(sender: UIButton){
// Notificatiのインスタンス生成
let content = UNMutableNotificationContent()
// タイトルを設定する
content.title = "ここがタイトルです"
// 通知の本文です
content.body = "ここが通知の本文です。ここが通知の本文です。ここが通知の本文です。"
// デフォルトの音に設定します
content.sound = UNNotificationSound.default()
// Triggerを生成(いつ通知が来るのか)
let trigger = UNTimeIntervalNotificationTrigger.init(timeInterval: 10, repeats: false)
// Requestを生成する。idには通知IDを設定する
let request = UNNotificationRequest.init(identifier: "ID_TenSecond", content: content, trigger: trigger)
// Noticationを生成する
let center = UNUserNotificationCenter.current()
center.add(request) { (error) in
}
}
//時間を指定して着火するボタンが終われたら呼ばれる
internal func setDayAndTimeBtnClicked(sender: UIButton){
// Notificatiのインスタンス生成
let content = UNMutableNotificationContent()
// タイトルを設定する
content.title = "ここがタイトルです"
// 通知の本文です
content.body = "ここが通知の本文です。ここが通知の本文です。ここが通知の本文です。"
// デフォルトの音に設定します
content.sound = UNNotificationSound.default()
//着火時間の設定
//以下の例では午前1時25分0秒に設定しています
var dateComponents = DateComponents()
dateComponents.hour = 1
dateComponents.minute = 30
dateComponents.second = 0
let calendarTrigger = UNCalendarNotificationTrigger(dateMatching: dateComponents, repeats: false)
// Requestを生成する。idには通知IDを設定する
let request = UNNotificationRequest.init(identifier: "ID_SetDayAndTime", content: content, trigger: calendarTrigger)
// Noticationを発行する.
let center = UNUserNotificationCenter.current()
center.add(request) { (error) in
print(error ?? "aa")
}
}
//オリジナルの通知音に設定ボタンが押されたら動く(10秒後に着火する)
internal func setOriginalSoundBtnClicked(sender: UIButton){
// Notificatiのインスタンス生成
let content = UNMutableNotificationContent()
// タイトルを設定する
content.title = "ここがタイトルです"
// 通知の本文です
content.body = "ここが通知の本文です。ここが通知の本文です。ここが通知の本文です。"
// オリジナルの音を設定する
content.sound = UNNotificationSound.init(named: "sampleSound.caf")
// Triggerを生成(いつ通知が来るのか)
let trigger = UNTimeIntervalNotificationTrigger.init(timeInterval: 10, repeats: false)
// Requestを生成する。idには通知IDを設定する
let request = UNNotificationRequest.init(identifier: "ID_TenSecond", content: content, trigger: trigger)
// Noticationを生成する
let center = UNUserNotificationCenter.current()
center.add(request) { (error) in
}
}
}

  1. Speech Recognition(SFSpeechRecognizer)
  2. UITextField
  3. Creating a CAF file
  4. UINavigationBar
  5. UILabel
  6. Obtaining the current time
  7. UITableView