这是我的ReceiveRemoteNotification代码。

func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) {
        PFPush.handlePush(userInfo)
        if application.applicationState == UIApplicationState.Inactive {
            PFAnalytics.trackAppOpenedWithRemoteNotificationPayload(userInfo)
        }

        let notifiAlert = UIAlertView()

        let aps = userInfo["aps"] as? [NSObject: AnyObject]
            let alert1 = aps!["alerts"] as? String
            let link1 = aps!["links"] as? String

            notifiAlert.title = alert1!
            notifiAlert.message = link1
            notifiAlert.addButtonWithTitle("OK")
            notifiAlert.show()

            print(userInfo)
            print("success")

    }


我试图从我的网站接收JSON数据,我的JSON看起来像:

{"aps":{"alerts":"3","links":"","sounds":"default"}}


我确实收到了Json数据并将数据转到视图。使用此代码:

let aps = userInfo["aps"] as? [NSObject: AnyObject]
                let alert1 = aps!["alerts"] as? String
                let link1 = aps!["links"] as? String

                notifiAlert.title = alert1!
                notifiAlert.message = link1
                notifiAlert.addButtonWithTitle("OK")
                notifiAlert.show()

                print(userInfo)
                print("success")

        }


但是,我仅在使用应用程序时收到,但如果不使用,则无法收到通知。但我的手机振动或发声,但没有任何消息。

我在这里想念什么吗?谢谢 !

添加的是我的完整代码http://pastebin.com/mkFiq6Cs

编辑

    func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject], fetchCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void) {
             if application.applicationState == UIApplicationState.Inactive {
                 PFAnalytics.trackAppOpenedWithRemoteNotificationPayload(userInfo)
             }
 let aps = userInfo["aps"] as? [NSObject: AnyObject]
                    let alert1 = aps!["alerts"] as? String
                    let link1 = aps!["links"] as? String

                    notifiAlert.title = alert1!
                    notifiAlert.message = link1
                    notifiAlert.addButtonWithTitle("OK")
                    notifiAlert.show()

                    print(userInfo)
                    print("success")
}


所以我必须添加以下代码:

  func application(application: UIApplication, didReceiveLocalNotification notification: UILocalNotification) {
    if let userInfo = notification.userInfo {
        let aps = userInfo["aps"] as? [NSObject: AnyObject]
        let alert2 = aps!["alerts"] as? String
        let link2 = aps!["links"] as? String
        print("didReceiveLocalNotification: \(aps)")
        print("didReceiveLocalNotification: \(alert2)")
        print("didReceiveLocalNotification: \(link2)")
    }
}


而在didFinishLaunchingOption上:

 if let options = launchOptions {
    if let notification = options[UIApplicationLaunchOptionsLocalNotificationKey] as? UILocalNotification {
        if let userInfo = notification.userInfo {
            let customField1 = userInfo["CustomField1"] as! String
            // do something neat here
        }
    }
}


解决了

 func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject], fetchCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void) {
    PFPush.handlePush(userInfo)
    if application.applicationState == UIApplicationState.Inactive {
        PFAnalytics.trackAppOpenedWithRemoteNotificationPayload(userInfo)
    }
        let notifiAlert = UIAlertView()
        if let aps = userInfo["aps"] as? [NSObject: AnyObject],
            let alert1 = aps["alert"] as? String,
            let content1 = aps["content-available"] as? String,
            let link1 = userInfo["links"] as? String {
                    notifiAlert.title = alert1
                    notifiAlert.message = link1
                    notifiAlert.addButtonWithTitle("OK")
                    notifiAlert.show()
            }

        print(userInfo)
        print("success")

    completionHandler(UIBackgroundFetchResult.NewData)
}


这很完美。 !感谢@tskulbru

最佳答案

这是因为application(_:didReceiveRemoteNotification:)仅在应用程序处于活动状态时被调用。它无法处理非应用程序/后台远程通知。


  如果远程通知到达时应用程序未运行,则该方法将启动应用程序并在启动选项字典中提供适当的信息。该应用程序不会调用此方法来处理该远程通知。而是,您对application:willFinishLaunchingWithOptions:application:didFinishLaunchingWithOptions:方法的实现需要获取远程通知有效负载数据并进行适当响应。
  
  资料来源:https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIApplicationDelegate_Protocol/index.html?hl=ar#//apple_ref/occ/intfm/UIApplicationDelegate/application:didReceiveRemoteNotification


如上所述,如果application:didReceiveRemoteNotification应该能够处理后台通知,则需要具有其他实现。

如果您只想用一种方法来为您处理所有好处,而又没有很多不同的方法来实现基本相同的事情,那么Apple提供了一种称为application:didReceiveRemoteNotification:fetchCompletionHandler:的方法来为您处理所有这些事情。

您需要实现application:didReceiveRemoteNotification:fetchCompletionHandler:方法而不是此方法来处理后台通知。这将处理后台任务,完成后,您可以使用适当的枚举(.NoData / .NewData / .Failed)调用completionHandler。

如果要在应用程序处于活动状态时收到远程通知时显示本地通知,则需要使用上述方法创建该通知。如果要在应用程序处于活动状态时显示警报视图,则还需要处理该方法中的两种情况。我的建议是改用application:didReceiveRemoteNotification:fetchCompletionHandler:并在那里执行所有操作。

因此,在您的情况下,我将执行以下操作:

func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject], fetchCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void) {
  PFPush.handlePush(userInfo)
  if application.applicationState == UIApplicationState.Inactive {
    PFAnalytics.trackAppOpenedWithRemoteNotificationPayload(userInfo)
  } else {
    let notifiAlert = UIAlertView()
    if let aps = userInfo["aps"] as? [NSObject: AnyObject],
      let alert1 = aps["alerts"] as? String,
      let link1 = aps["links"] as? String {

      notifiAlert.title = alert1
      notifiAlert.message = link1
      notifiAlert.addButtonWithTitle("OK")
      notifiAlert.show()
    }

    print(userInfo)
    print("success")
  }
  completionHandler(UIBackgroundFetchResult.NewData)
}


我没有测试代码,但是应该没有太多更改。我没有使用过Parse,所以我不完全了解PFPush.handlePush(userInfo)的作用,您需要检查文档。

看来您的APNS有效负载也是错误的。见https://developer.apple.com/library/mac/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/Chapters/TheNotificationPayload.html#//apple_ref/doc/uid/TP40008194-CH107-SW1

在您的情况下:

{"aps":{"alert":"alert content","sound":"default", "content-available": 1}, "links":""}


您使用的是复数形式而不是单一形式。查看我为允许的密钥提供的URL。 links键在aps词典中不存在,这意味着它是自定义键,需要放置在词典外部。

请注意,这是标准的推送通知方式,使用Parse时可能不正确。

10-07 18:57
查看更多