在 iOS 10 上未收到推送通知,但在 iOS 9 及更早版本上工作
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/39532953/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Push Notifications not being received on iOS 10, but working on iOS 9 and before
提问by Stanley
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.
我有几个用 Swift 2.2 编写的应用程序,用 Xcode 7.3 编译并在 App Store 上架。这些应用程序利用推送通知并且在 iOS 9.3 及更早版本中运行良好。
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.
但是,在升级到 iOS 10 的设备上,我的应用程序不会收到任何推送通知。仍在运行 iOS 9 的设备仍在接收通知。
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.
认为这可能是证书或权利问题,我尝试了以下方法:将我的一个应用程序升级到 Swift 2.3,添加 APS 环境权利并在 Xcode 8 中编译它,但这没有任何区别。
In my AppDelegate I still have the existing methods to register for push notifications that includes:
在我的 AppDelegate 中,我仍然有现有的方法来注册推送通知,其中包括:
let notificationSettings = UIUserNotificationSettings(forTypes: [.Badge, .Sound, .Alert], categories:nil)
application.registerUserNotificationSettings(notificationSettings)
This registration seems to be successful even on iOS 10 since Application didRegisterForRemoteNotificationsWithDeviceTokenis then called, so I am receiving a token from APNS.
即使在 iOS 10 上,此注册似乎也成功,因为随后调用了Application didRegisterForRemoteNotificationsWithDeviceToken,因此我从 APNS 接收令牌。
The problem is that when I send a push notification to this device, Application didReceiveRemoteNotificationis never called.
问题是,当我向此设备发送推送通知时,从未调用过应用程序 didReceiveRemoteNotification。
Now, hereit is said that the methods on UIApplicationDelegate is deprecated on iOS 10 and I should implement userNotificationCenter(:didReceive:withCompletionHandler:) and userNotificationCenter(:willPresent:withCompletionHandler:)
现在,这里说 UIApplicationDelegate 上的方法在 iOS 10 上已弃用,我应该实现 userNotificationCenter( :didReceive: withCompletionHandler:) 和 userNotificationCenter(:willPresent: withCompletionHandler:)
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。我仍然需要该应用程序在 iOS 8 和 9 上运行,所以我怀疑这是实现这些方法的正确方法。
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 10 的设备上工作?我需要重写代码吗?我是否只需要更新一些证书或权利并使用一些“新”设置在 Xcode 8 中重新编译?
回答by Stanley
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.
好的,我想通了。我现在拥有在 iOS 9 上运行的原始推送通知,在 iOS 10 上使用 Xcode 8 和 Swift 2.3。
I implemented this by making the following changes to my AppDelegate:
我通过对我的 AppDelegate 进行以下更改来实现这一点:
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".
1) 在我的项目设置中,在 Capabilities 选项卡下,向下滚动到“Push Notifications”并将其设为“ON”。这会自动生成一个包含密钥“APS Environment”和值为“development”的权利文件。
2) In AppDelegate.swift I make several code changes. I start by importing the UserNotifications framework:
2) 在 AppDelegate.swift 中,我进行了几处代码更改。我首先导入 UserNotifications 框架:
import UserNotifications
Then I have AppDelegate implement the UNUserNotificationCenterDelegate protocol:
然后我让 AppDelegate 实现了 UNUserNotificationCenterDelegate 协议:
class AppDelegate: /*Some other protocols I am extending...*/, UNUserNotificationCenterDelegate {
Then I add the following method:
然后我添加以下方法:
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)
}
}
Then I call this newly created function inside didFinishLaunchingWithOptions:
然后我在 didFinishLaunchingWithOptions 中调用这个新创建的函数:
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
...
registerForPushNotifications(application)
...
}
I leave my didRegisterForRemoteNotificationsWithDeviceToken method unchanged:
我保持我的 didRegisterForRemoteNotificationsWithDeviceToken 方法不变:
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.
}
Now I implement two new methods for iOS 10 to handle the receiving of Push Notifications:
现在我为 iOS 10 实现了两种新方法来处理推送通知的接收:
@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
}
I did not remove any of the methods I have previously implemented for Push Notifications in iOS 9.
我没有删除我之前在 iOS 9 中为推送通知实现的任何方法。
回答by ilan
IOS 10 has a different Notification kit.
IOS 10 有一个不同的通知工具包。
import UserNotifications
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
if #available(iOS 10.0, *) {
let center = UNUserNotificationCenter.current()
center.requestAuthorization(options: [.badge, .alert , .sound]) { (granted, error) in
if granted {
UIApplication.shared.registerForRemoteNotifications()
}
}
} else {
let type: UIUserNotificationType = [UIUserNotificationType.badge, UIUserNotificationType.alert, UIUserNotificationType.sound]
let setting = UIUserNotificationSettings(types: type, categories: nil)
UIApplication.shared.registerUserNotificationSettings(setting)
UIApplication.shared.registerForRemoteNotifications()
}
}
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data){
let token = String(format: "%@", deviceToken as CVarArg)
}
回答by Dheeraj D
I have fixed this issue using below step:
我已经使用以下步骤解决了这个问题:
Step 1:Go to Project -> Target -> Capabilities -> Push Notifications -> Switch it ON
第 1 步:转到项目 -> 目标 -> 功能 -> 推送通知 -> 将其打开
Step 2: Fix issue related to proper certificate,provisioning profile
Step 3: Quit Xcode, Clean and Run
第 3 步:退出 Xcode,清理并运行
回答by kevinl
In various SO responses to questions about iOS 9 vs iOS 10 push notifications, I've tried the following tests:
在对有关 iOS 9 与 iOS 10 推送通知问题的各种 SO 回复中,我尝试了以下测试:
1) update the notification calls to specifically use the UNUserNotificationCenter for iOS 10 devices
1) 更新通知调用,专门为 iOS 10 设备使用 UNUserNotificationCenter
2) update the Push capability so that the entitlements file is updated
2) 更新 Push 能力,以便更新权利文件
I can confirm that doing #2 and keeping all your existing iOS 9 push code the same will allow basic push notifications to come through on the iOS 10 devices
我可以确认执行 #2 并保持所有现有的 iOS 9 推送代码相同将允许基本推送通知在 iOS 10 设备上通过
回答by BrianM
When my app was moved from IOS 9 to IOS 10 the silent push notifications sent by my Parse server hosted on Heroku stopped being received by the app.
当我的应用程序从 IOS 9 移至 IOS 10 时,应用程序停止接收由 Heroku 上托管的 Parse 服务器发送的静默推送通知。
The only change I made to make notifications work again was to modify the Javascript on my server so that the argument to content-availablewas an integer 1 and not a string "1"
我为使通知再次工作所做的唯一更改是修改我服务器上的 Javascript,以便content-available的参数是整数 1 而不是字符串“1”
Parse.Push.send({
where: notifyDeletedRequestsQuery,
data: {
'content-available': 1,
refresh: constants.requestsClassName
}
}, { useMasterKey: true });
Also note that content-available cannot appear in the same push notification as badge, sound or alert.
另请注意, content-available 不能与徽章、声音或警报出现在同一推送通知中。