【问题标题】:Issue with notification hub通知中心问题
【发布时间】:2020-02-24 08:25:31
【问题描述】:

我是 Azure 通知中心的新手。我从文档中尝试过。但不能做名字。在这方面需要一些帮助。

从以下链接尝试 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);
      }

  });

在创建registrationID时,我必须在那里传递哪个注册ID。什么是 request.body.token,什么是 request.token.upn。我需要它用于 apns

【问题讨论】:

    标签: node.js azure azure-notificationhub


    【解决方案1】:

    在创建 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

    希望对你有帮助。

    【讨论】:

    • 谢谢莫希特。它是否适用于特定设备。我怎样才能做到这一点。
    • 是的,注册设备可以发送通知,我也添加了NotificationHubService实现的链接。请检查一下是否有帮助。
    • 好的。所以我已经浏览了链接。但没有找到我要找的东西。我需要发送设备令牌进行注册,假设我有 1k 个客户,我只需要发送给 200 个客户。我该怎么做。感谢您的帮助@mohit
    猜你喜欢
    • 2023-03-28
    • 1970-01-01
    • 1970-01-01
    • 2014-01-18
    • 2017-01-09
    • 1970-01-01
    • 2016-03-26
    • 2018-03-29
    • 1970-01-01
    相关资源
    最近更新 更多