推送通知未在iOS 10上收到,但在iOS 9及更早版本上运行

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中为推送通知实现的任何方法.

  • 非常重要:我们使用自己的推送通知服务器来发送消息.除了我在这里描述的客户端更改,我们还必须对我们的一些静默通知进行服务器更改.我们有一些静默通知,其结构{"my_field":"some_value","my_field2":"myvalue2"}在iOS 10之前运行良好,但似乎在iOS 10上必须包含"警报"或"aps"领域也是如此.所以我不得不将我在服务器上的消息更改为{"my_field":"some_value","my_field2":"myvalue2","aps":{"content-available":1}}否则我收不到任何内容APNS. (4认同)
  • 除了你__DON'T想要在APP START__上请求许可! (2认同)

ila*_*lan 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)


Dhe*_*j D 8

我已使用以下步骤解决了此问题:

步骤1:转到项目 - >目标 - >功能 - >推送通知 - >将其打开

在此输入图像描述 第2步:修复与正确证书,配置文件相关的问题

第3步:退出Xcode,清理并运行