Setting up push notification services
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:
-
Create a Firebase project.
-
Register your app with the Firebase project you created.
-
In the Firebase console, choose Download google-services.json. Copy the downloaded
google-services.json
file to theapp
directory of your Android project.
To access the service account key (json file)
:
-
Open the Firebase console.
-
Select the gear icon located in the top left hand corner of your screen, then select Project settings.
-
Select the Service accounts tab.
-
Select Firebase Admin SDK in the sidebar
-
Click Generate new private key
-
Keep the downloaded service account key (json file) safe. You will need to provide the path to the CLI.
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
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:
<receiver android:name="com.amazonaws.mobileconnectors.pinpoint.targeting.notification.PinpointNotificationReceiver" android:exported="false" > <intent-filter> <action android:name="com.amazonaws.intent.fcm.NOTIFICATION_OPEN" /> </intent-filter></receiver>
Open a deep link
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:
<!-- This activity allows your application to receive a deep linkthat navigates directly to the "Deeplink Page" --><activity android:name=".DeepLinkActivity" android:label="A deeplink!" > <intent-filter android:label="inAppReceiver"> <action android:name="android.intent.action.VIEW" /> <category android:name="android.intent.category.DEFAULT" /> <category android:name="android.intent.category.BROWSABLE" /> <!-- Accepts URIs of type "pinpoint://deeplink" --> <data android:scheme="pinpoint" android:host="deeplink" /> </intent-filter></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.
public class DeepLinkActivity extends Activity {
public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);
if (getIntent().getAction() == Intent.ACTION_VIEW) { Uri data = getIntent().getData();
if (data != null) {
// show an alert with the "custom" param new AlertDialog.Builder(this) .setTitle("An example of a Deeplink") .setMessage("Found custom param: " +data.getQueryParameter("custom")) .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); } }) .setIcon(android.R.drawable.ic_dialog_alert) .show(); } } }}
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:
-
Amazon Developer account.
-
Client ID and client secret from Amazon Device Messaging.
-
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:
pinpointManager.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:
NotificationDetails details = NotificationDetailsBuilder.builder() .intent(intent); .intentAction(NotificationClient.ADM_INTENT_ACTION) .build();
pinpointManager.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:
pinpointManager.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.
amplify console analytics
- On the left pane, select
Settings
andPush notifications
. - Click
Edit
and selectShow more push notification services
and clickAmazon Device Messaging
. - Enter the
Client ID
and theClient Secret
and clickSave
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:
<uses-permission android:name="android.permission.INTERNET" /><uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /><uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<!-- Baidu permissions --><uses-permission android:name="android.permission.WAKE_LOCK"/><uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" /><uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /><uses-permission android:name="android.permission.WRITE_SETTINGS" /><uses-permission android:name="android.permission.VIBRATE" /><uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /><uses-permission android:name="android.permission.ACCESS_DOWNLOAD_MANAGER" /><uses-permission android:name="android.permission.DOWNLOAD_WITHOUT_NOTIFICATION" /><uses-permission android:name="android.permission.DISABLE_KEYGUARD" />
Under <application>
, specify the following receivers and intent filters:
<!-- Baidu settings --><receiver android:name="com.baidu.android.pushservice.PushServiceReceiver" android:process=":bdservice_v1"> <intent-filter> <action android:name="android.intent.action.BOOT_COMPLETED" /> <action android:name="android.net.conn.CONNECTIVITY_CHANGE" /> <action android:name="com.baidu.android.pushservice.action.notification.SHOW" /> <action android:name="com.baidu.android.pushservice.action.media.CLICK" />
<action android:name="android.intent.action.MEDIA_MOUNTED" /> <action android:name="android.intent.action.USER_PRESENT" /> <action android:name="android.intent.action.ACTION_POWER_CONNECTED" /> <action android:name="android.intent.action.ACTION_POWER_DISCONNECTED" /> </intent-filter></receiver>
<receiver android:name="com.baidu.android.pushservice.RegistrationReceiver" android:process=":bdservice_v1"> <intent-filter> <action android:name="com.baidu.android.pushservice.action.METHOD" /> <action android:name="com.baidu.android.pushservice.action.BIND_SYNC" /> </intent-filter> <intent-filter> <action android:name="android.intent.action.PACKAGE_REMOVED" />
<data android:scheme="package" /> </intent-filter></receiver>
<service android:name="com.baidu.android.pushservice.PushService" android:exported="true" android:process=":bdservice_v1"> <intent-filter> <action android:name="com.baidu.android.pushservice.action.PUSH_SERVICE" /> </intent-filter></service><service android:name="com.baidu.android.pushservice.CommandService" android:exported="true" />
<!-- 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) --><receiver android:name="com.amazonaws.mobileconnectors.pinpoint.targeting.notification.PinpointNotificationReceiver"> <intent-filter> <action android:name="com.amazonaws.intent.baidu.NOTIFICATION_OPEN" /> </intent-filter></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.
<uses-permission android:name="baidu.push.permission.WRITE_PUSHINFOPROVIDER.YourPackageName" />
<permission android:name="baidu.push.permission.WRITE_PUSHINFOPROVIDER.YourPackageName" android:protectionLevel="normal" />
<provider android:name="com.baidu.android.pushservice.PushInfoProvider" android:authorities="YourPackageName.bdpush" android:exported="true" android:protectionLevel="signature" 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:
pinpointManager.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:
NotificationDetails details = NotificationDetailsBuilder.builder() .message(message); .intentAction(NotificationClient.BAIDU_INTENT_ACTION) .build();
pinpointManager.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
.
<receiver android:name="com.baidu.push.example.MyPushMessageReceiver"> <intent-filter> <action android:name="com.baidu.android.pushservice.action.MESSAGE" /> <action android:name="com.baidu.android.pushservice.action.RECEIVE" /> <action android:name="com.baidu.android.pushservice.action.notification.CLICK" /> </intent-filter></receiver>
To start the Baidu listener service, in your Android app’s main activity, add the following code to the onCreate method:
// ATTENTION:You need to modify the value of api_key to your own !!PushManager.startWork(getApplicationContext(), PushConstants.LOGIN_TYPE_API_KEY, api_key);
CustomPushNotificationBuilder cBuilder = new CustomPushNotificationBuilder( getResources().getIdentifier("notification_custom_builder", "layout", getPackageName()), getResources().getIdentifier("notification_icon", "id", getPackageName()), getResources().getIdentifier("notification_title", "id", getPackageName()), getResources().getIdentifier("notification_text", "id", getPackageName()));cBuilder.setNotificationFlags(Notification.FLAG_AUTO_CANCEL);cBuilder.setNotificationDefaults(Notification.DEFAULT_VIBRATE);
cBuilder.setStatusbarIcon(this.getApplicationInfo().icon);cBuilder.setLayoutDrawable(getResources().getIdentifier( "simple_notification_icon", "drawable", getPackageName()));cBuilder.setNotificationSound(Uri.withAppendedPath( Audio.Media.INTERNAL_CONTENT_URI, "6").toString());PushManager.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:
final PinpointConfiguration config = new PinpointConfiguration(this, IdentityManager.getDefaultIdentityManager() .getCredentialsProvider(), awsConfiguration) .withChannelType(ChannelType.BAIDU);Application.pinpointManager = new PinpointManager(config);
Or, you can define a configuration file to be consumed by AWSConfiguration
:
"PinpointAnalytics": { "Default": { "AppId": "[YourPinpointAppId]", "Region": "us-east-1", "ChannelType": "BAIDU" }}
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:
pinpointManager.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.
amplify console analytics
- On the left pane, select
Settings
andPush notifications
. - Click
Edit
and selectShow more push notification services
and clickBaidu Cloud Push
. - Enter the
Baidu API Key
and theBaidu Secret Key
and clickSave
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.