Skip to content

Instantly share code, notes, and snippets.

@uruly
Last active April 12, 2018 01:00
Show Gist options
  • Save uruly/2ba0f683668c66a66553d4c84fd6ed1b to your computer and use it in GitHub Desktop.
Save uruly/2ba0f683668c66a66553d4c84fd6ed1b to your computer and use it in GitHub Desktop.
LocalNotification
import UIKit
class AlertManager {
let id = "alert"
var text = "通知が来ているはずだよ"
//現在の時間から
func after(day:Int,hour:Int,minute:Int,seconds:Int) -> UILocalNotification{
//ローカル通知
let notification = UILocalNotification()
let currentDate = Date()
let pushDate = self.pushDate(date:currentDate, day: day,hour:hour,minute:minute,seconds:seconds)
//ロック中にスライドで〜〜のところの文字
notification.alertAction = "アプリを開く"
//通知の本文
notification.alertBody = self.text
//通知時刻
notification.fireDate = pushDate
//通知音
notification.soundName = UILocalNotificationDefaultSoundName
//アインコンバッジの数字
notification.applicationIconBadgeNumber = 1
//通知を識別するID
notification.userInfo = ["notifyID":self.id]
return notification
}
//通知をする時間を返す
func pushDate(date:Date,day:Int,hour:Int,minute:Int,seconds:Int) -> Date{
let current = Calendar.current.dateComponents([.hour,.minute,.second], from: date)
var interval = TimeInterval()
interval = Double(60 * 60 * 24 * day)
let nextDate = date.addingTimeInterval(interval)
var fireDateComponent = Calendar.current.dateComponents([.year,.month,.day], from: nextDate)
fireDateComponent.hour = hour + ( current.hour ?? 0 )
fireDateComponent.minute = minute + ( current.minute ?? 0 )
fireDateComponent.second = seconds + ( current.second ?? 0 )
return Calendar.current.date(from: fireDateComponent)!
}
}
import UIKit
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
//復帰したかどうか
if let notification = launchOptions?[UIApplicationLaunchOptionsKey.localNotification] as? UILocalNotification,let _ = notification.userInfo{
application.applicationIconBadgeNumber = 0
application.cancelLocalNotification(notification)
}
//復帰に関係なくバッジが0じゃなければ0にする
if application.applicationIconBadgeNumber != 0{
application.applicationIconBadgeNumber = 0
}
let notiSettings = UIUserNotificationSettings(types: [.alert,.badge,.sound], categories: nil)
application.registerUserNotificationSettings(notiSettings)
application.registerForRemoteNotifications()
return true
}
func applicationWillResignActive(_ application: UIApplication) {
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
// Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
}
func application(_ application: UIApplication, didReceive notification: UILocalNotification) {
//アプリがactive時に通知を発生させた時にも呼ばれる
if application.applicationState != .active{
//バッジを0にする
application.applicationIconBadgeNumber = 0
//通知領域から削除する
application.cancelLocalNotification(notification)
}else{
//active時に通知が来たときはそのままバッジを0に戻す
if application.applicationIconBadgeNumber != 0{
application.applicationIconBadgeNumber = 0
application.cancelLocalNotification(notification)
}
}
}
func applicationDidEnterBackground(_ application: UIApplication) {
//通知を全て消してから登録する
application.cancelAllLocalNotifications()
//とりあえず10秒後に発火
let alertManager = AlertManager()
let notification = alertManager.after(day:1, hour: 0, minute: 1, seconds: 10)
application.scheduleLocalNotification(notification)
}
func applicationWillEnterForeground(_ application: UIApplication) {
//アプリが起動されたらバッジを0にする
if application.applicationIconBadgeNumber != 0{
application.applicationIconBadgeNumber = 0
}
}
func applicationDidBecomeActive(_ application: UIApplication) {
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}
func applicationWillTerminate(_ application: UIApplication) {
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment