3

我正在尝试实现丰富的推送通知,但注册推送通知有问题。

有人帮我吗?

4

1 回答 1

4

我检查了苹果文档并找到了一种方法,其中一些Class在 iOS 10 中被废弃,我们一直使用到 iOS 9.x

步骤有:

  1. 添加框架UserNotifications
  2. 在 info plist 中添加一个键(我这样做是因为我正在使用后台获取),检查屏幕截图
  3. 使用以下代码并将令牌发送到您的服务器

注册远程通知

let center = UNUserNotificationCenter.current()
    center.requestAuthorization(options: [.alert, .sound, .badge]) { (granted, error) in

        // Enable or disable features based on authorization.
        if granted == true
        {
            print("Allow")
            UIApplication.shared.registerForRemoteNotifications()
        }
        else
        {
            print("Don't Allow")
        }
    }

获取令牌

func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
    print(deviceToken)
}

在此处输入图像描述

于 2016-08-13T14:05:58.113 回答