如何打开一个特定的视图控制器在应用程序恢复时,在didReceiveRemoteNotification上
我正在实施一个警报,我从服务器得到pushNotification,我收到完美的推送通知,它在前台模式下工作正常,但是当应用程序在后台进入时,它只获取推送通知,但不加载视图,我想加载
请检查下面的代码
func registerForPushNotifications(application: UIApplication) { let notificationSettings = UIUserNotificationSettings( forTypes: [.Badge, .Sound, .Alert], categories: nil) application.registerUserNotificationSettings(notificationSettings) }
此方法从didFinishLaunchingWithOptions
调用
func application(application: UIApplication, didRegisterUserNotificationSettings notificationSettings: UIUserNotificationSettings) { if notificationSettings.types != .None { application.registerForRemoteNotifications() } } func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) { let tokenChars = UnsafePointer<CChar>(deviceToken.bytes) var tokenString = "" for i in 0..<deviceToken.length { tokenString += String(format: "%02.2hhx", arguments: [tokenChars[i]]) } NSUserDefaults.standardUserDefaults().setObject(tokenString, forKey: "deviceToken") }
这是最后的方法
func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) { print(userInfo) let storyboard = UIStoryboard(name: "Main", bundle: nil) let navigationController = storyboard.instantiateViewControllerWithIdentifier("AlarmDetailsController") as! AlarmDetailsController //let dVC:AlarmDetailsController = navigationController.topViewController as! AlarmDetailsController navigationController.isPushNotification = true self.window?.rootViewController?.presentViewController(navigationController, animated: true, completion: {}) }
请帮我解决这个问题记住我的应用程序在前台模式下工作正常
1.首先,您应该在应用程序“function”中的后台获取2.然后在应用程序委托中使用以下代码
在AppDelegate类中添加以下代码:
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) { // print(userInfo) let vc = mainStoryBoard.instantiateViewController(withIdentifier: "destinationVC") as! destinationVC self.visibleNavController.pushViewController(vc, animated: true) }
对于iOS 10,请使用以下代码:1.导入
import UserNotifications
为了前景获取
@available(iOS 10.0, *) func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (_ options: UNNotificationPresentationOptions) -> Void) { var userInfo = NSDictionary() userInfo = notification.request.content.userInfo as NSDictionary let pay = userInfo as NSDictionary let driverLocationVC = mainStoryBoard.instantiateViewController(withIdentifier: "destinationVC") as! destinationVC self.visibleNavController.pushViewController(driverLocationVC, animated: true) }
为背景
@available(iOS 10.0, *) func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) { print("Userinfo \(response.notification.request.content.userInfo)") var userInfo = NSDictionary() userInfo = response.notification.request.content.userInfo as NSDictionary print(userInfo) let driverLocationVC = mainStoryBoard.instantiateViewController(withIdentifier: "DriverLocationVC") as! DriverLocationVC self.visibleNavController.pushViewController(driverLocationVC, animated: true) }
用于设备令牌提取
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) { let tokenString = deviceToken.reduce("", {$0 + String(format: "%02X", $1)}) print("Got token data! \(tokenString)") UserDefaults.standard.set(tokenString, forKey: "device_token") UserDefaults.standard.synchronize() }
如果您的应用程序被挂起,请从application:didFinishLaunchingWithOptions
的字典中检查UIApplicationLaunchOptionsRemoteNotificationKey
application:didFinishLaunchingWithOptions
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { ... // Check if launched from notification if let userInfo = launchOptions?[UIApplicationLaunchOptionsRemoteNotificationKey] as? [String: AnyObject] { // handle your notification like in application:didReceiveRemoteNotificatioUserInfo: } ... }
首先你需要在你的Xcode中设置
有三种状态: 前景,背景,杀死 ,你需要知道你的通知明确的细节。
1.前景 :当您的应用程序处于前台时,一旦您的通知进入,当您在正常模式下在横幅或警报基础上点击您的configuration时,它将导致特定的屏幕。 但是,在后台模式下,只要您的通知到达,甚至可以默默导航,而不用点击它。
2.背景 :如果您的应用程序不在前台,使用后台模式可以帮助您打开特定的屏幕,如果您在didReceiveRemoteNotification
处理它,并且一旦应用程序打开,当您点击它时,它将导致它的特定屏幕。
3.杀死 :如果你的应用程序不在内存中,只有点击推送通知工作才能浏览特定屏幕,因为它不在设备内存中,所以如果用户忽略通知,则不会触发didReceiveRemoteNotification
。
那么,我们如何处理特定的屏幕导航呢? 这是基于你如何开发你的应用程序devise,我不能提供正确的答案,直到我看到你的代码。
但是,是的,只要您通过点击横幅或locking屏幕上显示的通知打开应用程序时,您可以导航到特定的应用程序在后台。当您想要在后台执行它时,一切都可以在didReceiveRemoteNotification
内完成。
看看这个 :
在您的推送通知json中,您必须包含以下内容
content-available : 1
当它被包含时,它甚至可以根据从服务器发送的附加数据在后台执行后台执行。
在您的额外数据:
{apns : [], data : [ "Navigate" : "Home" ]}
然后在您的didReceiveRemoteNotification
,
if let aps = userInfo["data"] as? NSDictionary{ // get the "Home" data and do navigation here // You might need to instantiate storyboard of the specific view controller which you want to go as RootViewController }
所以,当你的通知来了,它会导致在后台的具体屏幕,只要你点击推送通知从横幅或alert.The应用程序将打开,并将导致具体的屏幕。