3

I was trying to use the REST API to cancel a pending notification that I sent to a single user also using the API (with include_player_ids and a single recipient), however I can not do this without passing the REST API KEY, which should not happen, because in the manual it says in the conditions I am sending this was not necessary.

Am I doing something wrong or maybe it's a problem with the OneSignal's service? Here is the code:

//Creates the notification
$.post(
    "https://onesignal.com/api/v1/notifications",
    {
        "app_id": appId,
        "include_player_ids": [userId],
        "send_after": later,
        "template_id": templateId
    },
    function(data){
        localStorage.ni = data.id;
    }
);

//Cancel the notification
var notificationId = localStorage.ni;
$.get("https://onesignal.com/api/v1/notifications/"+notificationId+"?app_id="+appId);

Here is the response for the cancel request:

{
    "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"
        ]
}
João Gabriel
  • 41
  • 1
  • 7

1 Answers1

1

The answer was easier than I imagined. To exclude a notification, I can not use $.get(), I have to explicitly say that I want to delete them.

So, this worked fine:

$.ajax({
    url: "https://onesignal.com/api/v1/notifications/"+notificationId+"?app_id="+appId,
    type: "DELETE"
});
João Gabriel
  • 41
  • 1
  • 7
  • How to delete more than one notification per request? Like, ID001, ID002, ID003? – Bruno Freire Sep 14 '21 at 20:24
  • Except this doesn't actually DELETE the notification. It only cancels it by setting the "canceled" property. The notification still stays in the complete list of notifications. – Sámal Rasmussen May 24 '22 at 12:05