-1

I am new to azure notification hub. I tried from the documentation. But not able to do the name . Need some help on this .

Tried from the below link How to register devices to Azure Notification Hub from server side(with NodeJS sdk) ?

Not sure about the params.

var azure = require('azure-sb');

var notificationHubService = azure.createNotificationHubService('<Hub Name>','<Connection String>');
var payload={
        alert: 'Hello!'
      };

notificationHubService.createRegistrationId(function(error, registrationId, response){

      if(!error){
        console.log(response);
        console.log(registrationId);


        //RegistrationDescription registration = null;
        //registration.RegistrationId = registrationId;
        //registration.DeviceToken = req.body.token;
        notificationHubService.apns.createOrUpdateNativeRegistration(registrationId, req.body.token, req.token.upn, function(error, response){

            if(!error){
              console.log('Inside : createOrUpdateNativeRegistration' + response);

                notificationHubService.apns.send(null, payload, function(error){
                if(!error){
                  // notification sent

                  console.log('Success: Inside the notification send call to Hub.');
                  }
              });

            }
            else{
              console.log('Error in registering the device with Hub' + error);
            }

        });

      }
      else{
        console.log('Error in generating the registration Id' + error);
      }

  });

While creating registrationID which registration id i have to pass there. What is request.body.token and what is request.token.upn. I need it for apns

Mohit Verma
  • 5,140
  • 2
  • 12
  • 27

1 Answers1

1

While creating registrationId , you dont have to pass any id. **createRegistrationId(callback)** takes callback as a parameter which creates a registration identifier.

As per the overall implemetation:

/**
* Creates a registration identifier.
*
* @param {Function(error, response)} callback      `error` will contain information
*                                                  if an error occurs; otherwise, `response`
*                                                  will contain information related to this operation.
*/
NotificationHubService.prototype.createRegistrationId = function (callback) {
  validateCallback(callback);
  var webResource = WebResource.post(this.hubName + '/registrationids');
  webResource.headers = {
    'content-length': null,
    'content-type': null
  };
  this._executeRequest(webResource, null, null, null, function (err, rsp) {
    var registrationId = null;
    if (!err) {
      var parsedLocationParts = url.parse(rsp.headers.location).pathname.split('/');
      registrationId = parsedLocationParts[parsedLocationParts.length - 1];
    }
    callback(err, registrationId, rsp);
  });
};

Once you are done with RegistrationID Creation then you can call createOrUpdateRegistration(registration, optionsopt, callback) and here is the overall implementation for the same:

/**
* Creates or updates a registration.
*
* @param {string}             registration              The registration to update.
* @param {object}             [options]                 The request options or callback function. Additional properties will be passed as headers.
* @param {object}             [options.etag]            The etag.
* @param {Function(error, response)} callback           `error` will contain information
*                                                       if an error occurs; otherwise, `response`
*                                                       will contain information related to this operation.
*/
NotificationHubService.prototype.createOrUpdateRegistration = function (registration, optionsOrCallback, callback) {
  var options;
  azureutil.normalizeArgs(optionsOrCallback, callback, function (o, c) { options = o; callback = c; });
  validateCallback(callback);
  if (!registration || !registration.RegistrationId) {
    throw new Error('Invalid registration');
  }
  var webResource = WebResource.put(this.hubName + '/registrations/' + registration.RegistrationId);
  registration = _.clone(registration);
  var registrationType = registration[Constants.ATOM_METADATA_MARKER]['ContentRootElement'];
  delete registration[Constants.ATOM_METADATA_MARKER];
  delete registration.ExpirationTime;
  delete registration.ETag;
  if (!registration.Expiry) {
    delete registration.Expiry;
  }
  registration.BodyTemplate = '<![CDATA[' + registration.BodyTemplate + ']]>';
  var registrationXml = registrationResult.serialize(registrationType, registration);
  this._executeRequest(webResource, registrationXml, registrationResult, null, callback);
};

You can find the complete implementation of NotificationHubService.js here.

Hope it helps.

Mohit Verma
  • 5,140
  • 2
  • 12
  • 27
  • Thank you Mohit. Will it work for specific devices. How can i achieve the same. – Pritiranjan Mishra Oct 31 '19 at 06:21
  • Yes for the registered devices, you can send notification, i have added the link for the implementation of NotificationHubService as well. Please check and see if it helps. – Mohit Verma Oct 31 '19 at 06:24
  • Ok. So i have gone through the link . But did not find what i am looking for. Where i need to send the device token for registration and also let's say i have 1k customers , I need to send only to 200 customers. How do i do the same. Thank you for your help @mohit – Pritiranjan Mishra Oct 31 '19 at 12:55