###前提・実現したいこと
かなりハマっています。。。どなたか知恵をお貸しくださいませ。
iPhoneのwebviewアプリ開発をしております。
サーバー側はrailsのgem'rpush'を利用してプッシュ通知・バッジ通知を実装しています。
プッシュ通知は実装完了したのですが、バッジ通知がうまくいっていません。
swift側の処理は下記のように記載をしているのですが、こちらが動作していないようです。
プッシュ通知は来るのですがバッジ数が増加しません。
その原因がわからず本当に困っています。
###発生している問題・エラーメッセージ
アプリがバックグラウンドにいる際にバッジ数が加算されない。
テストでフォアグラウンドにいる際にバッジ数加算の処理を記載したら、バッジ数は1,2,3,4と増えました。
###該当のソースコード
■rails側 def notificate_for_ios notification = Rpush::Apns::Notification.new notification.app = app_for_ios notification.device_token = terminal_number notification.alert = '新規の投稿が追加されました。' notification.data = { foo: :bar } notification.badge = 1 notification end ■swift側 ◯バックグラウンド時にバッジ通知を送ろうしている箇所(AppDelegate内) func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void){ UIApplication.shared.applicationIconBadgeNumber += 1 } ◯devicetokenをサーバーに送る処理を書いている箇所(AppDelegate内) func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) { var deviceToken = String(format: "%@", deviceToken as CVarArg) as String print("deviceToken = \(deviceToken)") let characterSet: CharacterSet = CharacterSet.init(charactersIn: "<>") deviceToken = deviceToken.trimmingCharacters(in: characterSet) deviceToken = deviceToken.replacingOccurrences(of: " ", with: "") deviceToken = deviceToken sendToken(token: deviceToken) print("deviceToken = \(deviceToken)") } func sendToken(token: String){ let serverRails = "http://hogehoge.co.jp/api/v1/device_token" let postString = "DeviceToken=" + token var request = URLRequest(url: URL(string: serverRails)!) request.httpMethod = "POST" request.httpBody = postString.data(using: .utf8) let task = URLSession.shared.dataTask(with: request, completionHandler: { (data, response, error) in if error != nil { print(error) return } print("response: \(response!)") }) task.resume() } ◯プッシュ通知許可のダイアログを表示している箇所(viewcontorller内) UNUserNotificationCenter.current().requestAuthorization( options: [.badge, .alert, .sound]) {(accepted, error) in if accepted { print("Notification access accepted !") DispatchQueue.main.async { UIApplication.shared.registerForregisters() } } else{ print("Notification access denied.") } }
どなたかお分かりの方、助けていただけませんでしょうか。よろしくお願いいたします。