parse-community / Parse-SDK-Android

The Android SDK for Parse Platform
https://parseplatform.org/
Other
1.89k stars 735 forks source link

push notification is sent 4 times but still times out #689

Closed mginz83 closed 7 years ago

mginz83 commented 7 years ago
Parse.Cloud.define('sendAnnouncement', function(request, response) {

var personSending = request.params.sendingPerson;
var personGetting = request.params.sendToPerson;

Parse.Cloud.httpRequest({
  method: 'POST',
  url: 'https://onesignal.com/api/v1/notifications',
  headers: {
    'Content-Type': 'application/json;charset=utf-8',
    "Authorization": "Basic REST_API_KEY"
  },
  body: {
    app_id: "APP_KEY", 
    include_player_ids: [personGetting],
    contents: {en: "English Message"},
  }
    }).then(function(httpResponse) {
        console.log(httpResponse.text);
    }, function(httpResponse) {
        console.error('Request failed with response code ' + httpResponse.status);
    });

});

So the above code will work. However, I get a total of 4 pushes on my device and still an i/o error.

Any reason why????

flovilmart commented 7 years ago

You're using the onesignal API, I'M not sure what about parse-server is there. perhaps that's just because you don't send response.success() / response.error()

mginz83 commented 7 years ago

Thats exactly what was wrong.

For those who find this later on. Here is my soluation to use OneSignal for push notifications. This code is meant for my friending system so it sends the notification to a specific person.

Parse.Cloud.define('replyFriendRequest', function(request, response) {

    var personSending = request.params.sendingPerson;
    var personGetting = request.params.sendToPerson;

    Parse.Cloud.httpRequest({ 
        url: "https://onesignal.com/api/v1/notifications", 
        method: "POST", 
        headers: {
            'Content-Type': 'application/json;charset=utf-8',
            "Authorization": "Basic REST_KEY"
        }, 
        body: {
            app_id: "APP_ID", 
            include_player_ids: [personGetting],
            headings: {en: "New Friend!"},
            contents: {en: personSending + " accepted your friend request!"}
        }, 
        success: function(httpResponse) { 
            response.success("sent"); 
        }, 
        error: function(httpResponse) { 
            response.error('Failed with: ' + httpResponse.status); 
        } 
    });
});