如何在app-backend中注册带有通知中心的Window phone

时间:2013-10-10 20:39:18

标签: windows azure windows-phone-8 notifications push-notification

朋友您好我正在尝试使用通知中心实现应用后端注册应用程序。为了实现它,我正在关注此notify user with notification hub,但我想为Windows手机注册,所以我试图这样做在移动服务Api中编写此代码

exports.post = function(request, response) {
// Use "request.service" to access features of your mobile service, e.g.:
//   var tables = request.service.tables;
//   var push = request.service.push;

var azure = require('azure');
var hub = azure.createNotificationHubService('samplenotificationhub', 
    'full access key');

var platform = request.body.platform;

var installationId = request.header('X-ZUMO-INSTALLATION-ID');

var registrationComplete = function(error, registration) {
    if (!error) {
        // Return the registration.
        response.send(200, registration);
    } else {
        response.send(500, 'Registration failed!');
    }
}

// Function called to log errors.

var logErrors = function(error) {
    if (error) {
        console.error(error)
    }
}

hub.listRegistrationsByTag(installationId, function(error, existingRegs) {
    var firstRegistration = true;
    if (existingRegs.length > 0) {
         for (var i = 0; i < existingRegs.length; i++) {
            if (firstRegistration) {
                // Update an existing registration.
                if (platform === 'wp') {
                    existingRegs[i].ChannelUri = request.body.channelUri;                        
                    hub.updateRegistration(existingRegs[i], registrationComplete);                        
                } else {
                    response.send(500, 'Unknown client.');
                }
                firstRegistration = false;
            } else {
                // We shouldn't have any extra registrations; delete if we do.
                hub.deleteRegistration(existingRegs[i].RegistrationId, logErrors);
            }
        }
    } else {
        // Create a new registration.

        if (platform === 'wp') {
            hub.mpns.createNativeRegistration(request.body.channelUri, 
            [request.body.CurrentDate], registrationComplete);
        }  
        else {
            response.send(500, 'Unknown client.');
        }
    }
});


};

我可以在我的应用程序中通过此代码获取api调用..

private async Task AcquirePushChannel()
    {
        CurrentChannel = HttpNotificationChannel.Find("mychannel");

        string message;

        if (CurrentChannel == null)
        {
            CurrentChannel = new HttpNotificationChannel("mychannel");
            CurrentChannel.Open();
            CurrentChannel.BindToShellTile();
            CurrentChannel.BindToShellToast();

        }


        var body = new NotificationRequest
        {
            channelUri = CurrentChannel.ChannelUri.ToString(),
            platform = "wp",
            CurrentDate = "1",
        };

        try
        {
            // Call the custom API POST method with the supplied body.
            var result = await App.MobileService
                .InvokeApiAsync<NotificationRequest,
                RegistrationResult>("registrationapi", body,
                System.Net.Http.HttpMethod.Post, null);


            // Set the response, which is the ID of the registration.
            message = string.Format("Registration ID: {0}", result.RegistrationId);

            registrationid = result.RegistrationId;
        }
        catch (MobileServiceInvalidOperationException ex)
        {
            message = ex.Message;
        }

我在移动服务信息中心看到了一个活跃的api调用,但无法从API获得响应。 我已经在我的表脚本中编写了这段代码,这样我就可以将推送通知发送到我的手机了。如果有任何问题,请查看。

function insert(item, user, request) {
var azure = require('azure');
var hub = azure.createNotificationHubService('samplenotificationhub', 
'listen signature string');




// Create the payload for a Windows Store app.
    var wnsPayload = '<toast><visual><binding template="ToastText02"><text id="1">New item added:</text><text id="2">' + "tanuj" + '</text></binding></visual></toast>';
var Toasttemplate = "<?xml version=\"1.0\" encoding=\"utf-8\"?>" + "<wp:Notification xmlns:wp=\"WPNotification\">" +"<wp:Toast>" +"<wp:Text1>$(" + "1" + ")</wp:Text1>" +"</wp:Toast> " +"</wp:Notification>";

// Execute the request and send notifications.
request.execute({
    success: function() {
        // Write the default response and send a notification 
        // to the user on all devices by using the userId tag.
        request.respond();


      hub.wpns.send("1", Toasttemplate, 'wpns/toast', function(error) {
        if (error) {
            console.log(error);
        }
    });
}

});

我知道这是很多代码我放这个因为链接没有提到wp所以只是想确保我做得对。 另请先告诉我var installationId = request.header('X-ZUMO-INSTALLATION-ID')中的 INSTALATIONID ; 希望得到一些回应。任何帮助,想法或建议都表示赞赏。

0 个答案:

没有答案