###前提・実現したいこと
swiftで日記アプリを作っています。
実現したいことは、アプリがバックグラウンド、もしくはプロセスが終了した状態でローカル通知を受けた時にバッジで1を表示し、ユーザーがアプリを開いたらバッジを消すことです。
###発生している問題・エラーメッセージ
以下でバッジを表示するコードを書いているつもりなのですが、
バックグラウンドの状態で通知が来てもバッジが表示されません。
通知の許可は得ている状態です。
notification.applicationIconBadgeNumber = 1
###該当のソースコード
こちらがLocalNotificationを設定している部分のコードです。
func scheduledLocalNotification(){ //まずは一度通知をキャンセルする UIApplication.sharedApplication().cancelAllLocalNotifications() //通知を設定する let notification:UILocalNotification = UILocalNotification() notification.alertAction = "アプリに戻る" //後で、ランダムにするように設定する。 notification.alertBody = "trimで今日1日を振り返りましょう!" notification.timeZone = NSTimeZone.defaultTimeZone() let realm = try!Realm() let remind = realm.objects(Reminder) let now = NSDate() let calendar = NSCalendar(identifier:NSCalendarIdentifierGregorian) let unit:NSCalendarUnit = [NSCalendarUnit.Year,NSCalendarUnit.Month,NSCalendarUnit.Day,NSCalendarUnit.Hour,NSCalendarUnit.Minute] let nowComps = calendar?.components(unit, fromDate: now) print("realmのデータ\(remind[0].Time)") let remindComps = calendar?.components(unit, fromDate: remind[0].Time!) nowComps?.calendar = calendar nowComps?.hour = (remindComps?.hour)! nowComps?.minute = (remindComps?.minute)! let remindDate = nowComps!.date //設定したreminDateが今よりも前ならば、 if now.compare(remindDate!) != .OrderedAscending{ //設定したリマインドがすでに過ぎていたら、1日加える。 nowComps!.day += 1 } print("リマインドする時間\(remindDate)") notification.fireDate = remindDate! //notification.fireDate = NSDate(timeIntervalSinceNow: 10) notification.soundName = "bgm_gameclear_2.mp3" notification.repeatInterval = .Day notification.applicationIconBadgeNumber = 1 UIApplication.sharedApplication().scheduleLocalNotification(notification)
###試したこと
バッジのつけ方を検索しコードの間違いを考えてみたのですが、発見できませんでした。
よろしくお願いします。
###補足情報(言語/FW/ツール等のバージョンなど)
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。