Git Product home page Git Product logo

awsswiftsnssdk's Introduction

AwsSns - Swift

AwsSns is a Swift library that enables you to use Amazon Web Service Simple Notification Service (AWS SNS) with Swift. More details on this are available from the AWS SNS documentation.

Swift 5.1 Platforms iOS | macOS | watchOS | tvOS | Linux SPM compatible

This package builds with Swift Package Manager. Ensure you have installed and activated the latest Swift 5.1 tool chain.

Quick Start

To use AwsSns, modify the Package.swift file and add following dependency:

.package(url: "https://github.com/nikola-mladenovic/AwsSwiftSNSsdk", .branch("master"))

Then import the AwsSns library into the swift source code:

import AwsSns

Usage

The current release provides Publish functionality. First initialize the AwsSns instance with your credentials and the SNS host:

let snsClient = AwsSns(host: "https://sns.us-west-2.amazonaws.com/", accessKeyId: "593ca2ad2782e4000a586d28", secretAccessKey: "ASDI/YZZfLXLna3xEn7JTIJhyH/YZZfLXLna3xEn7JTIJhyH")

Then use the publish method of the AwsSns instance to send messages to the SNS topic or the SNS target. Message can be represented either as String or as JSON (dictionary). Example with String:

snsClient.publish(message: "Your message", topicArn: "arn:aws:sns:us-west-2:487164526243:test", completion: { success, error in
    // Do some work
    ...
})

To create an endpoint for a device and mobile app on one of the supported push notification services, such as GCM and APNS, use createPlatformEndpoint method of the AwsSns instance. The endpointArn that is returned by completion closure when using createPlatformEndpoint can then be used by the publish method to send a message to a mobile app. Example:

let token = "93SDOIFUHWIUEHF839UR0Q93JRF0W93FJ04W385U2Q0"
let platformApplicationArn = "arn:aws:sns:us-west-2:3904190132:app/test"

snsClient.createPlatformEndpoint(token: token, platformApplicationArn: platformApplicationArn) { success, endpointArn, error in
    // Do some work
    ...
}

To list the platform application objects for the supported push notification services, such as APNS and GCM, use listPlatformApplications method of the AwsSns instance. Method can return up to 100 platform application objects per call in it's completion closure. If additional objects are available after the first page results, then a nextToken string will not be nil in response object. Example:

snsClient.listPlatformApplications { (success, response, error) in
    // Do some work
    ...
}

To list the endpoint application arns for devices in a supported push notification service, such as GCM and APNS, use listEndpointsBy method of the AwsSns instance. Method can return up to 100 platform application objects per call in it's completion closure. If additional objects are available after the first page results, then a nextToken string will not be nil in response object. Example:

let platformApplicationArn = "arn:aws:sns:us-west-2:3904190132:app/test"

snsClient.listEndpointsBy(platformApplicationArn: platformApplicationArn) { (success, response, error) in
    // Do some work
    ...
}

To delete the endpoint for a device and mobile app from Amazon SNS, use deleteEndpoint method of the AwsSns instance. When you delete an endpoint that is also subscribed to a topic, then you must also unsubscribe the endpoint from the topic. Example:

let endpointArn = "arn:aws:sns:us-west-2:4834231343:endpoint/Example/a34939514-6d01-4444-3333-ffba93942"

snsClient.deleteEndpoint(endpointArn: endpointArn) { success, error in
    // Endpoint deleted
    ...
}

To retrive the endpoint attributes for a device on one of the supported push notification services, such as GCM and APNS, use getEndpointAttributes method of the AwsSns instance. Example:

let endpointArn = "arn:aws:sns:us-west-2:4834231343:endpoint/Example/a34939514-6d01-4444-3333-ffba93942"

snsClient.getEndpointAttributes(endpointArn: endpointArn) { (success, attributes, error) in
    // Do some work
    ...
}

To set the attributes for an endpoint for a device on one of the supported push notification services, such as GCM and APNS, use setEndpointAttributes method of the AwsSns instance. Example:

let endpointArn = "arn:aws:sns:us-west-2:4834231343:endpoint/Example/a34939514-6d01-4444-3333-ffba93942"
let attributes = [ "Enabled" : "false",
                   "Token" : "93SDOIFUHWIUEHF839UR0Q93JRF0W93FJ04W385U2Q0" ]

snsClient.setEndpointAttributes(endpointArn: endpointArn, attributes: attributes) { (success, error) in
    // Do some work
    ...
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.