David David - 1 year ago 171
iOS Question

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

I added push notification support to my iOS app using OneSignal some time before. The app is made in Xcode with Swift.

I want to send a test push notification only to my test device(s). I the documentation I found the following manual: How do I send a notification to a single user?

I managed to create the segment but I don't know where to put this peace of code:

OneSignal.sendTag("is_test", "true")

Does anybody know where I have to put this piece of code to make it working as I described above?

I uploaded my code here: https://codeshare.io/DxcNn



OneSignal now also supports to set a device as test device without doing something in the code. You can also download your own app from App Store and use it as test device. Just select you device from devices list one OneSignal and mark it as test device. You can find your device in the list by model, version and/or time added.

Answer Source

The sendTag method is from the device sdk. In your case iOS. https://documentation.onesignal.com/docs/ios-sdk-api#sendTag

You should do this anytime after initWithLaunchOptions in the app delegate. Updated code based on comments

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
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download