2

プッシュ通知を受信したときにアプリにバッジ番号が表示されるように、このコードを完成させるのに助けが必要です。プッシュ通知を受信できますが、アプリにバッジがありません。コードは次のとおりです

 func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any],
 fetchCompletionHandler completionHandler: @escaping
 (UIBackgroundFetchResult) -> Void) {

if let aps = userInfo["aps"] as? NSDictionary, let _ = aps["alert"] as?   String, let sound = aps["sound"] as? String
         {

             if let systemSound = SystemSoundID(sound) {
             AudioServicesPlayAlertSound(systemSound)
         }

         NotificationCenter.default.post(name: Notification.Name(rawValue: SystemSetting.refreshCouponListNotification), object: nil)

         completionHandler(UIBackgroundFetchResult.newData)
     }
 }
4

2 に答える 2

3

プッシュ通知には 2 つのメソッドが呼び出されます

  1. フォアグラウンド メソッド
  2. バックグラウンド メソッド

1.フォアグラウンド方式。

    func userNotificationCenter(_ center: UNUserNotificationCenter, 
               willPresent notification: UNNotification,withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void)

届出書の内容を取得する

let content = notification.request.content

2.背景法

func userNotificationCenter(_ center: UNUserNotificationCenter, 
                    didReceive response: UNNotificationResponse, 
withCompletionHandler completionHandler: @escaping () -> Void)

届出書の内容を取得する

let content = response.notification.request.content

バッジ数を取得

let badge = content.badge as! Int
于 2017-10-06T04:56:40.200 に答える