在 iOS 10 上未收到推送通知,但在 iOS 9 及更早版本上工作 [英] Push Notifications not being received on iOS 10, but working on iOS 9 and before

查看:26
本文介绍了在 iOS 10 上未收到推送通知,但在 iOS 9 及更早版本上工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有几个使用 Swift 2.2 编写、使用 Xcode 7.3 编译并且在 App Store 上架的应用程序.这些应用使用推送通知,在 iOS 9.3 及更早版本中运行良好.

I have several apps that were written in Swift 2.2, compiled with Xcode 7.3 and is live on the App Store. The apps utilize Push Notifications and is working fine in iOS 9.3 and earlier.

但是,在升级到 iOS 10 的设备上,我的应用没有收到任何推送通知.仍在运行 iOS 9 的设备仍在接收通知.

On devices that have been upgraded to iOS 10, however, my apps don't receive any Push Notifications. Devices that are still running iOS 9 are still receiving notifications.

认为可能是证书或权限问题,我尝试了以下方法:将我的一个应用程序升级到 Swift 2.3,添加了 APS 环境授权并在 Xcode 8 中编译它,但这没有任何区别.

Thinking it might be a certificate or entitlement issue, I have tried the following: Upgraded one of my apps to Swift 2.3, added the APS Environment Entitlement and compiled it in Xcode 8 but this made no difference.

在我的 AppDelegate 中,我仍然有注册推送通知的现有方法,包括:

In my AppDelegate I still have the existing methods to register for push notifications that includes:

let notificationSettings = UIUserNotificationSettings(forTypes: [.Badge, .Sound, .Alert], categories:nil)
application.registerUserNotificationSettings(notificationSettings)

即使在 iOS 10 上,此注册似乎也成功,因为 Application didRegisterForRemoteNotificationsWithDeviceToken 被调用,所以我从 APNS 接收令牌.

This registration seems to be successful even on iOS 10 since Application didRegisterForRemoteNotificationsWithDeviceToken is then called, so I am receiving a token from APNS.

问题是,当我向该设备发送推送通知时,Application didReceiveRemoteNotification 从未被调用.

The problem is that when I send a push notification to this device, Application didReceiveRemoteNotification is never called.

现在,这里据说 UIApplicationDelegate 上的方法在 iOS 10 上已弃用,我应该实现userNotificationCenter(:didReceive:withCompletionHandler:) 和 userNotificationCenter(:willPresent:withCompletionHandler:)

Now, here it is said that the methods on UIApplicationDelegate is deprecated on iOS 10 and I should implement userNotificationCenter(:didReceive:withCompletionHandler:) and userNotificationCenter(:willPresent:withCompletionHandler:)

问题是我不仅仅针对 iOS 10.我仍然需要该应用程序在 iOS 8 和 9 上运行,所以我怀疑这是实现这些方法的正确方法.

The problem is that I am not ONLY targeting iOS 10. I still need the app to work on iOS 8 and 9 so I doubt that it's the correct approach to implement those methods.

如何获取现有应用的推送通知以继续在已升级到 iOS 10 的设备上运行?我需要重写代码吗?我是否只需要更新一些证书或权利并使用一些新"设置在 Xcode 8 中重新编译?

How do I get push notifications for an existing app to continue working on devices that have been upgraded to iOS 10? Do I need to rewrite code? Do I just need to update some certificates or entitlements and recompile in Xcode 8 with some "new" settings?

推荐答案

好的,我想通了.我现在拥有在 iOS 9 上运行的原始推送通知,在 iOS 10 上使用 Xcode 8 和 Swift 2.3.

Ok I have figured it out. I now have my original Push Notifications that was working on iOS 9 working on iOS 10 with Xcode 8 and Swift 2.3.

我通过对 AppDelegate 进行以下更改来实现此目的:

I implemented this by making the following changes to my AppDelegate:

1) 在我的项目设置中,在 Capabilities 选项卡下,我向下滚动到Push Notifications"并将其设为ON".这会自动生成一个包含密钥APS Environment"和值为development"的权利文件.

1) On my project settings, under the Capabilities Tab, I scroll down to "Push Notifications" and turn it to "ON". This automatically generates an entitlements file that contains the key "APS Environment" and with value "development".

2) 在 AppDelegate.swift 中,我进行了几处代码更改.我首先导入 UserNotifications 框架:

2) In AppDelegate.swift I make several code changes. I start by importing the UserNotifications framework:

import UserNotifications

然后我让 AppDelegate 实现了 UNUserNotificationCenterDelegate 协议:

Then I have AppDelegate implement the UNUserNotificationCenterDelegate protocol:

class AppDelegate: /*Some other protocols I am extending...*/, UNUserNotificationCenterDelegate {

然后我添加以下方法:

func registerForPushNotifications(application: UIApplication) {

    if #available(iOS 10.0, *){
        UNUserNotificationCenter.currentNotificationCenter().delegate = self
        UNUserNotificationCenter.currentNotificationCenter().requestAuthorizationWithOptions([.Badge, .Sound, .Alert], completionHandler: {(granted, error) in
            if (granted)
            {
                UIApplication.sharedApplication().registerForRemoteNotifications()
            }
            else{
                //Do stuff if unsuccessful...
            }
        })
    }

    else{ //If user is not on iOS 10 use the old methods we've been using
        let notificationSettings = UIUserNotificationSettings(
            forTypes: [.Badge, .Sound, .Alert], categories: nil)
        application.registerUserNotificationSettings(notificationSettings)

    }

}

然后我在 didFinishLaunchingWithOptions 中调用这个新创建的函数:

Then I call this newly created function inside didFinishLaunchingWithOptions:

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

我保持我的 didRegisterForRemoteNotificationsWithDeviceToken 方法不变:

I leave my didRegisterForRemoteNotificationsWithDeviceToken method unchanged:

func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) {
    //Implement this. Do something with the token I receive. Send it to my push notification server to register the device or something else I'd like to do with the token.
 }

现在我为 iOS 10 实现了两种新方法来处理推送通知的接收:

Now I implement two new methods for iOS 10 to handle the receiving of Push Notifications:

@available(iOS 10.0, *)
func userNotificationCenter(center: UNUserNotificationCenter, willPresentNotification notification: UNNotification, withCompletionHandler completionHandler: (UNNotificationPresentationOptions) -> Void) {
    //Handle the notification
}

@available(iOS 10.0, *)
func userNotificationCenter(center: UNUserNotificationCenter, didReceiveNotificationResponse response: UNNotificationResponse, withCompletionHandler completionHandler: () -> Void) {
    //Handle the notification
}

我没有删除我之前在 iOS 9 中为推送通知实现的任何方法.

I did not remove any of the methods I have previously implemented for Push Notifications in iOS 9.

这篇关于在 iOS 10 上未收到推送通知,但在 iOS 9 及更早版本上工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆