如何从服务器端(使用NodeJS sdk)向Azure Notification Hub注册设备?

Ame*_*had 6 azure node.js azure-notificationhub azure-node-sdk windows-phone-8.1

我正在开发Windows Phone 8.1 App(RT),我正在尝试使用Azure Notification Hub推送通知.我可以使用客户端SDK提供.但我想从服务器端进行设备注册,标记等.我在http://blogs.msdn.com/b/azuremobile/archive/2014/04/08/push-notifications-using-notification-hub-and-net-backend.aspx上看到了.Net后端的一个很好的指南.我在后端服务器端使用NodeJS.任何人都可以使用示例代码帮助我.

  • 我想从服务器端(iPhone,Android和Windows Phone)注册设备,实际上我在服务器端有设备令牌,这是通过API调用从设备发送的.
  • 我想不时地为每个设备更新多个标签.
  • 我想在用户请求时取消注册设备.
  • 我想使用模板将推送通知发送到特定标签.

Emb*_*der 6

注册设备令牌并使用node.js中的通知中心发送通知的步骤如下:

  1. 创建注册ID
  2. 创建注册
  3. 发送通知

收到设备令牌后,这是服务器端代码.请注意,注册ID,设备令牌,标记和回调函数是notificationHubService.apns.send调用的必需参数.

这是代码片段:

var azure = require('azure');

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

  });
Run Code Online (Sandbox Code Playgroud)


efi*_*ndr 0

查看服务器端的开源 SDK 。我从未尝试过,但应该没问题,因为任何 SDK 只是REST API的包装器。