0

J'essaie de déclencher une notification locale lorsque je reçois une notification push à distance (VoIP PushKit) qui démarre mon application en arrière-plan.Est-il possible de déclencher des notifications locales lorsque l'application iOS 10 est en arrière-plan?

Je vois ma notification locale dans le tableau des notifications en attente au UNUserNotificationCenter.

[[UNUserNotificationCenter currentNotificationCenter] getPendingNotificationRequestsWithCompletionHandler: 
^(NSArray<UNNotificationRequest *> * _Nonnull requests) 
{ 
    NSLog(@"Local notifications: %@",requests); 
}]; 


"<UNNotificationRequest: 0x17162e500; identifier: 2A364020-3BA2-481B-9255-16EBBF2F4484, content: <UNNotificationContent: 0x1708e8080; title:test, subtitle: (null), body: test2, categoryIdentifier: missed, launchImageName: , peopleIdentifiers: (\n), threadIdentifier: , attachments: (\n), badge: 1, sound: <UNNotificationSound: 0x1704bfe60>, hasDefaultAction: YES, shouldAddToNotificationsList: YES, shouldAlwaysAlertWhileAppIsForeground: YES, shouldLockDevice: YES, shouldPauseMedia: NO, isSnoozeable: NO, fromSnooze: NO, darwinNotificationName: (null), darwinSnoozedNotificationName: (null), trigger: <UNTimeIntervalNotificationTrigger: 0x171624260; repeats: NO, timeInterval: 5.000000>>" 

Mais cela n'apparaît pas après 5 sec. J'utilise UNTimeIntervalNotificationTrigger.

J'ai réussi à appeler la notification locale lorsque l'application est au premier plan. J'utilise la même fonction pour le déclenchement en l'appelant en appuyant sur le bouton.

Y at-il un moyen de déclencher une notification locale lorsque l'application iOS est en arrière-plan?

Répondre

0

Oui, c'est possible, et si cela ne fonctionne pas pour vous, alors vous devez faire quelque chose d'incorrect avec votre code, mais vous ne l'avez pas montré, donc vous ne pouvez pas le commenter.

est ici un code de débogage de travail que je dois afficher une notification sur réception push voip:

func pushRegistry(_ registry: PKPushRegistry, didReceiveIncomingPushWith payload: PKPushPayload, forType type: PKPushType) 
    { 
     registerBackgroundTask() 
     //present a local notifcation to visually see when we are recieving a VoIP Notification 
     if UIApplication.shared.applicationState == UIApplicationState.background { 
      let notification = UNMutableNotificationContent() 
      notification.title  = "Voip Push" 
      notification.body  = "App in background" 
      let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 1, repeats: false) 
      let request = UNNotificationRequest(identifier: "id", content: notification, trigger: trigger) 
      DispatchQueue.main.async { 
       UNUserNotificationCenter.current().add(request) 
       { (error) in 
        if error != nil 
        { 
         let e = error as? NSError 
         NSLog("Error posting notification \(e?.code) \(e?.localizedDescription)") 
        } 
       } 
      } 
     } 

     else 
     { 
      let notification = UNMutableNotificationContent() 
      notification.title  = "Voip Push" 
      notification.body  = "App in foreground" 
      let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 1, repeats: false) 
      let request = UNNotificationRequest(identifier: "id", content: notification, trigger: trigger) 
      DispatchQueue.main.async { 
       UNUserNotificationCenter.current().add(request) 
       { (error) in 
        if error != nil 
        { 
         let e = error as? NSError 
         NSLog("Error posting notification \(e?.code) \(e?.localizedDescription)") 
        } 
       } 
      } 
     } 

     NSLog("incoming voip notfication: \(payload.dictionaryPayload)") 
    }