0

배경에서 내 앱을 시작하는 원격 푸시 알림 (VoIP PushKit)을받을 때 로컬 알림을 트리거하려고합니다.iOS 10 앱이 백그라운드에있을 때 로컬 알림을 실행할 수있는 방법이 있습니까?

내 로컬 알림은 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>>" 

그러나 5 초 후에 나타나지 않습니다. UNTimeIntervalNotificationTrigger를 사용합니다.

응용 프로그램이 포 그라운드 일 때 로컬 알림을 성공적으로 호출했습니다. 나는 버튼을 눌러서 트리거를 호출함으로써 같은 기능을 사용한다.

iOS 앱이 백그라운드에있을 때 로컬 알림을 실행할 수있는 방법이 있습니까?

답변

0

네, 그게 가능하고, 당신을 위해 그때 당신이 코드와 함께 뭔가 잘못하고 있어야합니다,하지만 당신은 그것을 보여주지 않았기 때문에 그것에 대해 언급 할 수 없습니다.

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)") 
    } 
: 여기

내가 VoIP를 푸시 수신에 알림을 표시해야 할 몇 가지 작업 디버깅 코드입니다