Setting up push notification services

You are currently viewing the AWS SDK for Mobile documentation which is a collection of low-level libraries. Use the Amplify libraries for all new app development. Learn more

Setup FCM/GCM in Firebase

You can enable your Android app to receive push notifications that you send through by using Amazon Pinpoint. With Amazon Pinpoint, you can send push notifications through Firebase Cloud Messaging (FCM) or its predecessor, Google Cloud Messaging (GCM). Before you can send push notifications through FCM or GCM, the following setup steps need to be performed:

  1. Create a Firebase project.

  2. Register your app with the Firebase project you created.

  3. In the Firebase console, choose Download google-services.json. Copy the downloaded google-services.json file to the app directory of your Android project.

To access the Server Key:

  1. Open the Firebase console.

  2. Choose your project.

  3. Choose the settings icon next to Project Overview in the top left and then Project Settings.

  4. Open the Cloud Messaging tab.

  5. Copy the token next to Server key.

Handling FCM/GCM Push Notifications

Amazon Pinpoint campaigns can take one of three actions when a user taps a notification: Open your app, Go to a URL, or Open a deep link.

Open your app

By specifying this action, you can open the app when the user taps on the notification.

Adding a Receiver

This receiver is no longer necessary and should be removed as of com.amazonaws:aws-android-sdk-pinpoint:2.48.0

The SDK provides PinpointNotificationReceiver which handles the notification to open your app. In order to use this action, you must register this receiver in your AndroidManifest.xml file. For example:

1<receiver
2 android:name="com.amazonaws.mobileconnectors.pinpoint.targeting.notification.PinpointNotificationReceiver" android:exported="false" >
3 <intent-filter>
4 <action android:name="com.amazonaws.intent.fcm.NOTIFICATION_OPEN" />
5 </intent-filter>
6</receiver>

This action opens the app to a specified activity.

To specify a destination activity for deep links, the app must have set up deep linking. This setup requires an intent filter that registers a URL scheme the deep links will use. After the app creates an intent filter, the data provided by the intent determines the activity to render.

Creating an Intent Filter

Begin to set up deep linking by creating an intent filter in your AndroidManifest.xml file. For example:

1<!-- This activity allows your application to receive a deep link
2that navigates directly to the "Deeplink Page" -->
3<activity
4 android:name=".DeepLinkActivity"
5 android:label="A deeplink!" >
6 <intent-filter android:label="inAppReceiver">
7 <action android:name="android.intent.action.VIEW" />
8 <category android:name="android.intent.category.DEFAULT" />
9 <category android:name="android.intent.category.BROWSABLE" />
10 <!-- Accepts URIs of type "pinpoint://deeplink" -->
11 <data android:scheme="pinpoint"
12 android:host="deeplink" />
13 </intent-filter>
14</activity>

The data element in the previous example registers a URL scheme, pinpoint://, as well as the host, deeplink. As a result, when given a URL in the form of pinpoint://deeplink, the manifest is prepared to execute the action.

Handling the Intent

Next, set up an intent handler to present the screen associated with the registered URL scheme and host. Intent data is retrieved in the onCreate() method, which then can use Uri data to create an activity. The following example shows an alert and tracks an event.

1public class DeepLinkActivity extends Activity {
2
3 public void onCreate(Bundle savedInstanceState) {
4 super.onCreate(savedInstanceState);
5
6 if (getIntent().getAction() == Intent.ACTION_VIEW) {
7 Uri data = getIntent().getData();
8
9 if (data != null) {
10
11 // show an alert with the "custom" param
12 new AlertDialog.Builder(this)
13 .setTitle("An example of a Deeplink")
14 .setMessage("Found custom param: " +data.getQueryParameter("custom"))
15 .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
16 public void onClick(DialogInterface dialog, int which) {
17 dialog.dismiss();
18 }
19 })
20 .setIcon(android.R.drawable.ic_dialog_alert)
21 .show();
22 }
23 }
24 }
25}

Handling Amazon Device Messaging Push Notifications

Amazon Device Messaging (ADM) is a service used to send push notifications to apps running on Amazon devices, such as Kindle Fire tablets. By integrating ADM with your app, you can use Amazon Pinpoint to send notifications to your app through the ADM mobile push channel.

Prerequisites

To send push notifications to your app using Amazon Pinpoint and ADM, you need the following:

  1. Amazon Developer account.

  2. Client ID and client secret from Amazon Device Messaging.

  3. ADM registration ID (provided by the end device that contains the ADM platform).

Integrating ADM with Your App

If you are already familiar with ADM and have ADM credentials, you can follow the steps for Integrating Your App with Amazon Device Messaging in the Amazon Developer documentation. Otherwise, for an introduction to ADM, see Understanding Amazon Device Messaging.

To integrate with Amazon Pinpoint, your subclass implementation of com.amazon.device.messaging.ADMMessageHandlerBase should include the following methods and perform the corresponding calls:

onRegistered

Called when the device is registered with the ADM service. Provides the ADM registration ID that is needed to register the device with Amazon Pinpoint. Include the following call as part of this method:

1pinpointManager.getNotificationClient().registerDeviceToken(registrationId)

onUnregistered

Called when the device is no longer registered with the ADM service.

onMessage

Called when the device receives a message notification from ADM. Include the following as part of this method:

1NotificationDetails details = NotificationDetailsBuilder.builder()
2 .intent(intent);
3 .intentAction(NotificationClient.ADM_INTENT_ACTION)
4 .build();
5
6pinpointManager.getNotificationClient().handleCampaignPush(details)

Testing ADM Push Notifications

To test, you need an Amazon Pinpoint project, an ADM client ID, and an ADM client secret.

Before you begin, augment your app to display the device token after registration. The device token can be retrieved by calling:

1pinpointManager.getNotificationClient().getDeviceToken()

Complete the following steps using the Amplify CLI and Amazon Pinpoint console to test ADM push notifications

Register ADM as a channel with your Amazon Pinpoint project. Provide the ADM client ID and the ADM client secret.

Run the following command to navigate to the Amazon Pinpoint console.

1amplify console analytics
  • On the left pane, select Settings and Push notifications.
  • Click Edit and select Show more push notification services and click Amazon Device Messaging.
  • Enter the Client ID and the Client Secret and click Save at the right bottom of the page.

Now ADM is registered as a push notification service.

Install your app on a device that has ADM enabled, and capture the generated device token.

Send a direct message to the device specifying the device token as the address.

  • On the Amazon Pinpoint console, go to Test messaging.
  • Select Push notifications as the channel.
  • Enter the endpoint ID or the device token in the Destinations.
  • Select ADM as the push notifications service.
  • Create a message and click Send message at the bottom right corner of the page to send a direct message.

Handling Baidu Push Notifications

Baidu Cloud Push is the push notification service provided by Baidu, a Chinese cloud service. By integrating Baidu Cloud Push with your mobile app, you can use Amazon Pinpoint to send notifications to your app through the Baidu mobile push channel.

Prerequisites

To send push notifications to mobile devices using Amazon Pinpoint and Baidu, you need the following:

  • Baidu account.

  • Registration as a Baidu developer.

  • Baidu Cloud Push project.

  • API key and secret key from a Baidu Cloud Push project.

  • Baidu user ID and channel ID.

The following procedure is based on version 5.7.1.65 of the Baidu push service jar.

To integrate Baidu with your app

Download the latest Baidu Cloud Push SDK Android client from http://push.baidu.com/.

Extract the zip file and import the pushservice-x.x.xx.jar file from the Baidu-Push-SDK-Android libs folder into your Android app’s lib folder.

The Baidu-Push-SDK-Android libs folder should also include the following folders:

  • arm64-v8a

  • armeabi

  • armeabi-v7a

  • mips

  • mips64

  • x86

  • x86_64

Add each complete folder to your Android app’s src/main/jniLibs folder.

In the Android app’s AndroidManifest.xml file, declare the following permissions:

1<uses-permission android:name="android.permission.INTERNET" />
2<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
3<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
4
5<!-- Baidu permissions -->
6<uses-permission android:name="android.permission.WAKE_LOCK"/>
7<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
8
9<uses-permission android:name="android.permission.READ_PHONE_STATE" />
10<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
11<uses-permission android:name="android.permission.WRITE_SETTINGS" />
12<uses-permission android:name="android.permission.VIBRATE" />
13<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
14<uses-permission android:name="android.permission.ACCESS_DOWNLOAD_MANAGER" />
15<uses-permission android:name="android.permission.DOWNLOAD_WITHOUT_NOTIFICATION" />
16<uses-permission android:name="android.permission.DISABLE_KEYGUARD" />

Under <application>, specify the following receivers and intent filters:

1<!-- Baidu settings -->
2<receiver android:name="com.baidu.android.pushservice.PushServiceReceiver"
3 android:process=":bdservice_v1">
4 <intent-filter>
5 <action android:name="android.intent.action.BOOT_COMPLETED" />
6 <action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
7 <action android:name="com.baidu.android.pushservice.action.notification.SHOW" />
8 <action android:name="com.baidu.android.pushservice.action.media.CLICK" />
9
10 <action android:name="android.intent.action.MEDIA_MOUNTED" />
11 <action android:name="android.intent.action.USER_PRESENT" />
12 <action android:name="android.intent.action.ACTION_POWER_CONNECTED" />
13 <action android:name="android.intent.action.ACTION_POWER_DISCONNECTED" />
14 </intent-filter>
15</receiver>
16
17<receiver android:name="com.baidu.android.pushservice.RegistrationReceiver"
18 android:process=":bdservice_v1">
19 <intent-filter>
20 <action android:name="com.baidu.android.pushservice.action.METHOD" />
21 <action android:name="com.baidu.android.pushservice.action.BIND_SYNC" />
22 </intent-filter>
23 <intent-filter>
24 <action android:name="android.intent.action.PACKAGE_REMOVED" />
25
26 <data android:scheme="package" />
27 </intent-filter>
28</receiver>
29
30<service android:name="com.baidu.android.pushservice.PushService"
31 android:exported="true"
32 android:process=":bdservice_v1">
33 <intent-filter>
34 <action android:name="com.baidu.android.pushservice.action.PUSH_SERVICE" />
35 </intent-filter>
36</service>
37<service android:name="com.baidu.android.pushservice.CommandService"
38 android:exported="true" />
39
40<!-- Amazon Pinpoint Notification Receiver (This receiver is no longer necessary and should be removed as of com.amazonaws:aws-android-sdk-pinpoint:2.48.0) -->
41<receiver android:name="com.amazonaws.mobileconnectors.pinpoint.targeting.notification.PinpointNotificationReceiver">
42 <intent-filter>
43 <action android:name="com.amazonaws.intent.baidu.NOTIFICATION_OPEN" />
44 </intent-filter>
45</receiver>

Update the AndroidManifest.xml file with the following permissions, which are specific to your application. Remember to replace <YourPackageName> with the name of your package.

1<uses-permission android:name="baidu.push.permission.WRITE_PUSHINFOPROVIDER.YourPackageName" />
2
3<permission
4 android:name="baidu.push.permission.WRITE_PUSHINFOPROVIDER.YourPackageName"
5 android:protectionLevel="normal" />
6
7<provider
8 android:name="com.baidu.android.pushservice.PushInfoProvider"
9 android:authorities="YourPackageName.bdpush"
10 android:exported="true"
11 android:protectionLevel="signature"
12 android:writePermission="baidu.push.permission.WRITE_PUSHINFOPROVIDER.YourPackageName" />

Inside your Android application, create a MessageReceiver class that subclasses com.baidu.android.pushservice.PushMessageReceiver. The subclass should implement the following methods and perform the corresponding calls:

onBind

Called when the device is registered with Baidu Cloud Push. Provides the Baidu user ID and channel ID that are needed to register the device with Amazon Pinpoint. Include the following call as part of this method:

1pinpointManager.getNotificationClient().registerDeviceToken(userId, channelId);

onUnbind

Called when the device is no longer registered with Baidu Cloud Push.

onMessage

Called when the device receives a raw message from Baidu Cloud Push. Amazon Pinpoint transmits campaign push notifications with the Baidu Cloud Push raw message format. Include the following call as part of this method:

1NotificationDetails details = NotificationDetailsBuilder.builder()
2 .message(message);
3 .intentAction(NotificationClient.BAIDU_INTENT_ACTION)
4 .build();
5
6pinpointManager.getNotificationClient().handleCampaignPush(details)

Only the message parameter contains data. The customContentString is not used with raw messages.

After creating the subclass, modify the AndroidManifest.xml file to register it as a receiver. In the following example, the PushMessageReceiver subclass is named com.baidu.push.example.MyPushMessageReceiver.

1<receiver android:name="com.baidu.push.example.MyPushMessageReceiver">
2 <intent-filter>
3 <action android:name="com.baidu.android.pushservice.action.MESSAGE" />
4 <action android:name="com.baidu.android.pushservice.action.RECEIVE" />
5 <action android:name="com.baidu.android.pushservice.action.notification.CLICK" />
6 </intent-filter>
7</receiver>

To start the Baidu listener service, in your Android app’s main activity, add the following code to the onCreate method:

1// ATTENTION:You need to modify the value of api_key to your own !!
2PushManager.startWork(getApplicationContext(), PushConstants.LOGIN_TYPE_API_KEY, api_key);
3
4CustomPushNotificationBuilder cBuilder = new CustomPushNotificationBuilder(
5 getResources().getIdentifier("notification_custom_builder", "layout", getPackageName()),
6 getResources().getIdentifier("notification_icon", "id", getPackageName()),
7 getResources().getIdentifier("notification_title", "id", getPackageName()),
8 getResources().getIdentifier("notification_text", "id", getPackageName()));
9cBuilder.setNotificationFlags(Notification.FLAG_AUTO_CANCEL);
10cBuilder.setNotificationDefaults(Notification.DEFAULT_VIBRATE);
11
12cBuilder.setStatusbarIcon(this.getApplicationInfo().icon);
13cBuilder.setLayoutDrawable(getResources().getIdentifier(
14 "simple_notification_icon", "drawable", getPackageName()));
15cBuilder.setNotificationSound(Uri.withAppendedPath(
16 Audio.Media.INTERNAL_CONTENT_URI, "6").toString());
17PushManager.setNotificationBuilder(this, 1, cBuilder);

Remember to properly initialize your PinpointManager reference. Use a PinpointConfiguration with a ChannelType value of ChannelType.BAIDU. You can do this programmatically, as in the following example:

1final PinpointConfiguration config =
2 new PinpointConfiguration(this,
3 IdentityManager.getDefaultIdentityManager()
4 .getCredentialsProvider(),
5 awsConfiguration)
6 .withChannelType(ChannelType.BAIDU);
7Application.pinpointManager = new PinpointManager(config);

Or, you can define a configuration file to be consumed by AWSConfiguration:

1"PinpointAnalytics": {
2 "Default": {
3 "AppId": "[YourPinpointAppId]",
4 "Region": "us-east-1",
5 "ChannelType": "BAIDU"
6 }
7}

Testing Baidu Push Notifications

To test, you need an Amazon Pinpoint project, a Baidu API key, and a Baidu Secret key.

Before you begin, augment your app to display the device token after registration. The device token can be retrieved by calling:

1pinpointManager.getNotificationClient().getDeviceToken()

Complete the following steps using the Amplify CLI and Amazon Pinpoint console to test Baidu push notifications.

Run the following command to navigate to the Amazon Pinpoint console.

1amplify console analytics
  • On the left pane, select Settings and Push notifications.
  • Click Edit and select Show more push notification services and click Baidu Cloud Push.
  • Enter the Baidu API Key and the Baidu Secret Key and click Save at the right bottom of the page.

Now Baidu Cloud Push is registered as a push notification service.

Install your app on to a Baidu-enabled device and capture the generated device token.

Send a direct message to the device specifying the device token as the address.

  • On the Amazon Pinpoint console, go to Test messaging.
  • Select Push notifications as the channel.
  • Enter the endpoint ID or the device token in the Destinations.
  • Select Baidu as the push notifications service.
  • Create a message and click Send message at the bottom right corner of the page to send a direct message.