How to integrate push notifications for Amazon Appstore
Tags: android, amazon, amazonappstore, tutorial
Published: 2022-11-18 18:25:51


Recently I was at [Droidcon London](https://london.droidcon.com) catching up ✨ _in-person_ ✨ with Android developers and longtime friends. The event was well-run, full of engaging technical talks and great conversations.  


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8yb4spq5cqz4k20o96y7.jpeg)

One particular conversation I had was with an Android + iOS app developer who found a way to sustainably freelance by launching multiple revenue streams: Ads, In App Purchasing (IAP), and selling online programming courses _(...a dream of mine to do someday when I have the time..._ 😉⏳ _)._ 

He was building everything from games to utility apps and was confronted with a recurring issue: The app user base would steadily grow until hitting a plateau after a variable amount of time (months). Once new interest peaked, less new users were discovering and installing his apps. 

While I don't pretend to have a magic solution, I shared a suggestion based on a talk my colleague and I delivered hours earlier: 
> *Launch your app in multiple app stores.*

...and in my role as a Developer Advocate, I have to recommend these apps should definitely be the Amazon Appstore! 🥳📲

The [Amazon Appstore](https://developer.amazon.com/apps) offers an active global user base and one of the coolest features: existing apps can be installed directly on Windows 11.

**_What if your app could now reach millions of new desktop/laptop Windows 11 users around the world?_**

This developer then asked the standard software engineering questions:
> _"Yes but...
> Are all the features I already developed going to be supported on the new system? How much effort is involved to port the app?"_

The classic yet honest answer to this is:
> *"it depends!"* 🤷

So let's dive in.

### Porting Android apps to Amazon Appstore 

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/i8bkcwynhne6kcu708se.jpg)

[Amazon Appstore](https://www.amazon.com/gp/mas/get/amazonapp) runs on Fire OS devices (Fire Tablets, Fire TVs...) and can be installed on directly on Android devices. Fire OS is based on [AOSP (Android Open Source Project)](https://source.android.com/) and without Google Play Services running on it. So for developers with app features dependent on Google Play Services, some migration work is needed.

For example, if your app relies on Firebase Cloud Messaging (FCM) for push notifications, this should be updated. Specifically in Fire OS, push notifications function through [Amazon Device Messaging (ADM)](https://developer.amazon.com/docs/adm/overview.html). Integrating the ADM SDK will allow your app to run smoothly in Fire OS.

To support both FCM and ADM, the Amazon Appstore team also created Amazon Appstore Abstraction Library [A3L](https://developer.amazon.com/docs/a3l-messaging/understanding-a3l-messaging.html) - an SDK to reduce porting time and code maintenance needs for notifications and messaging.

### Integrating your app with A3L

**Step 0:** Download the A3L SDK

Download the A3L SDK from our [developer documentation](https://developer.amazon.com/docs/a3l-messaging/understanding-a3l-messaging.html#download)

Afterwards, make sure to add the downloaded **.aar** to the **app/libs** directory of your Android project.

**Step 1:** Add the A3L dependency to your project

After this, you will have to add the dependency on this file of the project. Open the `build.gradle` file and insert the following line:

```java
dependencies {
     ...
     implementation files("libs/A3L-Messaging-1.0.0.aar")
 }
```

**Step 2:** Obtain the credentials

Similar to what you already have done with Firebase through the `google-services.json` file, you have to obtain ADM credentials in order to uniquely identify your app the web service.

Once you have credentials, follow the instructions in our [ADM docs](https://developer.amazon.com/docs/adm/obtain-credentials.html).


**Step 3:** Define the messaging service

Now you will should create the Service that listens for messages and tokens from ADM.

To do this, you will have to extend the class `A3LMessagingService` and override these 2 methods:

```kotlin
class MyA3LMessagingService : A3LMessagingService() {

    override fun onMessageReceived(context: Context, remoteMessage: RemoteMessage) {
        Log.d(TAG, "From: ${remoteMessage.from}")
    }

    override fun onNewToken(context: Context, token: String) {
        Log.d(TAG, "Refreshed token: $token")
    }
```
To Enable this service you will need to declare in the `AndroidManifest.xml`:

```xml
<application>
    <receiver android:name=".MyA3LMessagingService"
        android:exported="false"
        android:enabled="true">
        <intent-filter>
            <action android:name="com.amazon.A3L.messaging.intent.REGISTRATION"/>
            <action android:name="com.amazon.A3L.messaging.intent.MESSAGE"/>
        </intent-filter>
    </receiver>

    <meta-data android:name="com.a3l.clsName" 
    android:value="com.example.mya3lapp.MyA3LMessagingService" />
    
 </application>
```

**Step 4:** Initialize the Service

Now you define the callback `MainActivity` to handle the registration phase and initialize the service:

```kotlin
class MainActivity : AppCompatActivity() {

    private val TAG = "MainActivity"

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        setContentView(R.layout.activity_main)

        val onInitCallback: OnInitCallback = object : OnInitCallback() {
            override fun onReady(initCallbackResponse: InitCallbackResponse) {
                if (initCallbackResponse.isSuccessFul) {
                    Log.d(TAG, "Device Id: " + initCallbackResponse.token)
                } else {
                    Log.d(
                        TAG, "Registration failed with Error: " +
                                initCallbackResponse.errorMessage
                    )
                }
            }
        }
        A3LMessaging.init(applicationContext, onInitCallback)

        if (savedInstanceState == null) {
            supportFragmentManager.beginTransaction()
                .replace(R.id.container, EggTimerFragment.newInstance())
                .commitNow()
        }
    }
}
```

and with that update, you are all set! Let's see how to test this implementation.

### Testing
You have three ways to test this integration:
- The Web Console
- Server-side scripts
- Solution-specific APIs

For our use-case, the easiest way is with the [Amazon Appstore ADM Web Console](https://developer.amazon.com/adm/console). I suggest running the app on a Fire Tablet in order get the Device registration ID.
This will be available in the `onReady` of the `onInitCallback`.
Copy the ID and head to the Appstore ADM Web Console:


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/40botem83irw35tkz007.png)

Select your target app to test and enter the Client ID and secret for your app. (You should have these saved from when you obtained your ADM credentials)

In the Device registration ID field, enter the value previously saved.

1. Choose **Notification message**
2. Populate the fields **Notification Title** and **Body content**
3. Click **Send test message**

You should then receive an app notifications appearing on your Fire Device.

For the other ways of testing, you can check the [official documentation](https://www.developer.amazon.com/docs/a3l-messaging/test.html).

### Wrap up

I hope the Droidcon developer I spoke with and you are able to quickly launch existing apps for Amazon Appstore.

Using A3L will allow your app to support both FCM and ADM. If you want to try an A3L sample project with support for both Firebase Cloud Messaging and Amazon Device Messaging, check out my recent [repo on GitHub](https://github.com/giolaq/android-amazon-notifications-porting) and our related [documentation](https://developer.amazon.com/docs/a3l-messaging/understanding-a3l-messaging.html).

Make sure to review the full [pre-submission checklist](https://developer.amazon.com/apps-and-games/app-submission) when you’re ready to submit your app in the Amazon Appstore.

**Stay connected** 

For the latest Amazon Appstore developer news, product releases, tutorials, and more:

📣 Follow [@AmazonAppDev](https://twitter.com/AmazonAppDev) and [our team](https://twitter.com/i/lists/1580293569897984000/members) on Twitter

📺 Subscribe to our [Youtube channel](https://www.youtube.com/amazonappstoredevelopers)

* 📧 Sign up for the [Developer Newsletter](https://m.amazonappservices.com/devto-newsletter-subscribe)