我正在实现一个警报,我从服务器获取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: {})
}
请帮我解决这个问题请记住我的应用程序在前台模式下正常工作
答案 0 :(得分:5)
1.首先,您应该在应用程序“功能”中启用后台提取 2.然后在app delegate中使用以下代码
在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()
}
答案 1 :(得分:2)
如果您暂停了应用,请检查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:
}
...
}
答案 2 :(得分:0)
首先,您需要在Xcode中设置如下
有三种状态:前景,背景,已杀/ ,您需要了解有关通知的明确详细信息。
1。前景:当您的应用处于前台时,当您的通知进入时,当您点击横幅或警报基础时,它会指向特定屏幕在正常模式下配置。但是,在后台模式下,它甚至可以在您的通知到达时立即进行静音导航,而无需点击它。
2。背景:如果您的应用不在前台,使用后台模式可以帮助您打开特定屏幕,如果您在didReceiveRemoteNotification
内处理它只要应用程序在您点按它时打开,它就会显示它的特定屏幕。
3。已杀死:如果您的应用不在内存中,则仅点击推送通知工作以导航特定屏幕,因为它不在设备内存中,因此{如果用户忽略通知,则不会触发{1}}。
那么,我们如何处理特定的屏幕导航?这取决于您如何开发应用程序设计,在看到您的代码之前,我无法提供正确的答案。
但是,是的,当您通过点击横幅或锁定屏幕上显示的通知打开应用时,您可以导航到应用处于后台的特定内容。在didReceiveRemoteNotification
内,一切都可以在一个条件下完成你想在后台做这件事。
看看这个:
在推送通知json中,您必须包含以下内容
didReceiveRemoteNotification
当包含它时,它可以执行后台执行,即使应用程序是基于您从服务器发送的其他数据的后台。
根据您的其他数据:
content-available : 1
然后在{apns : [],
data : [
"Navigate" : "Home"
]}
,
didReceiveRemoteNotification
因此,当您的通知到来时,如果您在点击横幅或提醒时点击推送通知,则会在背景中显示特定屏幕。应用程序将打开并显示特定屏幕。