3

I am trying to push notification to ios from quickblox js sdk. I have some sample code. QuickBlox JavaScript SDK : 2.12.7

'use strict';

const QuickBlox = require('quickblox').QuickBlox;

const CREDENTIALS = {
  appId: 'appId',
  authKey: 'authKey',
  authSecret: 'authSecret'
};
const QB = new QuickBlox();

QB.init(CREDENTIALS.appId, CREDENTIALS.authKey, CREDENTIALS.authSecret);

function createSession() {
  return new Promise((resolve, reject) => {
    QB.createSession(function(err, result) {
      if (err) {
        console.log(err);
        reject(err);
      }
      resolve(result);
    });
  });
}

function pushNotification(userIds, message) {
  const params = {
    notification_type: 'push',
    push_type: 'apns',
    user: {ids: userIds},
    environment: 'development',
    message: QB.pushnotifications.base64Encode(message)
  };

  return new Promise((resolve, reject) => {
    QB.pushnotifications.events.create(params, function(err, response) {
      if (err) {
        console.log(err);
        reject(err);
      }
      resolve(response);
    });
  });
}

function loginUser(email, password) {
  const params = { email, password };

  return new Promise((resolve, reject) => {
    QB.login(params, function(err, user){
      if (user) {
        resolve(user);
      } else {
        reject(err);
      }
    });
  });
}


const iosMessage = {
  "aps" : {
    "alert" : "You got your emails.",
    "badge" : 9,
    "sound" : "bingbong.aiff"
  },
  "acme1" : "bar",
  "acme2" : 42
};

(async () => {
  try {
    await createSession();
    await loginUser('email', 'password');
    const send = await pushNotification(['userId'], JSON.stringify(iosMessage));
    console.log(send);
  } catch (err) {
    console.log(err);
  }
})();

It successfully sends messages I got this response.

{
  "event": {
  "id": 31147883,
      "event_type": "one_shot",
      "message": "payload=eyJhcHMiOnsiYWxlcnQiOiJ7XCJhcHNcIjp7XCJhbGVydFwiOlwiWW91IGdvdCB5b3VyIGVtYWlscy5cIixcImJhZGdlXCI6OSxcInNvdW5kXCI6XCJiaW5nYm9uZy5haWZmXCJ9LFwiYWNtZTFcIjpcImJhclwiLFwiYWNtZTJcIjo0Mn0iLCJzb3VuZCI6ImRlZmF1bHQifX0=",
      "date": null,
      "period": null,
      "name": null,
      "occured_count": 0,
      "created_at": "2019-12-16T20:50:14Z",
      "updated_at": "2019-12-16T20:50:14Z",
      "end_date": null,
      "active": true,
      "application_id": 63650,
      "user_id": 35292731,
      "kind": "API",
      "environment": "development",
      "tag_query": null,
      "notification_channel": {
      "name": "apns"
    }
  }
}

The problem is that this base64 string payload=eyJhcHMiOnsiYWxlcnQiOiJ7... looks like this

{
  "aps": {
    "alert": "{\"aps\":{\"alert\":\"You got your emails.\",\"badge\":9,\"sound\":\"bingbong.aiff\"},\"acme1\":\"bar\",\"acme2\":42}",
    "sound": "default"
  }
}

and ios mobile phone get this ugly string "{\"aps\":{\"alert\":\"You got your emails.\",\"badge\":9,\"sound\":\"bingbong.aiff\"},\"acme1\":\"bar\",\"acme2\":42}" as notification. How correctly send to ios js object? I try different keys in an object but always get a stringified response on ios mobile.

1 Answers1

0

To achieve iOS only push notification base64 encoded string should be prepended with "payload=" like it is shown in QuickBlox REST API Platform based push notification section.

Try using this:

event[message]=payload=ew0KICAgICJhcHMiIDogew0KICAgICAgICAiYWxlcnQiIDogIllvdSBnb3QgeW91ciBlbWFpbHMuIiwNCiAgICAgICAgImJhZGdlIiA6IDksDQogICAgICAgICJzb3VuZCIgOiAiYmluZ2JvbmcuYWlmZiINCiAgICB9LA0KICAgICJhY21lMSIgOiAiYmFyIiwNCiAgICAiYWNtZTIiIDogNDINCn0=

BTW, using Universal Push Notifications you can achieve the same behavior but push notification will be delivered to more platforms.

To send universal push notification you should make a small change:

const iosMessage = {
  message: "You got an email.",
  ios_badge: 9,
  ios_sound: "bingbong.aiff"
  acme1: "bar",
  acme2: 42
};
const params = {
  notification_type: 'push',
  user: {ids: userIds},
  environment: 'development',
  message: QB.pushnotifications.base64Encode(JSON.stringify(iosMessage))
};
QuickBlox
  • 233
  • 1
  • 11