6

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

2 回答 2

7

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

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

这是服务器端代码,一旦收到设备令牌。注意注册ID、Device Token、Tag和Callback函数是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);
      }

  });
于 2015-01-27T19:06:02.147 回答
0

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

于 2014-11-22T00:19:20.217 回答