OneSignal iOS SDK send notification fail Code 400

有些话、适合烂在心里 提交于 2019-12-24 08:01:35

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/41755921/onesignal-ios-sdk-send-notification-fail-code-400

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!