Sta*_*ley 30 push-notification apple-push-notifications ios swift xcode8
我有几个用Swift 2.2编写的应用程序,使用Xcode 7.3编译并在App Store上运行.这些应用程序使用Push Notifications,在iOS 9.3及更早版本中运行良好.
但是,在已升级到iOS 10的设备上,我的应用程序不会收到任何推送通知.仍在运行iOS 9的设备仍在接收通知.
考虑到它可能是证书或授权问题,我尝试了以下内容:将我的一个应用程序升级到Swift 2.3,添加了APS环境权利并在Xcode 8中编译它,但这没有任何区别.
在我的AppDelegate中,我仍然有现有的方法来注册推送通知,其中包括:
let notificationSettings = UIUserNotificationSettings(forTypes: [.Badge, .Sound, .Alert], categories:nil)
application.registerUserNotificationSettings(notificationSettings)
Run Code Online (Sandbox Code Playgroud)
这个注册似乎在iOS 10上也很成功,因为然后调用了应用程序didRegisterForRemoteNotificationsWithDeviceToken,所以我从APNS收到一个令牌.
问题是,当我向此设备发送推送通知时,永远不会调用应用程序didReceiveRemoteNotification.
现在,这里据说在UIApplicationDelegate方法已被弃用iOS上10,我应该实现userNotificationCenter(:didReceive:withCompletionHandler :)和userNotificationCenter(:willPresent:withCompletionHandler :)
问题是我不仅仅针对iOS 10.我仍然需要应用程序才能在iOS 8和9上运行,所以我怀疑这是实现这些方法的正确方法.
如何获取现有应用的推送通知以继续处理已升级到iOS 10的设备?我需要重写代码吗?我是否只需要更新一些证书或权利,并使用一些"新"设置在Xcode 8中重新编译?
Sta*_*ley 52
好的,我已经弄清楚了.我现在有了我的原始Push Notifications,它正在iOS 9上运行,在iOS 10上使用Xcode 8和Swift 2.3.
我通过对AppDelegate进行以下更改来实现此目的:
1)在我的项目设置中,在Capabilities选项卡下,我向下滚动到"Push Notifications"并将其转为"ON".这会自动生成一个权利文件,其中包含密钥"APS Environment"和值"development".
2)在AppDelegate.swift中,我进行了几次代码更改.我首先导入UserNotifications框架:
import UserNotifications
Run Code Online (Sandbox Code Playgroud)
然后我让AppDelegate实现UNUserNotificationCenterDelegate协议:
class AppDelegate: /*Some other protocols I am extending...*/, UNUserNotificationCenterDelegate {
Run Code Online (Sandbox Code Playgroud)
然后我添加以下方法:
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)
}
}
Run Code Online (Sandbox Code Playgroud)
然后我在didFinishLaunchingWithOptions中调用这个新创建的函数:
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
...
registerForPushNotifications(application)
...
}
Run Code Online (Sandbox Code Playgroud)
我将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.
}
Run Code Online (Sandbox Code Playgroud)
现在我为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
}
Run Code Online (Sandbox Code Playgroud)
我没有删除我之前在iOS 9中为推送通知实现的任何方法.
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)
}
Run Code Online (Sandbox Code Playgroud)
我已使用以下步骤解决了此问题:
步骤1:转到项目 - >目标 - >功能 - >推送通知 - >将其打开
第3步:退出Xcode,清理并运行
| 归档时间: |
|
| 查看次数: |
35715 次 |
| 最近记录: |