iOS Swift 应用程序在前台时接收推送通知
iOS Swift Receive Push Notification When App in Foreground
这是我在前台应用程序时用来接收推送通知的代码
@available(iOS 10.0, *)
func userNotificationCenter(center: UNUserNotificationCenter, willPresentNotification notification: UNNotification, withCompletionHandler completionHandler: (UNNotificationPresentationOptions) -> Void)
{
completionHandler([UNNotificationPresentationOptions.Alert,UNNotificationPresentationOptions.Sound,UNNotificationPresentationOptions.Badge])
}
但我的问题是我的通知包含 [NSObject : AnyObject] 值。如何接收点赞后台通知
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any]) {
if (application.applicationState == 0) // active --- forground
{
// publish your notification message
}
}
除非用户打开推送通知,否则您的应用将无法 运行 任何代码,推送通知由 OS 处理,您的应用无法控制它们活跃或在后台你应该看看https://developer.apple.com/library/content/documentation/Performance/Conceptual/EnergyGuide-iOS/OptimizeVoIP.html
UNNOtification 有 属性 请求(UNNotificationRequest),您可以使用它来获取用户信息。
使用以下方法从 UNNotification 访问用户信息:
let userinfo = notification.request.content.userInfo
在 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])
}
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)
}
这是我在前台应用程序时用来接收推送通知的代码
@available(iOS 10.0, *)
func userNotificationCenter(center: UNUserNotificationCenter, willPresentNotification notification: UNNotification, withCompletionHandler completionHandler: (UNNotificationPresentationOptions) -> Void)
{
completionHandler([UNNotificationPresentationOptions.Alert,UNNotificationPresentationOptions.Sound,UNNotificationPresentationOptions.Badge])
}
但我的问题是我的通知包含 [NSObject : AnyObject] 值。如何接收点赞后台通知
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any]) {
if (application.applicationState == 0) // active --- forground
{
// publish your notification message
}
}
除非用户打开推送通知,否则您的应用将无法 运行 任何代码,推送通知由 OS 处理,您的应用无法控制它们活跃或在后台你应该看看https://developer.apple.com/library/content/documentation/Performance/Conceptual/EnergyGuide-iOS/OptimizeVoIP.html
UNNOtification 有 属性 请求(UNNotificationRequest),您可以使用它来获取用户信息。
使用以下方法从 UNNotification 访问用户信息:
let userinfo = notification.request.content.userInfo
在 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])
}
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)
}