我是天青通知中心的新手。我从文档中尝试过。但无法做到这个名字。需要一些帮助。
尝试以下链接 How to register devices to Azure Notification Hub from server side(with NodeJS sdk) ?
不确定参数。
var azure = require('azure-sb');
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);
}
});
在创建registrationID时,我必须将那里的注册ID传递给我。什么是request.body.token和什么是request.token.upn。我需要APNS
答案 0 :(得分:0)
在创建registrationId时,您不必传递任何ID。 **createRegistrationId(callback)**
将回调作为创建注册标识符的参数。
根据总体实现:
/**
* Creates a registration identifier.
*
* @param {Function(error, response)} callback `error` will contain information
* if an error occurs; otherwise, `response`
* will contain information related to this operation.
*/
NotificationHubService.prototype.createRegistrationId = function (callback) {
validateCallback(callback);
var webResource = WebResource.post(this.hubName + '/registrationids');
webResource.headers = {
'content-length': null,
'content-type': null
};
this._executeRequest(webResource, null, null, null, function (err, rsp) {
var registrationId = null;
if (!err) {
var parsedLocationParts = url.parse(rsp.headers.location).pathname.split('/');
registrationId = parsedLocationParts[parsedLocationParts.length - 1];
}
callback(err, registrationId, rsp);
});
};
完成RegistrationID创建后,您可以调用createOrUpdateRegistration(registration, optionsopt, callback)
,这是相同的总体实现:
/**
* Creates or updates a registration.
*
* @param {string} registration The registration to update.
* @param {object} [options] The request options or callback function. Additional properties will be passed as headers.
* @param {object} [options.etag] The etag.
* @param {Function(error, response)} callback `error` will contain information
* if an error occurs; otherwise, `response`
* will contain information related to this operation.
*/
NotificationHubService.prototype.createOrUpdateRegistration = function (registration, optionsOrCallback, callback) {
var options;
azureutil.normalizeArgs(optionsOrCallback, callback, function (o, c) { options = o; callback = c; });
validateCallback(callback);
if (!registration || !registration.RegistrationId) {
throw new Error('Invalid registration');
}
var webResource = WebResource.put(this.hubName + '/registrations/' + registration.RegistrationId);
registration = _.clone(registration);
var registrationType = registration[Constants.ATOM_METADATA_MARKER]['ContentRootElement'];
delete registration[Constants.ATOM_METADATA_MARKER];
delete registration.ExpirationTime;
delete registration.ETag;
if (!registration.Expiry) {
delete registration.Expiry;
}
registration.BodyTemplate = '<![CDATA[' + registration.BodyTemplate + ']]>';
var registrationXml = registrationResult.serialize(registrationType, registration);
this._executeRequest(webResource, registrationXml, registrationResult, null, callback);
};
您可以找到NotificationHubService.js here的完整实现。
希望有帮助。