【问题标题】:Tags not working in Notification Hub with Swift标签在 Swift 的通知中心中不起作用
【发布时间】:2016-01-31 06:51:36
【问题描述】:

当我将标签设置为 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/

【问题讨论】:

  • 我不确定我是否找到了解决方案 - 但我了解到其中带有空格的标签似乎被阻塞了。可能是这里的问题。

标签: swift apple-push-notifications azure-notificationhub


【解决方案1】:

通过反复试验,我能够创建一个解决方案。希望这可以帮助下一个人。首先,标签名称中没有空格。其次,Objective-C API 的签名与 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);
            }
        }
    }
}

【讨论】:

    【解决方案2】:

    确保您的标签有效:

    标签可以是任何字符串,最多 120 个字符,包含字母数字 以及以下非字母数字字符:“_”、“@”、“#”、“.”、 ‘:’, ‘-’。

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

    【讨论】:

      猜你喜欢
      • 2019-11-09
      • 2020-11-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-31
      • 1970-01-01
      • 2019-02-14
      相关资源
      最近更新 更多