iOS 未收到 APN
iOS APN not received
我有一个奇怪的问题。
注册通知的代码如下:
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// PUSH NOTIFICATION
let notificationTypes: UIUserNotificationType = [UIUserNotificationType.Alert, UIUserNotificationType.Badge, UIUserNotificationType.Sound]
let pushNotificationSettings = UIUserNotificationSettings(forTypes: notificationTypes, categories: nil)
application.registerUserNotificationSettings(pushNotificationSettings)
application.registerForRemoteNotifications()
并处理通知:
func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject], fetchCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void) {
let notification = UILocalNotification()
notification.soundName = UILocalNotificationDefaultSoundName
notification.alertBody = "TEST"
application.presentLocalNotificationNow(notification)
当我在 xcode 中测试这个简单的代码时(使用设备,而不是模拟器),没问题。但是当我在没有 xcode 的情况下测试它时(就在我的 phone 上),本地通知没有出现:没有声音,没有警报,没有徽章,什么都没有。
应用程序在后台 运行 时会出现此问题。
发送的 APN 是静默通知。
有解决办法吗?
编辑
我通过将静默 APN 的优先级降低到 5 解决了这个问题。
当通过 xcode 安装时,通知将与开发一起工作 (gateway.sandbox.push.apple.com) url。将其导出为 ipa(临时)并手动安装需要从生产 url gateway.push.apple.com(包括生产密钥)发送通知。因此,您必须创建这两个密钥,然后您可以设法在处理 xcode 时通过开发 url 发送通知,在从导出的 ipa 安装时通过生产发送通知。
如果您已经通过 Xcode 运行 您的应用一次,那么您之前会将令牌存储为默认值。因此,当您 运行 它再次 registerForRemoteNotifications() 不会被调用,因为您已将其包装在该 if 语句中。
您应该在每次应用程序启动时调用 registerForRemoteNotifications()(并且不要假设令牌是固定值,它不是,它可以更改)。
您的 deviceToken 将在您编译到生产环境(临时)并从 Xcode 部署时发生变化。
看到这个 answer。
我终于找到问题了,请看我的编辑。
当您发送静默 APN(即 content-available
设置为 1
)时,APN priority
必须为 5 而 而不是 10.
我有一个奇怪的问题。
注册通知的代码如下:
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// PUSH NOTIFICATION
let notificationTypes: UIUserNotificationType = [UIUserNotificationType.Alert, UIUserNotificationType.Badge, UIUserNotificationType.Sound]
let pushNotificationSettings = UIUserNotificationSettings(forTypes: notificationTypes, categories: nil)
application.registerUserNotificationSettings(pushNotificationSettings)
application.registerForRemoteNotifications()
并处理通知:
func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject], fetchCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void) {
let notification = UILocalNotification()
notification.soundName = UILocalNotificationDefaultSoundName
notification.alertBody = "TEST"
application.presentLocalNotificationNow(notification)
当我在 xcode 中测试这个简单的代码时(使用设备,而不是模拟器),没问题。但是当我在没有 xcode 的情况下测试它时(就在我的 phone 上),本地通知没有出现:没有声音,没有警报,没有徽章,什么都没有。
应用程序在后台 运行 时会出现此问题。
发送的 APN 是静默通知。
有解决办法吗?
编辑
我通过将静默 APN 的优先级降低到 5 解决了这个问题。
当通过 xcode 安装时,通知将与开发一起工作 (gateway.sandbox.push.apple.com) url。将其导出为 ipa(临时)并手动安装需要从生产 url gateway.push.apple.com(包括生产密钥)发送通知。因此,您必须创建这两个密钥,然后您可以设法在处理 xcode 时通过开发 url 发送通知,在从导出的 ipa 安装时通过生产发送通知。
如果您已经通过 Xcode 运行 您的应用一次,那么您之前会将令牌存储为默认值。因此,当您 运行 它再次 registerForRemoteNotifications() 不会被调用,因为您已将其包装在该 if 语句中。
您应该在每次应用程序启动时调用 registerForRemoteNotifications()(并且不要假设令牌是固定值,它不是,它可以更改)。
您的 deviceToken 将在您编译到生产环境(临时)并从 Xcode 部署时发生变化。 看到这个 answer。
我终于找到问题了,请看我的编辑。
当您发送静默 APN(即 content-available
设置为 1
)时,APN priority
必须为 5 而 而不是 10.