3
我正在開發使用離子框架的Cordova應用程序。我想使用amazon-sns服務進行推送通知。如何在Cordova mobile中配置亞馬遜SNS以接收推送通知
我已經爲應用程序中的瀏覽器配置了amazon sdk。但AWS.config.credentials.get
在瀏覽器中執行移動應用程序時只返回成功對象和數據,
但是它給android手機中的錯誤。它顯示網絡錯誤,認爲網絡連接和工作。
和sns.createPlatformEndpoint
方法也給予憑據錯誤,你可以在截圖中看到兩個錯誤。
這裏是代碼卡以及
.run(function($cordovaPush,$rootScope) {
var registerForSNS = function(gcmId){
var params = {
PlatformApplicationArn: 'my amazon arn', /* required */
Token: gcmId, /* required */
CustomUserData: 'STRING_VALUE'
};
sns.createPlatformEndpoint(params, function(err, data) {
if (err)
console.log(err, err.stack); // an error occurred
else{
console.log(data.EndpointArn);
alert(data.EndpointArn);
}
});
}
AWS.config.region = 'us-east-1';
AWS.config.update({
credentials : new AWS.CognitoIdentityCredentials({
IdentityPoolId: 'my Identity pool ID'
})
});
var sns = new AWS.SNS();
AWS.config.credentials.get(function(err) {
if (err){
console.log(err);
} else{
console.log(AWS.config.credentials);
registerForSNS();
}
});
document.addEventListener("deviceready", function(){
var androidConfig = {
"senderID": "my sender id",
};
$cordovaPush.register(androidConfig).then(function(result) {
// Success
alert(JSON.stringify(result));
}, function(err) {
// Error
alert(JSON.stringify(err));
})
$rootScope.$on('$cordovaPush:notificationReceived', function(event, notification) {
switch(notification.event) {
case 'registered':
if (notification.regid.length > 0) {
alert('registration ID = ' + notification.regid);
registerForSNS(notification.regid);
}
break;
case 'message':
// this is the actual push notification. its format depends on the data model from the push server
alert('message = ' + notification.message + ' msgCount = ' + notification.msgcnt);
break;
case 'error':
alert('GCM error = ' + notification.msg);
break;
default:
alert('An unknown GCM event has occurred');
break;
}
});
}, false);
});