2017-10-06 5 views
0

J'ai besoin d'aide pour terminer ce code afin que l'application affiche le numéro de badge quand une notification push est reçue, je peux recevoir une notification push, mais pas de badge sur l'application, voici le CodeComment mettre à jour le numéro de badge lorsque vous recevez une notification push

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) 
    } 
} 
+0

Vous pouvez suivre ce tutoriel https://eladnava.com/send-push-notifications-to-ios-devices-using-xcode-8-and-swift- 3/et https://stackoverflow.com/questions/44490391/swift-3-turn-off-badge-alert-sound – iPatel

Répondre

0

Il y a les deux méthodes sont appelées pour la notification Push

  1. Premier plan Méthode
  2. Méthode Contexte

1.Méthode de fond.

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

obtenir le contenu de la demande de notification

let content = notification.request.content 

2.Background Méthode

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

Obtenez le contenu de la demande de notification

let content = response.notification.request.content 

Obtenir un badge comte

let badge = content.badge as! Int 
0

Suivez ces étapes pour configurer le nombre de badges dans iOS:

logique 1.Mettre en œuvre pour calculer le nombre de back-end et envoyer ce que ça compte par notification Push comme:

{ "aps" : { "alert" : "You got your Push Message.", "badge" : 9 } }

2. Nombre de badges obtenu à partir de la réponse de notification Push:

let pushContent = response.notification.request.content 
let badgeCount = content.badge as! Int 

nombre de badge 3.Réglez dans la méthode didReceiveRemoteNotification:

UIApplication.sharedApplication().applicationIconBadgeNumber = badgeCount