OneSignal iOS SDK send notification fail Code 400
Asked Answered
D

1

1

What I have done:

I have created a simple iOS native App which has nothing but OneSignal initializaion codes. I installed my App on my 2 devices, device A and device B. Both devices are subscribed to my OneSignal App Console. When I send a notification from OneSignal Web Console, the 2 devices can both recive the notification.

What I want to do:

When I click on a button on device A, device A will send a notification to device B.

What is the problem:

I followed the OneSignal iOS SDK document and created the following function https://documentation.onesignal.com/docs/ios-native-sdk#section-sending-notifications

func onButtonClick() {
    OneSignal.postNotification(
        [
            "contents": ["en": "Test Message"],
            "include_player_ids": ["c00bb8a6-79da-419b-9999-e919831a5223"] // id of device B
        ],
        onSuccess: {(result) in print("success") },
        onFailure: {(error) in print("error : \(error)") }
    )
}

however, when I clicked the button, an error shows up in the xCode debug console:

2017-01-20 11:15:52.702852 my.bundle.id[2263:1201122] ERROR: Create notification failed
error : Optional(Error Domain=OneSignalError Code=400 "(null)" UserInfo={returned={
    errors =     (
        "Please include a case-sensitive header of Authorization: Basic <YOUR-REST-API-KEY-HERE> with a valid REST API key."
    );
    reference =     (
        "https://documentation.onesignal.com/docs/accounts-and-keys#section-keys-ids"
    );
}})

I was confused, because according to the OneSignal Reference document, send notification with "include_player_ids" Does not require API Auth Key.

https://documentation.onesignal.com/reference#create-notification

And it also says: NEVER use your 'REST API key' in client code, it is intended for use on your system or server only.

So what does this error mean? And what should I do?

I even copied and run this "work without any problems" code from this discussion, but the same Authorization error still shows up. What am I doing wrong?

Dreadnought answered 20/1, 2017 at 4:4 Comment(0)
D
2

The Answer

Just upgrade the SDK and the problem will be gone.

This was a bug in OneSignal-iOS-SDK 2.3.1, this bug was fixed in 2.3.2.

Please see their Github Release Notes.

How to avoid this kind of problems

Always run pod repo update before pod install to ensure that the pod install the latest version of the frameworks.

Thanks Kevin, Jason and Josh from One Signal Conversations to help me solve this problem.

Dreadnought answered 24/1, 2017 at 5:29 Comment(2)
Are you sure you are referring to the iOS SDK here? Cause the current release is 2.13.1. This recommendation does not solve the issue.Pleura
@TennyPinheiro This SO question was asked in Jan 2017, I'm sure a lot of things have changed now. Maybe you can contact OneSignal Support team or create another question in SO.Dreadnought

© 2022 - 2024 — McMap. All rights reserved.