1. Include the SDK
You have to add this maven URL ‘http://maven.pushamplify.com:8081/artifactory/libs-release’. Into project level build.gradle file include this URL like below:
allprojects {
repositories {
jcenter()
maven {
// add this line for PushAmplify repo, the above lines may already be there
url 'http://maven.pushamplify.com:8081/artifactory/libs-release'
}
}
}
You should also add “com.pushamplify.pushamplifylib:pushamplifylib:1.0.0” as a dependency in app/build.gradle. Add the dependency like below:
compile 'com.pushamplify.pushamplifylib:pushamplifylib:1.0.0'
If required, please add the following dependency:
implementation 'com.firebase:firebase-jobdispatcher:0.8.5'
2. Add the following in your AndroidManifest.xml
2.1. API key
The API key identifies your account and is available in your PushAmplify admin panel.
<meta-data android:name="PUSHAMPLIFY_APIKEY" android:value="<your api key>" />
Example:
<meta-data android:name="PUSHAMPLIFY_APIKEY" android:value="57ff22ef-4cd0-41fd- 9815-dc4161032afa" />
2.2. PushHandler class name
The class which is handling your push notifications. This is similar to your FCM push handler.
<meta-data android:name="PUSHAMPLIFY_HANDLER" android:value="<class of handler>" />
Example:
<meta-data android:name="PUSHAMPLIFY_HANDLER" android:value="com.your.app.PushHandler" />
2.3. Push fetch interval (optional)
PushAmplify fetches pushes from its beackend at regular intervals. You can change the fetch interval with this setting. The value should be specified in minutes. This is an optional setting. The default is 30 mins.
<meta-data android:name="PUSHAMPLIFY_INTERVAL_MINS" android:value="<value in minutes>" />
Example:
The following example fetches pushes once every 60 minutes.
<meta-data android:name="PUSHAMPLIFY_INTERVAL_MINS" android:value="60" />
3. Integration
3.1 Initialize
Initialize the SDK once in the lifetime of the app like below:
import com.pushamplify.pushamplifylib.PushAmplifyClient;
String deviceId = PushAmplifyClient.initialize(Context ctx);
// send the deviceId to your server and save it
The initialize(Context)
method takes application context as an argument and returns the deviceId which needs to be passed to your server side and stored. This deviceId is unique for this device and will be used in Push API to send pushes to this device. This is similar to FCM push token for that device.
3.1 Implement your PushHandler
Implement your PushHandler which receives the notifications sent to PushAmplify like below:
package com.your.app;
import com.pushamplify.pushamplifylib.PushAmplifyHandler;
public class PushHandler implements PushAmplifyHandler {
//@Override
public boolean onMessageReceived(Context ctx, Bundle bundle) {
// Your push handling code
}
}
The bundle
contains the push payload that has been sent to PushAmplify using Push API. This is very similar to the message handler of FCM. You can use the handler to do the following:
- Show normal push to your user
- Show rich push to your user
- Refresh or sync your app with your servers
- Detect uninstalls
You can return true to signify successful handling of push. Return false if push handling failed and you would want the push to be delivered again later.