0

Ich versuche lokale Benachrichtigung auszulösen, wenn ich Remote-Push-Benachrichtigung (VoIP PushKit) bekomme, die meine App im Hintergrund starten.Gibt es eine Möglichkeit, lokale Benachrichtigungen auszulösen, wenn sich die iOS 10 App im Hintergrund befindet?

Ich sehe meine lokale Benachrichtigung im Array der ausstehenden Benachrichtigungen im 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>>" 

Aber es erscheint nicht nach 5 sek. Ich benutze UNTimeIntervalNotificationTrigger.

Ich habe erfolgreich die lokale Benachrichtigung aufgerufen, wenn die Anwendung im Vordergrund ist. Ich benutze dieselbe Funktion für den Trigger, indem ich sie durch Drücken der Taste aufrufen.

Gibt es eine Möglichkeit, lokale Benachrichtigungen auszulösen, wenn die iOS App im Hintergrund läuft?

Antwort

0

Ja, es ist möglich, und wenn es nicht für Sie funktioniert, dann müssen Sie etwas falsch mit Ihrem Code tun, aber Sie haben es nicht gezeigt, so kann ich das nicht kommentieren.

Hier einige Arbeits Debug-Code, den ich eine Benachrichtigung auf VoIP Push-Empfangs angezeigt haben:

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)") 
    } 
Verwandte Themen