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プッシュ受信の通知を表示する必要があり、いくつかの作業デバッグコードです

関連する問題