【问题标题】:Push Notifications not being received on iOS 10, but working on iOS 9 and before在 iOS 10 上未收到推送通知,但在 iOS 9 及更低版本上工作
【发布时间】:2017-01-24 18:27:48
【问题描述】:

我有几个使用 Swift 2.2 编写的应用程序,使用 Xcode 7.3 编译,并且在 App Store 上发布。这些应用使用推送通知,在 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)

即使在 iOS 10 上,此注册似乎也是成功的,因为随后调用了 Application didRegisterForRemoteNotificationsWithDeviceToken,因此我收到了来自 APNS 的令牌。

问题是当我向这个设备发送推送通知时,Application didReceiveRemoteNotification 永远不会被调用。

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

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

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

【问题讨论】:

  • 这个“推送通知在 iOS 10 中不起作用”有几个预先存在的答案 ..
  • 您能否参考您所指的vaibhav的答案?我已经搜索了两天,找不到任何适用于我描述的场景的问题或答案。
  • 确定link 1, link 2 ..
  • 有没有你可以参考的 Swift 答案?
  • 你可以在 Objective C 中点击这个链接,但你可以得到一些想法stackoverflow.com/questions/39572897/…

标签: ios swift push-notification apple-push-notifications xcode8


【解决方案1】:

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

我通过对我的 AppDelegate 进行以下更改来实现这一点:

1) 在我的项目设置中,在“功能”选项卡下,我向下滚动到“推送通知”并将其设置为“开启”。这会自动生成一个包含键“APS Environment”和值为“development”的权利文件。

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

import UserNotifications

然后我让 AppDelegate 实现 UNUserNotificationCenterDelegate 协议:

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 中调用这个新创建的函数:

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

我的 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.
 }

现在我为 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
}

我没有删除我之前在 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。
  • 你是我的英雄
  • 这非常有用的答案。谢谢你,斯坦利
  • 除非您不想在应用启动时请求权限
  • 您为 iOS 10 实现了这 2 个新方法来处理用户对通知的操作! “当用户通过打开应用程序、关闭通知或选择 UNNotificationAction 来响应通知时,将在委托上调用该方法”。投反对票,因为它在传播虚假信息。
【解决方案2】:

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)
}

【讨论】:

  • 这是 swift 3.0 语法。
【解决方案3】:

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

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

第 2 步:修复与正确证书、配置文件相关的问题

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

【讨论】:

  • 这个答案对我来说很甜蜜。就我而言,iOS9.x 确实收到了设备令牌,但 iOS10 没有
【解决方案4】:

在对有关 iOS 9 与 iOS 10 推送通知问题的各种 SO 回复中,我尝试了以下测试:

1) 更新通知调用以专门为 iOS 10 设备使用 UNUserNotificationCenter

2) 更新推送功能以更新权利文件

我可以确认,执行 #2 并保持所有现有的 iOS 9 推送代码相同将允许基本推送通知在 iOS 10 设备上通过

【讨论】:

  • 可以确认 - 刚刚通过添加权利文件开始实施已接受的答案,它在 iOS 10 上无需任何额外更改即可工作 :)
【解决方案5】:

当我的应用程序从 IOS 9 移动到 IOS 10 时,由托管在 Heroku 上的 Parse 服务器发送的静默推送通知停止被应用程序接收。

我为使通知再次起作用所做的唯一更改是修改服务器上的 Javascript,以便 content-available 的参数是整数 1 而不是字符串“1”

           Parse.Push.send({
                where: notifyDeletedRequestsQuery,
                data: {
                    'content-available': 1,
                    refresh: constants.requestsClassName
                    }
            }, { useMasterKey: true });

另请注意,内容可用不能与徽章、声音或警报出现在同一推送通知中。

【讨论】:

    猜你喜欢
    • 2017-01-23
    • 2017-05-24
    • 1970-01-01
    • 2016-03-08
    • 1970-01-01
    • 1970-01-01
    • 2019-02-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多