angularjsjsonnode.jsibm-cloudibm-mobile-services

IBM Push Notification: Invalid Json


I try to using the https://mobile.ng.bluemix.net/imfpush/v1/apps/{appID}/devices/{deviceID} to push notification on this site: https://mobile.ng.bluemix.net/imfpush/#/, but I have a error message

body:

{ "alert" : "You have a request for payment."}

error message:

400 {
  "code": "FPWSE0004E",
  "message": "Bad Request - Invalid JSON."
}

and try to using by calling https request on nodejs

In my code:

var message = {
            alert : "You have a request for payment.",
            url : "payment_id"
        };

var headers = {
            'Content-Type' : 'application/json',
            'appSecret': 'xxxxxx',
             "clientSecret": "xxxxxxxx"
            'Content-Length' : Buffer.byteLength(message, 'utf8')
        };

var options = {
                            host : 'mobile.ng.bluemix.net',
                            port : 443,
                            path : '/imfpush/v1/apps/'+appId +'/devices/'+deviceId,
                            method : 'PUT',
                            headers : headers,
                            data:   JSON.stringify(message)
                        };

var reqPost = https.request(options, function(res) {
            res.on('data', function(d) {
                 console.info('PUT result:\n');                  
                 process.stdout.write(d);
                 console.info('\n\PUTcompleted');
                    });
                });

reqPost.end();

and call API by postman, I get an error message: cannot get any response. Please give me your thoughts.


Solution

  • It looks like you are using the wrong endpoint to send a push notification.

    Here's a very simple example of sending a push notification to a device using the lightweight request library, superagent, in Node.

    var request = require('superagent');
    
    var baseUrl = 'https://mobile.ng.bluemix.net';
    
    var message = {
      "message": {
        "alert": "Notification alert message"
      }
    };
    
    request
      .post(baseUrl + '/imfpush/v1/apps/' + appId + '/messages')
      .send(message)
      .set('Content-Type', 'application/json')
      .set('Accept', 'application/json')
      .set('appSecret', appSecret)
      .end(function(err, res){
        console.log(res.body) || console.log(err);
      });
    

    You just need to set the appID and appSecret (probably externally).

    Or, if you want to send to device IDs, just change the body of the message:

    var message = {
      "message": {
        "alert": "Notification alert message"
      },
      "target": {
        "deviceIds": [
          deviceID
        ]
      }
    };
    

    And set the deviceID.