2016-09-30 67 views
20

正在尝试为iOS10设置推送通知。有它先前通过一些导游我的设置是像这样工作版本低于10
阅读:在iOS10和iOS9中接收推送通知时遇到问题

// Register for the defined notifications 

     if #available(iOS 10.0, *) { 

      let center = UNUserNotificationCenter.current() 
      center.delegate = UIApplication.shared.delegate as! AppDelegate 
      center.requestAuthorization(options: [.alert, .badge, .sound]) { (granted, error) in 

       if error == nil { 
        UIApplication.shared.registerForRemoteNotifications() 
       } 
      } 

     } else { 

      // Fallback on earlier versions 

      let notificationSettings = UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: [autoCancelCategory]) 
      UIApplication.shared.registerUserNotificationSettings(notificationSettings) 
      UIApplication.shared.registerForRemoteNotifications() 

     } 

这是在我的视图控制器的一个呼吁登录^。

现在在AppDelegate,它符合UNUserNotificationCenterDelegate,我有以下方法:

@available(iOS 10.0, *) 
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) { 
    print("Got a push!") 
} 


@available(iOS 10.0, *) 
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping() -> Void) { 
    print("Got a push!") 
} 

而且我也有:

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

    let settings = UserDefaults.standard 
    settings.setValue(deviceToken, forKey: "deviceToken") 
} 

设备令牌被上传到服务器Data,和这对以前的版本来说工作得很好。
我检查过服务器上的令牌与手机上的令牌匹配。
我已经为Capabilities中的所有目标启用了推送通知,我也打勾了Add the push Notifications entitlement to your entitlements file
即使在推送时也没有收到任何东西。
任何想法我可能在这里做错了吗?任何指针将非常感激!

谢谢!

编辑:我也注意到,推送通知不适用于iOS9。

+0

这可能是操作系统问题。请尝试重新启动设备。 https://twitter.com/yogye7/status/781340933228945408 –

回答

1

要回答这个问题,也许它会对其他人有用。我的代币错了。编码随最近的更新而改变。我实际上搜索了所有堆栈溢出,这是我的理解,我必须使用base64stringData转换为String。但是我注意到这个字符串中有几个奇怪的字符,如\。我终于结束了制作Data扩展:

extension Data { 

    func hexString() -> String { 
     return self.reduce("") { string, byte in 
      string + String(format: "%02X", byte) 
     } 
    } 

} 

此转换Data为正确的格式。因此,如果这是你怎么做您的转换,加上所有的权利都设置,你记得要添加新的方法:

@available(iOS 10.0, *) 
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) { 
    print("Got a push!") 
} 


@available(iOS 10.0, *) 
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping() -> Void) { 
    print("Got a push!") 
} 

,并设置在didFinishLaunchingWithOptions一切委托对象应该是好去。

2

您是否检查过“构建设置”>“代码签名授权”对授权文件的引用? [AppName的] .entitlements。

而且该权利文件包含正确的信息:

(关键:APS环境,值:发展)

除此之外,你可以尝试再生推证书。我们必须创建新的证书才能在IOS 10升级后生产(但不在沙盒环境中)。

+0

选中了“生成设置”>“代码签名授权”,但那里似乎没有问题。 – Kex

+0

@Kex与我们公司的应用数量有同样的问题。还请检查“生成设置”>“功能”,并确保“通知”已打开。 – ibnetariq

2

只是注意到您注册的代表晚于application(_:willFinishLaunchingWithOptions:)application(_:didFinishLaunchingWithOptions:)方法被调用。

也许这就是为什么你的委托方法没有被击中?

docs

重要

必须不迟您的应用程序启动完毕之前,你的委托对象分配给UNUserNotificationCenter对象。例如,在iOS应用程序中,您必须在应用程序中指定它(:willFinishLaunchingWithOptions :)或应用程序(:didFinishLaunchingWithOptions :)方法。

+0

真的很不错!当我回到家里并让你知道结果时,我会给你一个机会。 – Kex

+0

移动'let center = UNUserNotificationCenter.current();如果#available(_应用程序:UIApplication,didFinishLaunchingWithOptions launchOptions:[UIApplicationLaunchOptionsKey:Any]?) - > Bool'仍然没有通过 – Kex

+0

我有我的: 'if #available iOS 10.0,*){ UNUserNotificationCenter.currentNotificationCenter()。delegate = self }' – Mark