如何在Cordova移动设备中配置amazon SNS以接收推送通知

时间:2016-02-19 13:29:54

标签: cordova amazon-web-services push-notification ionic-framework amazon-sns

我正在使用离子框架开发Cordova应用程序。我想使用amazon-sns服务进行推送通知。

我在应用程序中为浏览器配置了amazon sdk。但是AWS.config.credentials.get仅在浏览器中执行移动应用程序时返回带有数据的成功对象

但它在Android手机中给出错误。它显示网络错误,认为网络连接并正常工作。

并且sns.createPlatformEndpoint方法也会提供凭据错误,您可以在屏幕截图中看到这两个错误。 enter image description here

这里也是代码快照

.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);
});

1 个答案:

答案 0 :(得分:1)

我没有得到实际问题,为什么它不是从离子移动应用程序调用。但我删除所有插件,然后尝试它开始工作。我有点想到一些插件在app中添加或删除了一些与网络相关的权限