Skip to content

Instantly share code, notes, and snippets.

@akimach
Last active June 24, 2016 17:17
Show Gist options
  • Select an option

  • Save akimach/11025ad528b9737f9593da634daeb439 to your computer and use it in GitHub Desktop.

Select an option

Save akimach/11025ad528b9737f9593da634daeb439 to your computer and use it in GitHub Desktop.
UILocalNotificationメモ

UserNotification

端末内か端末外から通知を送るかの違いだけで、アプリ側のハンドリングは同じ

  • ローカル通知
  • プッシュ通知
  • Remote Notification

通知の許可を要求する

application.registerUserNotificationSettings(UIUserNotificationSettings(forTypes: [
            UIUserNotificationType.Sound,
            UIUserNotificationType.Badge,
            UIUserNotificationType.Alert,
            ], categories: nil))
// 通知の作成
let notification = UILocalNotification()
// ロック中に通知された時に表示される文字列
notification.alertAction = "即座にもどる"
// 通知の本文
notification.alertBody = "早く戻ってくればいい…"
notification.fireDate = NSDate(timeIntervalSinceNow: 10)
// 通知の音を鳴らす
// 独自の音を鳴らすことが可能
notification.soundName = UILocalNotificationDefaultSoundName
// アプリのアイコンにバッジを表示する
notification.applicationIconBadgeNumber = 1
// 識別用
notification.userInfo = [
    "notifyId" : "通知を識別できる文字列など",
]
// 通知をスケジュールする
application.scheduleLocalNotification(notification)

通知からの復帰を検出する

  1. アプリがバックグランド状態からの復帰
  2. アプリが起動していない状態からの復帰

UIApplicationDelegateプロトコル``を実装する

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment