Created
July 21, 2016 11:24
-
-
Save manishchaks/9464254c51a968dec96d325f982ba2ea to your computer and use it in GitHub Desktop.
Amazon Mobile Javascript SDK with Wavemaker demonstrating connectivity with Amazon Cognito, SNS and Mobile Analytics
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Application.$controller("MainPageController", ["$scope", function($scope) { | |
"use strict"; | |
/* perform any action on widgets/variables within this block */ | |
$scope.onPageReady = function() { | |
var authenticatedIdentityPoolID = "YOUR_AMAZON_AUTHENTICATED_IDENTITY_POOL_ID"; | |
var unauthenticatedIdentityPoolID = "YOUR_AMAZON_UNAUTHENTICATED_IDENTITY_POOL_ID"; | |
var twitterTokenKey = "YOUR_TWITTER_TOKEN_KEY"; | |
var twitterTokenSecret = "YOUR_TWITTER_TOKEN_SECRET"; | |
var params = { | |
IdentityPoolId: authenticatedIdentityPoolID, | |
Logins: { | |
'api.twitter.com': twitterTokenKey + ';' + twitterTokenSecret | |
} | |
}; | |
// set the Amazon Cognito region | |
AWS.config.region = 'us-east-1'; | |
// initialize the Credentials object with our parameters | |
AWS.config.credentials = new AWS.CognitoIdentityCredentials(params); | |
// We can set the get method of the Credentials object to retrieve | |
// the unique identifier for the end user (identityId) once the provider | |
// has refreshed itself | |
AWS.config.credentials.get(function(err) { | |
if (err) { | |
console.log("Error: " + err); | |
return; | |
} | |
console.log("Cognito Identity Id: " + AWS.config.credentials.identityId); | |
// Other service clients will automatically use the Cognito Credentials provider | |
// configured in the JavaScript SDK. | |
var cognitoSyncClient = new AWS.CognitoSync(); | |
cognitoSyncClient.listDatasets({ | |
IdentityId: AWS.config.credentials.identityId, | |
IdentityPoolId: authenticatedIdentityPoolID | |
}, function(err, data) { | |
if (!err) { | |
console.log(JSON.stringify(data)); | |
} | |
}); | |
var APP_TITLE = "test_ma_app" | |
var APP_VERSION_NAME = "1.1" | |
var APP_PACKAGE_NAME = "com.foo" | |
var APP_VERSION_CODE = "42" | |
var options = { | |
appId: 'YOUR_AMAZON_MOBILE_ANALYITCS_APP_ID', //Amazon Mobile Analytics App ID | |
appTitle: APP_TITLE, //Optional e.g. 'Example App' | |
appVersionName: APP_VERSION_NAME, //Optional e.g. '1.4.1' | |
appVersionCode: APP_VERSION_CODE, //Optional e.g. '42' | |
appPackageName: APP_PACKAGE_NAME //Optional e.g. 'com.amazon.example' | |
}; | |
var mobileAnalyticsClient = new AMA.Manager(options); | |
mobileAnalyticsClient.submitEvents(); | |
}); | |
// publish a simple message to Amazon SNS | |
var sns = new AWS.SNS(); | |
var params = { | |
Message: 'Hello world from WaveMaker!', | |
Subject: 'Hello world from Wavemaker!', | |
TopicArn: 'YOUR_TOPIC_ARN' | |
}; | |
sns.publish(params, function(err, data) { | |
if (err) console.log(err, err.stack); // an error occurred | |
else console.log(data); // successful response | |
}); | |
AWS.config.region = 'us-east-1'; | |
AWS.config.credentials = new AWS.CognitoIdentityCredentials({ | |
IdentityPoolId: 'YOUR_AMAZON_COGNITO_IDENTITY_POOL_ID' //Amazon Cognito Identity Pool ID | |
}) | |
}; | |
}]); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment