Search code examples
qtqmlhttp-postonesignalv-play

sending OneSignal notification with QML


I am integrating OneSignal into my app built with Felgo, when building my app for test purposes I am able to manually send notifications from my OneSignal dashboard, but obviously I want these to be automatic when an event happens in app.

I am truly struggling to understand how to get this working, I have read through both:

and I think combining these would be how I would go about it? Something along the lines of:

AppButton {
    id: button
    onClicked: {
        //other onClicked actions,
        HttpRequest
        .post("https://onesignal.com/api/v1/notifications")
        .set('Content-Type', 'application/json')
        .send({ title: "post title", body: "post body" })
        .then(function(res) {
            console.log(res.status);
            console.log(JSON.stringify(res.header, null, 4));
            console.log(JSON.stringify(res.body, null, 4));
        })
        .catch(function(err) {
            console.log(err.message)
            console.log(err.response)
        });
    }
}

But how on earth would I go about sending to specific tags for targeted notifications?

In the Felgo OneSignal link above, they show that I can test push notifications with curl in the following way:

curl --include \
    --request POST \
    --header "Content-Type: application/json" \
    --header "Authorization: Basic <ONESIGNAL-REST-API-KEY>" \
    --data-binary '{
     "app_id": "<ONESIGNAL-APP-ID>",
     "contents": { "en": "Message" },
     "tags": [{"key": "userId", "relation": "=", "value": "1"}]
    }' \
    https://onesignal.com/api/v1/notifications

But outside of test purposes, how would I go assigning the specific tags and trigger a notification on a button press (or other event) within my app?

I understand all the information I should need to implement the notifications is there - but I cannot begin to make sense of it! :(

Any help would be massively appreciated as even when reading through documentation I am struggling.


Solution

  • I have got this working using the code below, what is shown is the end result of a signal sent when an AppButton is pressed elsewhere in my app.

    I do not have my own web service to host this my notifications through, but for security reasons my REST key should not be in my code.

    To combat this I have added my One Signal Rest API Key to a branch in my firebase database (which I am using in my app also), which can only be accessed by authorised users, this is then downloaded on the instance the request is made, and changed to a different string after.

    property var osKey
    
    firebaseDb.getValue("keys/OSkey", {
                             }, function(success, key, value) {
                                 if(success){
                                 osKey = value;
                                 HttpRequest
                                   .post("https://onesignal.com/api/v1/notifications")
                                   .set('Content-Type', 'application/json')
                                   .set("Authorization", "Basic " + osKey)
                                   .send({
                                       "app_id": "<MY_APP_ID>",
                                       "contents": { "en": "MESSAGE" },
                                       "tags":  [
                                       // the specific tags I want to send too
                                           {"key": "groupAdmin", "relation": "=", "value": "1"},
                                           {"key": "group", "relation": "=", "value": groupName}
                                       ]
                                   })
                                   .then(function(res) {
                                      console.log(res.status);
                                      console.log(JSON.stringify(res.header, null, 4));
                                      console.log(JSON.stringify(res.body, null, 4));
                                   })
                                   .catch(function(err) {
                                      console.log(err.message)
                                      console.log(err.response)
                                   });
                                }
                             })
                osKey = "Nothing to see here"
    

    I understand for securities sake, this still may not be the most secure, and again - if anyone can tell me how to improve this it would be a massive help!

    Thanks