ios - 解析和 Swift : Push notifications with "aps": {"content-available": 1} not being received by didReceiveRemoteNotification

标签 ios swift parse-platform push-notification

我像这样发出 Parse REST API 请求

  curl -X POST \
  -H "X-Parse-Application-Id: key-here" \
  -H "X-Parse-REST-API-Key: key-here" \
  -H "Content-Type: application/json" \
  -d '{
        "where": {
          "deviceToken": "token-here",
          "deviceType": "ios"
        },
        "data": {
          "aps": {"content-available": "1"}
        }
      }' \
  https://api.parse.com/1/push

现在,当应用程序打开时,设备会读取它。但是当我锁定设备并再次发出上述 curl 请求时,没有任何反应。下面是我的 didReceiveRemoteNotification 函数。

func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject], fetchCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void) {
    println("received notification")
    var remoteServer = RemoteServer()
    remoteServer.sendDataToServer { (success) -> () in
         completionHandler(UIBackgroundFetchResult.NewData)
    }
}

一旦我打开应用程序,didReceiveRemoteNotification 的内部调用就会被执行。但同样,当应用程序未打开或手机被锁定时,什么也不会发生。

有什么想法/建议吗?

最佳答案

我假设您正确配置了所有证书和配置文件。因为正如您所说,当应用程序处于事件状态时您会收到响应(用您的话说它已打开)

但是当应用程序处于后台模式或当您当时锁定设备时,您的应用程序的状态不是事件的。并且您没有收到推送通知的警报或声音。

发生这种情况是因为 PAYLOAD 包含有关系统如何提醒用户或处理您发送的数据的信息。

对于每个通知,您需要组成一个 JSON 字典对象,其中包含另一个名为 "aps"

的目录

aps 包含以下内容:

  1. 警告消息(将显示给用户)[String OR 词典]

  2. 要标记的数字(显示在应用程序图标的右上角。)[数字]

  3. A sound to play(它播放声音)[String] 这里你需要传递app bundle中声音文件的名字

如果您想在设备的通知面板或锁定屏幕上显示警报或横幅,则您的aps 必须根据其类型具有正确的值。

还有一件事

"aps": {"content-available": "1"}

您的aps 包含上述 JSON。

content-available This property with value 1 Means : Remote notification work as SILENT NOTIFICATION And when SILENT NOTIFICATION kind of notification arrives iOS activce your app in background mode from which you can complete you background progress. (For ex : Downloading the data or other process)

这里我提供了aps字典的示例。

aps JSON 示例,其中包括“警报、声音和角标(Badge)”ia,如下所示:

    "aps" : {

    "alert" : "This a alert",

    "badge" : 1,

    "sound" : "bingbong.aiff"

}

并确保您已在 AppDelegate 类中正确配置远程通知。

对于 iOS7 及更早版本(在 swift 中)

        var types = UIRemoteNotificationType.Badge | UIRemoteNotificationType.Sound | UIRemoteNotificationType.Alert

        UIApplication.sharedApplication().registerForRemoteNotificationTypes(types)

对于 iOS8

        var notiType = UIUserNotificationType.Badge | UIUserNotificationType.Sound | UIUserNotificationType.Alert

        var notiSettings:UIUserNotificationSettings
        notiSettings = UIUserNotificationSettings(forTypes:notiType, categories: nil)

        UIApplication.sharedApplication().registerUserNotificationSettings(notiSettings)
        UIApplication.sharedApplication().registerForRemoteNotifications()

希望对你有帮助。

关于ios - 解析和 Swift : Push notifications with "aps": {"content-available": 1} not being received by didReceiveRemoteNotification,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26706039/

相关文章:

ios - 如何将整数发送到类型 id 的指针

ios - Swift getter + 数组初始化

c# - 通过 Facebook 登录注册用户(未指定用户名/密码)

ios - 为什么 EAAccessoryDidConnectNotification 会出现两次?

ios - 如何从惰性集变量内的完成处理程序返回

ios - SpriteKit/Swift 中向上滑动角色跳跃

ios - PFQuery FindObjectsInBackground 返回 0

android - 如何在没有 gcm 注册的情况下解析发送推送?

objective-c - 从 TableView 中删除行

ios - 添加将缩小底层 View 的覆盖 UIView