My app (Ionic 4) receive notification through onesignal native plugin. I used onesignal online desk to test the app, and the onesignal API with postman to send notification via api. All ok.
Then I made a simple utility to send the notifications, using HTTP (cordova-plugin-advanced-http 2.0.9 "Advanced HTTP plugin").
w/o any problem about header and auth but every time I got from the onesignal API server (that run correcly with Postman) the errorcode 400 with description:
{“errors”:[“contents must be key/value collections by language code”]}
This is my code (run() is a function called by a click of a button, url and headers are defined as constant):
Try inspecting what’s really being sent to the OneSignal API from your app. You can do that by running in a local browser like Chrome with ˋionic serveˋ, open the developer tools (usually with F12) and choose the “Network” tab. There you can see request headers, payload and reply for every request sent from the browser app.
I assume there is some kind of translation happening when Angular serializes your “body” object into raw post data, e.g. escaping the double quotes.
Thanks for help, Alex.
Unfortunately I’m not able to catch the real message with Chrome, I’m lacking of some knowledge, I’m afraid. I can see only messages regarding the load of the app into the test device; if I try with ionic serve, not on device, I can’t run this application because of the cordova need:
Native: tried calling HTTP.post, but Cordova is not available. Make sure to include cordova.js or run in a device/simulatorcacth only
I’m pretty sure that the problem is into the {"en": "Message"} that actually is {en: "Message"} instead, but I cant understand why! As you can see above, body is correct.
Try using HttpClient from Angular instead of HTTP, this should run in the browser.
And what about your http headers? Is the Content-Type set appropriately?
If you think about bringing your app to other devices than yours, you should be aware that your OneSignal API key is not safe on the device. This API is intended for server-to-server communication.
a little annoys me to move to HttpClient (not native, I imagine) only for readout the real message. I’ll try something as send the post to a dummy server.
Pietro
PS: I know that key will be into the app, but it is only a utility for internal use (people that use it also have to know the key)
Fixed. The problem was that json is not the standard serialize, so the app needs a http.setDataSerializer('json'); in the constructor to work properly.