Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Question] How to send data for broadcastMessage API? #26

Closed
alimustofa opened this issue May 3, 2017 · 4 comments
Closed

[Question] How to send data for broadcastMessage API? #26

alimustofa opened this issue May 3, 2017 · 4 comments

Comments

@alimustofa
Copy link

Hi @evanshortiss, I am using broadcastMessage to publish news, but I want to send some data to the mobile device (for example id). Is it possible?

Please help me, thanks

@evanshortiss
Copy link
Owner

evanshortiss commented May 3, 2017

Hi @alimustofa,

You can use broadcastMessage, or alternatively it would be more efficient to call subscribe(endpointArn, topicArn, callback) when you register each user the first time.

Then you just send a single message to the topic and every user will get it. Here's a pseudo code example:

const SNS = require('sns-mobile');

const SNS_KEY_ID = process.env['SNS_KEY_ID'];
const SNS_ACCESS_KEY = process.env['SNS_ACCESS_KEY'];
const ANDROID_ARN = process.env['SNS_ANDROID_ARN'];


const androidApp = new SNS({
  platform: SNS.SUPPORTED_PLATFORMS.ANDROID,
  region: 'eu-west-1',
  apiVersion: '2010-03-31',
  accessKeyId: SNS_KEY_ID,
  secretAccessKey: SNS_ACCESS_KEY,
  platformApplicationArn: ANDROID_ARN,
  //sandbox: true (This is required for targetting (iOS) APNS_SANDBOX only)
});

const BROADCAST_TOPIC = 'SOME_TOPIC_ID_FROM_SNS_DASHBOARD';

exports.registerUser = function (deviceId, callback) {
  // first register the user
  androidApp.addUser(deviceId, {}, function(err, endpointArn) {
    if(err) {
      callback(err);
    } else {
      // now subscribe the user to the broadcast topic (like a channel)
      androidApp.subscribe(endpointArn, BROADCAST_TOPIC, callback);
    }
  });
};

exports.sendBroadcastMessage = function (msg, callback) {
  // every user will get this message
  androidApp.publishToTopic(BROADCAST_TOPIC, msg, callback);
};

You can read about the msg format for topic here. You can include custom data in this message such as id like you asked.

@evanshortiss
Copy link
Owner

If you have any further problems you can reopen this @alimustofa

@alimustofa
Copy link
Author

But is it possible use broadcastMessage? Can I modify msg format to add custom data from broadcastMessage API?

@alimustofa
Copy link
Author

Ooops, solved @evanshortiss
Thanks 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants