Search code examples
node.jsazureazure-notificationhub

Issue with notification hub


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


Solution

  • 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.