[英]Handle iOS Push Notification when app in backgroud
I have to handle push when the app in background / running.当应用程序在后台/运行时,我必须处理推送。 I'm using firebase and native push services.
我正在使用 firebase 和本机推送服务。 Setting up Capability - Background Fetch .
设置功能-后台获取。 I'm receiving push's all and I can open app taping on push view when the app in the background and handle push when the app is running.
我正在接收推送,当应用程序在后台运行时,我可以在推送视图上打开应用程序录音,并在应用程序运行时处理推送。 But I have to implement follow:
但我必须执行以下操作:
When app running.应用程序运行时。
Receive push, show alert, logout user.接收推送,显示警报,注销用户。 Works!
作品!
When app in background / closed.当应用程序在后台/关闭时。 Open app from the push.
从推送中打开应用程序。
Handling this action in didReceiveRemoteNotification method.在didReceiveRemoteNotification方法中处理此操作。 Works!
作品!
When app in background / closed.当应用程序在后台/关闭时。 When open app from the app icon.
从应用程序图标打开应用程序时。
Handling this action in didReceiveRemoteNotification method.在didReceiveRemoteNotification方法中处理此操作。 Doesn't work!
不起作用! .
.
How can I get push When app in background / closed .当应用程序在后台/关闭时如何获得推送。 Save data (ex. badge count or push can in AppDelegate variable).
保存数据(例如徽章计数或推送可以在 AppDelegate 变量中)。 And get this in applicationDidBecomeActive method?
并在applicationDidBecomeActive方法中得到这个?
to handle push notifications you need to implement two methods, one is called when the app is closed, and one is called when app is in foreground.要处理推送通知,您需要实现两种方法,一种在应用程序关闭时调用,一种在应用程序处于前台时调用。
extension AppDelegate: UNUserNotificationCenterDelegate {
/// Called when the app receive the notification in foreground
/// - Parameter center: <#center description#>
/// - Parameter notification: <#notification description#>
/// - Parameter completionHandler: <#completionHandler description#>
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (_ options: UNNotificationPresentationOptions) -> Void) {
let userInfo = notification.request.content.userInfo
// Get aps object
let aps = userInfo["aps"] as! [String : Any]
// Do your stuff here
// This line is NEEDED, otherwise push will not be fired
completionHandler([.alert, .badge, .sound]) // Display notification depending on your needed
}
/// Detect when user tap on a push message when app is closed
/// - Parameter center: <#center description#>
/// - Parameter response: <#response description#>
/// - Parameter completionHandler: <#completionHandler description#>
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
let userInfo = response.notification.request.content.userInfo
// Get aps object
let aps = userInfo["aps"] as! [String : Any]
// Do your stuff here
// This line is NEEDED, otherwise push will not be fired
completionHandler()
}
}
And in your didFinishLaunchingWithOptions
set your AppDelegate as delegate:并在您的
didFinishLaunchingWithOptions
中将您的 AppDelegate 设置为委托:
// Set AppDelegate as UNUserNotificationCenterDelegate
UNUserNotificationCenter.current().delegate = self
Hope this can help :-)希望这可以帮助:-)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.