【问题标题】:Why not able to handle APNS Push notification and Local notification?为什么不能处理 APNS 推送通知和本地通知?
【发布时间】:2016-08-02 06:12:51
【问题描述】:

我也在使用推送通知和本地通知,但前台通知工作正常,但是在这种情况下终止应用程序时,我无法在点击通知时重定向特定视图。

 func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {  

         // Check if launched from notification
        // 1
        if let notification = launchOptions?[UIApplicationLaunchOptionsRemoteNotificationKey] as? [String: AnyObject] {
            // 2
            let aps = notification["aps"] as! [String: AnyObject]
            //Redirect to notification view
            handlePushMessage(aps)
        }else if let notification = launchOptions?[UIApplicationLaunchOptionsLocalNotificationKey] as? [String: AnyObject] {
            // 2
            self.postData = notification["aps"] as! [String: AnyObject]
            //Redirect to notification view
            didTapNotification()
        }else{
            //Session
            //Redirect to Main view
            checkUserSession()
        }
   return true
}

当应用程序处于非活动状态或终止时,我正面临来自 APNS 通知和本地通知的此问题。请帮助我找到解决方案。

【问题讨论】:

  • 当您点击通知时,您的应用是否在后台运行?只有当您的应用程序将再次启动时,您的代码才会起作用,但当它在后台并且您点击通知时不会起作用
  • 那么如何管理应用程序在后台/非活动状态如何重定向到特定视图?
  • 当我点击通知时我的应用程序处于后台状态时,它不会重定向特定页面。
  • 你可以在这里找到一个很好的答案:stackoverflow.com/a/32079458/4287861。如果您想使用它,您还需要启用远程通知功能
  • 我们需要更多信息,当您在 cmets 中谈论后台应用程序时,您可以轻松设置断点并检查问题所在,并且绝对不在 didFinishLaunchingWithOptions 中

标签: ios iphone swift swift2 apple-push-notifications


【解决方案1】:

你试试didReceiveRemoteNotification函数怎么样?

func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) {
     if(application.applicationState == UIApplicationStateBackground){



     }else if(application.applicationState == UIApplicationStateInactive){



     }
}

【讨论】:

  • 它已经在我这边工作了,只有一件事是当你终止应用程序并点击通知时它不起作用。
  • 如何添加 if(application.applicationState == UIApplicationStateBackground){ }
【解决方案2】:

试试这个代码,你可以根据你的要求修改它。如果您在理解此逻辑时有任何问题,请告诉我。带有 completionHandler 的 didReceiveRemoteNotification 在后台和前台都可以工作。不要忘记在 plist 中进行适当的更改以支持后台获取和后台通知。

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
    // Override point for customization after application launch.
    application.setMinimumBackgroundFetchInterval(UIApplicationBackgroundFetchIntervalMinimum)

    if let notification = launchOptions?[UIApplicationLaunchOptionsRemoteNotificationKey] as? [String: AnyObject] {
        notificationDataDict = notification;
    }


    return true
}
func handleRemoteNotificationWithUserInfo(application:UIApplication, userInfo:NSDictionary){

}
func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject], fetchCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void) {

    print(userInfo)

    if application.applicationState != .Inactive{

        notificationDataDict = userInfo;

        if let navigationController = self.window?.rootViewController as? UINavigationController{


            if application.applicationState == .Active{

                if application.backgroundRefreshStatus == .Available{

                    completionHandler(.NewData)

                    self.handleRemoteNotificationWithUserInfo(application, userInfo: userInfo)


                }
                else
                {
                    completionHandler(.NoData)
                }
            }
            else{
                completionHandler(.NewData)

                self.handleRemoteNotificationWithUserInfo(application, userInfo: userInfo)
            }

        }


    }
}

【讨论】:

    【解决方案3】:

    我终于得到了管理 APNS 推送通知和本地通知的解决方案,现在可以正常工作了。

    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
    
    UIApplication.sharedApplication().setStatusBarStyle(UIStatusBarStyle.LightContent, animated: true)
    
        //Register here For Notification
    
        if #available(iOS 9, *) {
    
            let notificationTypes: UIUserNotificationType = [UIUserNotificationType.Alert, UIUserNotificationType.Badge, UIUserNotificationType.Sound]
            let pushNotificationSettings = UIUserNotificationSettings(forTypes: notificationTypes, categories: nil)
            application.registerUserNotificationSettings(pushNotificationSettings)
            application.registerForRemoteNotifications()
    
        }else{
    
            UIApplication.sharedApplication().registerUserNotificationSettings(
                UIUserNotificationSettings(forTypes: .Alert, categories: nil))
            application.registerForRemoteNotifications()
        }
    
    dispatch_async(dispatch_get_main_queue()) {
            // Check if launched from notification
            // 1
            if let notification = launchOptions?[UIApplicationLaunchOptionsRemoteNotificationKey] as? [String: AnyObject] {
                // 2
                let aps = notification["aps"] as! [String: AnyObject]
                handlePushMessage(aps)
            }else if let notification = launchOptions?[UIApplicationLaunchOptionsLocalNotificationKey] as? UILocalNotification {
                // 2
                self.postData = notification.userInfo as! [String: AnyObject]
                didTapNotification()
            }else{
                //Session
                checkUserSession()
            }
          }
    
    }
    

    【讨论】:

      猜你喜欢
      • 2013-04-16
      • 1970-01-01
      • 1970-01-01
      • 2018-07-15
      • 1970-01-01
      • 1970-01-01
      • 2012-02-22
      • 1970-01-01
      • 2015-12-29
      相关资源
      最近更新 更多