Skip to content

Instantly share code, notes, and snippets.

@takoikatakotako
Last active December 1, 2016 16:57
Show Gist options
  • Save takoikatakotako/7e6d540d473d228cece71ee522e8866d to your computer and use it in GitHub Desktop.
Save takoikatakotako/7e6d540d473d228cece71ee522e8866d to your computer and use it in GitHub Desktop.
UserNotificationsのサンプル
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
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment