node.js - 如何从服务器端(使用 NodeJS sdk)将设备注册到 Azure 通知中心?

标签 node.js azure windows-phone-8.1 azure-notificationhub azure-node-sdk

我正在开发 Windows Phone 8.1 应用程序 (RT),我正在尝试使用 Azure 通知中心推送通知。我可以使用可用的客户端 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)注册设备,实际上我在服务端有可用的设备 token ,该 token 是通过 API 调用从设备发送的。
  • 我想不时更新每个设备的多个标签。
  • 我想在用户请求时取消注册设备。
  • 我想使用模板向特定标签发送推送通知。

最佳答案

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

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

收到设备 token 后,这是服务器端代码。请注意,注册 ID、设备 token 、标签和回调函数是 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);
      }

  });

关于node.js - 如何从服务器端(使用 NodeJS sdk)将设备注册到 Azure 通知中心?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27033194/

相关文章:

c# - 在 FlipView 控件中捏合缩放和平移? Windows Phone 8.1 XAML Winrt 应用程序

node.js - Bitbucket 管道部署到 gCloud AppEngine 导致 [13] 发生内部错误

node.js - 间接关联的 Sequelize

Node.js 使用查询字符串路由到 url

c# - Microsoft 认知服务视觉 API 确实检测代码中的任何字符

c# - 使用 C# 通过 VNet 创建 Azure 容器实例

azure - 使用 Active Directory 客户端库通过电子邮件地址查找用户

c# - 重新打开时应用程序崩溃。 WP 8.1

node.js - 使用 MERN 堆栈和 socket.io 进行私有(private)聊天

emulation - Windows Phone 8.1 模拟器启动问题