在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称(OpenSource Name):evanshortiss/sns-mobile开源软件地址(OpenSource Url):https://github.com/evanshortiss/sns-mobile开源编程语言(OpenSource Language):JavaScript 98.6%开源软件介绍(OpenSource Introduction):Maintainer(s)I no longer use this and haven't got the capacity to maintain it. If you'd like to be a maintainer of this library please get in touch. A current active fork is available at https://github.com/kalisio/sns-mobile (have a look to #34 for some details). SNS Mobile PushModule to make interacting with mobile push notifications for iOS and Android easier. Wraps the Amazon aws-sdk node module. The idea is that you can create an object to represent each Platform Application you plan to use and remove the excess features that aren't needed for Android and iOS applications. Installationnpm install sns-mobile Setting Up a User for SNSTo use Amazon SNS you need a Secret Access Key and an Access Key Id. Getting these will require you to create a user under the IAM section of AWS and attach a User Policy with the following Policy Document: Disclaimer: I'm not an AWS ninja, this configuration may be too liberal but it works!
Simple Code Example for AndroidThe below example creates an SNS instance for an Android application identified by a PlatformApplicationArn. var SNS = require('sns-mobile'),
EVENTS = SNS.EVENTS;
var SNS_KEY_ID = process.env['SNS_KEY_ID'],
SNS_ACCESS_KEY = process.env['SNS_ACCESS_KEY'],
ANDROID_ARN = process.env['SNS_ANDROID_ARN'];
var androidApp = new SNS({
platform: SNS.SUPPORTED_PLATFORMS.ANDROID,
region: 'eu-west-1',
apiVersion: '2010-03-31',
accessKeyId: SNS_KEY_ID,
secretAccessKey: SNS_ACCESS_KEY,
platformApplicationArn: ANDROID_ARN,
//sandbox: true (This is required for targetting (iOS) APNS_SANDBOX only)
});
// Add a user, the endpointArn is their unique id
// endpointArn is required to send messages to the device
androidApp.addUser('some_fake_deviceid_that_i_made_up', JSON.stringify({
some: 'extra data'
}), function(err, endpointArn) {
if(err) {
throw err;
}
// Send a simple String or data to the client
androidApp.sendMessage(endpointArn, 'Hi There!', function(err, messageId) {
if(err) {
throw err;
}
console.log('Message sent, ID was: ' + messageId);
});
}); Running TestsSNS_KEY_ID="<your_sns_key_id>" SNS_ACCESS_KEY="<your_sns_access_key>" SNS_ANDROID_ARN="arn:aws:sns:..." npm test Events EmittedInstances created will emit events as listed below and callbacks should have the shown format. Event strings can be accessed as shown below.
broadcastStart
Emitted prior to broadcasting the first message. broadcastEnd
Emitted once all messages are broadcast. messageSent
Emitted when a message sends successfully. userDeleted
When a user is deleted this is emitted. sendFailed
If a message fails to send this is emitted. addUserFailed
Fired when adding a user fails. userAdded
When a user is added this is emitted. attributesUpdateFailed
If updating the attributes for an endpoint fails this is emitted. attributesUpdated
When an endpoint's attributes are updated this is emitted. topicCreated
Emitted when a topic has been created successfully. createTopicFailed
Emitted when an attempt to create a topic has failed. topicDeleted
Emitted when a topic has been deleted successfully. deleteTopicFailed
Emitted when an attempt to delete a topic has failed. subscribed
Emitted when an endpoint has been subscribed to a topic successfully. subscribeFailed
Emitted when an attempt to subscribe an endpoint to a topic has failed. unsubscribed
Emitted when an endpoint has been unsubscribed from a topic successfully. unsubscribeFailed
Emitted when an attempt to unsubscribe an endpoint from a topic has failed. publishedMessage
Emitted when a message has been published to a topic successfully. publishFailed
Emitted when an attempt to publish a message to a topic has failed. APISNS(opts)Expects an object with the following params:
SUPPORTED_PLATFORMSAn object containing supported platforms. Available options are:
getPlatformApplicationArn()Returns the platformApplicationArn provided to the constructor. getRegion()Returns the region being used. getApiVersion()Returns apiVersion being used. getApplications(callback)Get all Platform Applications. This will not allow you to interface with other PlatformApplications but may be useful to just get a list of you applications. Callback format callback(err, users) getUser(endpointArn, callback)Get a user via endpointArn. The callback(err, user) receives an Object containg Attributes for the user and the EndpointArn. getUsers(callback)Get all users, this could take a while due to a potentially high number of requests required to get each page of users. The callback(err, users) receives an Array containing users. getTopics(callback)Get all topics by paging through them. The callback(err, topics) receives an Array containing topic objects. getSubscriptions(topicArn, callback)Get all subscriptions for the topic with the given topicArn by paging through them. The callback(err, subscriptions) receives an Array containing subscription objects. addUser(deviceToken, [data], callback)Add a device/user to SNS with optional extra data. Callback has format fn(err, endpointArn). createTopic(name, callback)Create a new topic with the given name. The callback has the format fn(err, topicArn). setAttributes(endpointArn, attributes, callback)Update an existing endpoint's attributes. Attributes is an object with the following optional properties:
Callback has format fn(err, attributes). deleteUser(endpointArn, callback)Delete a user from SNS. Callback has format callback(err) deleteTopic(topicArn, callback)Delete the topic with the given topicArn. The callback has the format fn(err). sendMessage(endpointArn, message, callback)Send a message to a user. The message parameter can be a String, or an Object with the formats below. The callback format is callback(err, messageId). subscribe(endpointArn, topicArn, callback)Subscribe an endpoint to a topic. The callback has the format fn(err, subscriptionArn). unsubscribe(subscriptionArn, callback)Unsubscribe an endpoint from a topic via the given subscriptionArn. The callback has the format fn(err). publishToTopic(topicArn, message, callback)Publish a message a topic. The callback has the format fn(err, messageId).
Please note that the message must be in the final Amazon SNS format as
specified here, i.e. it
must contain a key called {
"default": "Default message which must be present when publishing a message to a topic. Will only be used if a message is not present for one of the notification platforms.",
"APNS": "{\"aps\":{\"alert\": \"Check out these awesome deals!\",\"url\":\"www.amazon.com\"} }",
"GCM":"{\"data\":{\"message\":\"Check out these awesome deals!\",\"url\":\"www.amazon.com\"}}"
} You can read about Amazon SNS message formats here. iOS: {
aps: {
alert: message
}
} Read more about APNS payload here. Android & Kindle Fire: {
data: {
message: message
}
} Read more about GCM here and ADM here. broadcastMessage(message, callback)Send message to all users. May take some time with large sets of users as it has to page through users. Callback format is callback(err). If a single/mulitple messages fail to send the error will not be propogated/returned to the callback. To catch these errors use the sendFailed event. ContributorsContrinbutions are very much welcome, just submit a PR with updated tests where applicable. Current tests run against the actual SNS service, which may not be ideal so feel free to mock that out if you like too ;) Thanks to these awesome folks for contributions: |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论