Giter Site home page Giter Site logo

awspushbundle's Introduction

Aws Push Bundle

A convenient bundle for registering devices and then pushing to them using amazons SNS service.

Latest Stable Version License Build Status

Install

Composer

php composer.phar require mcfedr/awspushbundle

AppKernel

Include the bundle in your AppKernel

public function registerBundles()
{
    $bundles = array(
        ...
        new Mcfedr\AwsPushBundle\McfedrAwsPushBundle()

Config

Put something like this in your config. The arns in the platforms section should be the preconfigured app arns in SNS.

mcfedr_aws_push:
    platforms:
        ios: 'arn:aws:sns:....'
        android: 'arn:aws:sns:....'
    topic_arn: 'arn:aws:sns:...'
    pushPlatforms: [apns, fcm]
    aws:
        credentials: 
            key: 'my key'
            secret: 'my secret'
        region: 'my region'

You can skip credentials if you have want the Aws SDK to get credentials indirectly, either from environment or ec2 role.

If you don't set the pushPlatforms setting then android messages will sent in a format compatible with GCM, i.e. the notification field will not be set. This is for backwards compatibility of this bundle.

Usage

Basically have a look at how the ApiController does its stuff

  1. Register the device token

     $arn = $this->get('mcfedr_aws_push.devices')->registerDevice($token, $platform)
    
  2. Send message to one device

     $this->get('mcfedr_aws_push.messages')->send($message, $arn)
    
  3. Send message to all devices

     $this->get('mcfedr_aws_push.messages')->broadcast($message)
    

Alternative usage, using topics to send messages to lots of devices

  1. Register the device token

     $arn = $this->get('mcfedr_aws_push.devices')->registerDevice($token, $platform)
    
  2. Register the device on the topic

     $this->get('mcfedr_aws_push.topics')->registerDeviceOnTopic($arn, $topicArn)
    
  3. Send messages

     $this->get('mcfedr_aws_push.topics')->broadcast($message, $topicArn)
    

If you later add a topic_name to the configuration you can run the mcfedr:aws:subscribe command to add your existing devices to the topic.

Text in notifications

For GCM and ADM there is no 'standard' key for text data as there is for Apple pushes, so this bundle send text in a key called message.

If localized text is sent the keys are

  • message-loc-key
  • message-loc-args

'Complicated' data on ADM

ADM only allows strings as values in the push data. This bundle lets you send 'complicated' values and will automatically json encode these values for ADM. When it does this the key has _json added so that its easy to handle this on the app side.

Example

Sending:

$message = new Message();
$message->setCustom(['simple' => 'Hello', 'complicated' => ['inner' => 'value']]);

ADM received data:

{"data": {"simple": "Hello", "complicated_json": "{\"inner\":\"value\"}"}}

To handle this data you should detect keys that end with _json and decode the values

The applies to message-loc-args as well, they will always come as message-loc-args_json via ADM

Commands

There are some commands to help manage the devices

  1. mcfedr:aws:enable

    This will reenable all the devices

  2. mcfedr:aws:remove

    This will remove any disabled devices. Its a good idea to do something like this regularly to remove old devices

  3. mcfedr:aws:subscribe

    This will subscribe all devices to a topic, useful when introducing a topic

Api Controller

Optional.

There is a controller included which makes basic usage of the bundle very easy. You may or may not want to use it, you might find it most useful as an example.

There are some extra dependencies you must add when using the controller

  • sensio/framework-extra-bundle
  • symfony/validator
  • symfony/serializer
  • symfony/property-info
  • symfony/security-bundle
  • symfony/expression-language

They also need enabling in the framework config

framework:
    validation: { enable_annotations: true }
    serializer:
        enabled: true
    property_info:
        enabled: true

Add the routes in your routing.yaml:

mcfedr_aws_push:
    resource: "@McfedrAwsPushBundle/Controller/"
    type:     annotation
    prefix:   /

Usage

The controller provides two urls, both expect a JSON POST body

  1. The first is a way to register a device

     POST /devices
     {
         "device": {
             "deviceId": "a push token",
             "platform": "the platform name in your config file"
         }
     }
    
  2. The second is a way to send a broadcast message. If you are using a topic for all devices then don't send the platform parameter.

     POST /broadcast
     {
         "broadcast": {
             "platform": "ios"
             "message": {
                 "text": "The plain text message to send",
                 "badge": 1
             }
         }
     }
    

More info

awspushbundle's People

Contributors

ahonymous avatar avtehnik avatar ayoze avatar deguif avatar dylan-salmon avatar etheriq avatar javer avatar mcfedr avatar nonanerz avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

awspushbundle's Issues

deprecated autowiring

Hi, Im using symfony 3.4.22 and apear this deprecated

Autowiring services based on the types they implement is deprecated since Symfony 3.3 and won't be supported in version 4.0. You should rename (or alias) the "mcfedr_aws_push.sns_client" service to "Aws\Sns\SnsClient" instead.

my composer has this detail information

        "name": "mcfedr/awspushbundle",
        "version": "6.11.0",
        "source": {
            "type": "git",
            "url": "https://github.com/mcfedr/awspushbundle.git",
            "reference": "8a90e38b8f463e86618be816553f35e9262ad0dc"
        },

could I can remove this deprecated?

APNS aps data

I can't find any way to set aps data except badge, sound and content-avaliable.

How can I set category for example?

I found on your documentation the customAPNSdata but it seems it encapsulates it outside the aps key.

Could you provide a method to add an array of data the user wants inside aps key?

Thank you!

Microsoft Platforms(WNS,MPNS)

Hi,

First of all thank you for your great work. I have a question. Can you add the Windows Phone push platforms or can you help me how can I add them to your bundle.

Thanks.

Update to Symfony 3

Hi,

I have downloaded and tested your code in my Symfony 3 project and it works fine.

I only could test send to one device, but I think that the rest of methods work too.

ClassNotFoundException in AppKernel.php

Hi,

Installed mcfedr/awspushbundle V5.1.2 (version), but I got error while include awspushbundle in appkernel. Plz refer below screenshots:

  1. Installed path:
    screen1
  2. Include in appkernel:
    screen2

3.Error:
screen3

Thanks

add apns-collapse-id

Hello

collapse_key is compatible only for android devices, for apns we must use apns-collapse-id.
it exist this attribute AWS.SNS.MOBILE.APNS.COLLAPSE_ID now supported by SNS
https://docs.aws.amazon.com/sns/latest/dg/sns-message-attributes.html#sns-attrib-mobile-reserved

in the Mcfedr\AwsPushBundle\Service\Messages we could use the collapseKey to add this message attribute ?

    public function send($message, string $endpointArn): void
    {
        if ($this->debug) {
            $this->logger && $this->logger->notice(
                "Message would have been sent to $endpointArn",
                [
                    'Message' => $message,
                ]
            );

            return;
        }

        if (!($message instanceof Message)) {
            $message = new Message($message);
        }

        if ($this->platforms !== null && !$message->isPlatformsCustomized()) {
            $message->setPlatforms($this->platforms);
        }

        $messageAttributes = [
            'AWS.SNS.MOBILE.APNS.PUSH_TYPE' => ['DataType' => 'String', 'StringValue' => $message->getPushType()],
        ];

        if ($message->getCollapseKey() != Message::NO_COLLAPSE) {
            $messageAttributes['AWS.SNS.MOBILE.APNS.COLLAPSE_ID'] = ['DataType' => 'String', 'StringValue' => $message->getCollapseKey()];
        }

        $this->sns->publish(
            [
                'TargetArn' => $endpointArn,
                'Message' => $this->encodeMessage($message),
                'MessageStructure' => 'json',
                'MessageAttributes' => $messageAttributes,
            ]
        );
    }

Error while installing

I have error when I try installing plugin:

Notice: Undefined index: platforms In McfedrAwsPushExtension.php line 28:

Device disabled error

If a device is disabled in SNS, an error is thrown preventing the rest of my code from functioning โ€“ can't seem to find a method to check if a device is enabled prior to delivery of method therefore I can't guarantee execution of the rest of my code, what's the normal approach to handle this?

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.