【问题标题】:iOS Swift Receive Push Notification When App in Foreground当应用程序在前台时,iOS Swift 会收到推送通知
【发布时间】:2017-05-11 11:10:34
【问题描述】:

这是我用来在前台应用时接收推送通知的代码

@available(iOS 10.0, *)
func userNotificationCenter(center: UNUserNotificationCenter, willPresentNotification notification: UNNotification, withCompletionHandler completionHandler: (UNNotificationPresentationOptions) -> Void)
{
     completionHandler([UNNotificationPresentationOptions.Alert,UNNotificationPresentationOptions.Sound,UNNotificationPresentationOptions.Badge])
}

但我的问题是我的通知包含 [NSObject : AnyObject] 值。如何接收点赞后台通知

【问题讨论】:

  • 你想从 Notification 对象访问用户信息吗?
  • @iOS_devloper 是的先生
  • 我正在使用“notification.request.content.userInfo”来获取数据。你可以用上面的方法打印出来。
  • 你可以使用 notification.request.content.userInfo
  • @iOS_devloper 如果它解决了该帖子,您应该将其添加为答案(如果您有它们,请加上更多细节)。干杯!

标签: ios push firebase-notifications unusernotificationcenter


【解决方案1】:
  func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void)
{
    debugPrint("Received when is visible... :): \(userInfo)")
    

    guard let aps = userInfo["aps"] as? [String: AnyObject] else {
        completionHandler(.failed)
        return
      }
    
    let alert:Dictionary<String,String> = aps["alert"] as! Dictionary<String,String>

    let title:String = alert["title"]!
    let msg:String   = alert["body"]!
    
  
    let refreshAlert = UIAlertController(title: title, message: msg, preferredStyle: UIAlertController.Style.alert)
    
    refreshAlert.addAction(UIAlertAction(title: "Ok", style: .default, handler: { (action: UIAlertAction!) in
         
        refreshAlert.dismiss(animated: true, completion: nil)
      
    }))


    UIApplication.shared.windows.first!.rootViewController?.present(refreshAlert, animated: true, completion: nil)
 
}

【讨论】:

    【解决方案2】:

    将此代码添加到 AppDelegate.swift 文件中。

    @available(iOS 10.0, *)
    func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void)
    {
        completionHandler([UNNotificationPresentationOptions.alert,UNNotificationPresentationOptions.sound,UNNotificationPresentationOptions.badge])
    }
    

    【讨论】:

    • 如何在 iOS 8.0 中编写这个@Kavin Kumar Arumugam
    • @DilipTiwari 上面代码没有变化,已经支持iOS 8.0
    • 好的,所以它也会在前台显示通知@Kavin Kumar Arumugam?
    • 你能告诉我你的firebase版本@DilipTiwari
    • 我在 ios 10 中接收,但在 ios 8 中没有 @Kavin Kumar Arumugam
    【解决方案3】:

    UNNOtification 有属性请求 (UNNotificationRequest),您可以使用它来获取用户信息。

    使用以下命令从 UNNotification 访问用户信息:

    let userinfo =  notification.request.content.userInfo
    

    【讨论】:

      【解决方案4】:

      除非用户打开推送通知,否则您的应用程序将无法运行任何代码,推送通知由操作系统处理,并且您的应用程序在它不活动或在后台时无法控制它们,您应该看看https://developer.apple.com/library/content/documentation/Performance/Conceptual/EnergyGuide-iOS/OptimizeVoIP.html

      【讨论】:

        【解决方案5】:
         func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any]) {
        
           if (application.applicationState == 0) // active --- forground
            {
               // publish your notification message
            }
        
        }
        

        【讨论】:

          猜你喜欢
          • 2020-12-23
          • 2019-07-24
          • 1970-01-01
          • 2014-10-21
          • 1970-01-01
          • 2017-06-26
          • 2013-01-30
          • 1970-01-01
          相关资源
          最近更新 更多