简体   繁体   English

当应用程序在后台时处理 iOS 推送通知

[英]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.

相关问题 当应用程序在 iOS 前台时如何处理 Flutter FCM 推送通知? - How to handle Flutter FCM push notification when app in foreground for iOS? iOS如何在应用程序处于前台时处理解析JSON推送通知 - iOS How to Handle Parse JSON Push Notification when App is in Foreground 当 iOS 应用程序被暂停/杀死并且用户点击通知时,如何处理 Firebase 推送通知? - How to handle Firebase push notification when iOS app is suspended/killed and user clicks on the notification? 当应用程序在后台或被杀死时处理令牌更改 - Handle token change when app is in backgroud or killed 用户强制退出应用程序时如何处理推送通知? [iOS] - How to handle push notification when app was force-quit by user? [iOS] 使用Sinch iOS SDK在后台运行应用程序时无法处理来电推送通知 - Can't handle incoming call push notification when app is in background with Sinch iOS SDK iOS应用程序在后台时的GCM推送通知 - GCM push notification when iOS app is in the background 应用程序被杀死时的 iOS 推送通知 - iOS Push Notification When app is killed 处于“未运行”状态时在iOS应用中推送通知 - Push notification in iOS app when “Not running” state 在应用程序运行且打开通知栏时处理推送通知 - Handle push notification when app is running and notification tray is opened
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM