带有通知中心的问题

问题描述 投票:0回答:1

我是天青通知中心的新手。我从文档中尝试过。但不能做到这个名字。需要一些帮助。

从以下链接尝试How to register devices to Azure Notification Hub from server side(with NodeJS sdk) ?

不确定参数。

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);
      }

  });

创建注册ID时,我必须将注册ID传递到那里。什么是request.body.token和什么是request.token.upn。我需要APNS

node.js azure azure-notificationhub
1个回答
0
投票

虽然创建registrationId,但您不必传递任何ID。 **createRegistrationId(callback)**将回调作为创建注册标识符的参数。

根据总体实现:

/**
* 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);
  });
};

一旦完成RegistrationID创建,就可以调用createOrUpdateRegistration(registration, optionsopt, callback),这是相同的整体实现:

/**
* 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);
};

您可以找到NotificationHubService.js here的完整实现。

希望有帮助。

© www.soinside.com 2019 - 2024. All rights reserved.