没有获得推送通知的令牌

时间:2018-04-28 20:39:07

标签: ios swift push-notification

我试图推送通知工作,但我的印象是方法" didRegisterForRemoteNotificationsWithDeviceToken"永远不会被调用," didFailToRegisterForRemoteNotificationsWithError"的情况相同。 stackoverflow上的一些旧线程显示完全相同的问题,但提供的解决方案对我没有用:/

我到处搜索过,似乎我遇到了这个问题,令牌从未打印到控制台,我得到的唯一输出是:

CREATE TABLE films (
  code        char(5) CONSTRAINT firstkey PRIMARY KEY,
  title       varchar(40) NOT NULL,
  did         integer NOT NULL,
  date_prod   date,
  kind        varchar(10),
  len         interval hour to minute
);

我做了什么:

  • 检查服务是否已启动:https://developer.apple.com/system-status/
  • 为我的应用程序(APNs开发)成功颁发了证书
  • 在不同设备上运行应用
  • 导入的UserNotifications.framework
  • 设置推送通知和远程通知(后台模式)功能

这是我的代码(appdelegate.swift):

Permission granted: true
Notification settings: <UNNotificationSettings: 0x1c009cb10; authorizationStatus: Authorized, notificationCenterSetting: Enabled, soundSetting: Enabled, badgeSetting: Enabled, lockScreenSetting: Enabled, carPlaySetting: NotSupported, alertSetting: Enabled, alertStyle: Banner>

提前感谢!

1 个答案:

答案 0 :(得分:0)

您实际上从未尝试注册推送通知,因此永远不会调用相关功能。您需要拨打func registerForPushNotifications() { UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound, .badge]) { (granted, error) in print("Permission granted: \(granted)") guard granted else { return } UNUserNotificationCenter.current().getNotificationSettings { (settings) in print("Notification settings: \(settings)") DispatchQueue.main.async { UIApplication.shared.registerForRemoteNotifications() } } } } 才能接收推送通知,而您当前的代码只会请求通知权限。

d8.jar