使用 OneSignal 推送通知服务向特定测试设备发送推送通知

Send push notification to a specific test device using OneSignal push notification service

我之前使用 OneSignal 向我的 iOS 应用程序添加了推送通知支持。该应用程序是在 Xcode 中使用 Swift 制作的。

我只想向我的测试设备发送测试推送通知。我在文档中找到了以下手册:How do I send a notification to a single user?

我设法创建了该段,但我不知道将这段代码放在哪里:OneSignal.sendTag("is_test", "true")

有人知道我必须将这段代码放在哪里才能使它像我上面描述的那样工作吗?

我在这里上传了我的代码:https://codeshare.io/DxcNn

谢谢, 大卫.

更新:

OneSignal 现在还支持将设备设置为测试设备,而无需在代码中执行任何操作。您也可以从 App Store 下载您自己的应用程序并将其用作测试设备。只需 select 您的设备从设备列表中列出一个 OneSignal 并将其标记为测试设备。您可以在列表中按型号、版本 and/or 添加时间找到您的设备。

sendTag方法来自设备sdk。在你的情况下 iOS。 https://documentation.onesignal.com/docs/ios-native-sdk#section--sendtag-

您应该在应用程序委托中的 initWithLaunchOptions 之后随时执行此操作。根据评论更新代码

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject : AnyObject]?) -> Bool {

        let oneSignal = OneSignal(launchOptions: launchOptions, appId: "here_is_my_onesignal_app_id") { (message, additionalData, isActive) in
            NSLog("OneSignal Notification opened:\nMessage: %@", message)

            if additionalData != nil {
                NSLog("additionalData: %@", additionalData)
                // Check for and read any custom values you added to the notification
                // This done with the "Additonal Data" section the dashbaord.
                // OR setting the 'data' field on our REST API.
                if let customKey = additionalData["customKey"] as! String? {
                    NSLog("customKey: %@", customKey)
                }
            }


        }
        OneSignal.defaultClient().sendTag("is_test", value: "true")

        // Override point for customization after application launch.
        return true
    }