标签在 Swift 的通知中心不工作

Tags not working in Notification Hub with Swift

当我将标签设置为 nil 时,一切都很好。但是格式化标签似乎不起作用。

有什么想法吗?

func application( application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData ) {

    if Leagues.count > 0 {
        var myLeague: [String] = [String]()
        for eachleague in Leagues {
            myLeague.append(eachleague.LeagueName)
        }


       let categories: NSSet =  NSSet(array: myLeague)

        let hub : SBNotificationHub = SBNotificationHub(connectionString: "Endpoint=<my endpoint>", notificationHubPath: "<myhub>")

        hub.registerNativeWithDeviceToken(deviceToken, tags: categories as! Set<NSObject>) { (error) -> Void in
            if (error != nil){
                print("Error registering for notifications: %@", error);
            }
        }
    }
}

我正在尝试按照 objective-c 中的示例进行操作:https://azure.microsoft.com/en-us/documentation/articles/notification-hubs-ios-send-breaking-news/

确保您的标签有效:

A tag can be any string, up to 120 characters, containing alphanumeric and the following non-alphanumeric characters: ‘_’, ‘@’, ‘#’, ‘.’, ‘:’, ‘-’.

https://msdn.microsoft.com/en-us/library/azure/dn530749.aspx

通过反复试验,我找到了一个解决方案。希望这可以帮助下一个人。首先,标签名称中没有空格。其次,API Objective-C 的签名不同于 Swift 的签名。

这是一个有效的 Swift 示例:

func application( application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData ) {

    if Leagues.count > 0 {
        var myLeague: [AnyObject] = [AnyObject]()
        for eachleague in Leagues {
            myLeague.insert(eachleague.LeagueID, atIndex: 0)
        }

       let tagSet: NSSet = NSSet(array: myLeague)

        let hub : SBNotificationHub = SBNotificationHub(connectionString: gEndPointName, notificationHubPath: gHubName)

        hub.registerNativeWithDeviceToken(deviceToken, tags: tagSet as! Set<NSObject>) { (error) -> Void in
            if (error != nil){
                print("Error registering for notifications: %@", error);
            }
        }
    }
}