0

您好朋友,我正在尝试使用通知中心实现应用程序的应用程序后端注册。为了实现它,我正在使用通知中心关注此通知用户,但我想为 Windows Phone 进行注册,所以我尝试这样做并编写此代码在移动服务 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是什么;希望能得到一些回应。任何帮助,想法或建议表示赞赏。

4

0 回答 0