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

时间:2014-11-20 06:30:40

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

我正在开发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调用从设备发送的。
  • 我想不时为每个设备更新多个标签。
  • 我想在用户请求时取消注册设备。
  • 我想使用模板向特定标签发送推送通知。

2 个答案:

答案 0 :(得分:6)

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

  1. 创建注册ID
  2. 创建注册
  3. 发送通知
  4. 这是服务器端代码,一旦收到设备令牌。请注意,注册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);
          }
    
      });
    

答案 1 :(得分:0)

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

相关问题