ios - 使用 OneSignal 在 iOS 中接收后台通知

标签 ios iphone swift onesignal

我正在使用 swift 创建一个 iOS 应用程序。我已经集成了用于推送通知的 onesignal SDK。现在我可以在应用程序处于前台时接收和管理通知。当应用程序处于后台时,我可以在通知面板中接收通知,但我无法在 appdelegate 中接收和管理它。在 appdelegate 中,当应用程序处于后台时,我在哪里可以接收通知?在这里我分享我的代码。我应该更改有效负载中的任何内容吗?

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

   let notificationReceivedBlock: OSHandleNotificationReceivedBlock = { notification in

      print("Received Notification: \(notification!.payload.notificationID)")
   }

   let notificationOpenedBlock: OSHandleNotificationActionBlock = { result in
      // This block gets called when the user reacts to a notification received
      let payload: OSNotificationPayload = result!.notification.payload

      var fullMessage = payload.body
      print("Message = \(fullMessage)")

      if payload.additionalData != nil {
         if payload.title != nil {
            let messageTitle = payload.title
               print("Message Title = \(messageTitle!)")
         }

         let additionalData = payload.additionalData
         if additionalData?["actionSelected"] != nil {
            fullMessage = fullMessage! + "\nPressed ButtonID: \(additionalData!["actionSelected"])"
         }
      }
   }

   let onesignalInitSettings = [kOSSettingsKeyAutoPrompt: false,
      kOSSettingsKeyInAppLaunchURL: true]

   OneSignal.initWithLaunchOptions(launchOptions, 
      appId: ONESIGNALAPP_I", 
      handleNotificationReceived: notificationReceivedBlock, 
      handleNotificationAction: notificationOpenedBlock, 
      settings: onesignalInitSettings)

   OneSignal.inFocusDisplayType = OSNotificationDisplayType.notification

   return true
}

最佳答案

您需要在 AppDelegate 中实现这些方法:

extension AppDelegate {

    @available(iOS 10.0, *)
    func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
        //use response.notification.request.content.userInfo to fetch push data
    }

    // for iOS < 10
    func application(_ application: UIApplication, didReceive notification: UILocalNotification) {
        //use notification.userInfo to fetch push data
    }

    func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
        //use userInfo to fetch push data
    }
}

关于ios - 使用 OneSignal 在 iOS 中接收后台通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52560770/

相关文章:

ios - Watchkit动态通知按钮自定义

iOS 谷歌地图 API 错误 :Your key may be invalid for your bundle ID:

ios - 填充 NSmutableDictionary 错误

iphone - iOS 上的 WebSocket

iphone - 有没有办法在未提交 Ad-Hoc 分发的设备上测试 iOS 应用程序?

iphone - iOS 开发 : What are some ways I can troubleshoot a lag issue in my game that occurs 15 - 30 minutes after playing it?

php - 推送通知发送成功,但设备收不到(偶尔)

ios - iOS 10.3 中的备用图标

swift - ipa 文件包含非法字符?

cocoa - 在 Swift 中将存储属性添加到 NSManagedObject 的子类