Push Notifications
Learn how to set up push notifications for promo campaigns using Adapty Android SDK
We use Firebase Cloud Messaging (FCM) to send push notifications from Adapty. If you need to install it, please refer to the official documentation.
Make sure to set Firebase Cloud Messaging (FCM) server key in Adapty Dashboard, without it, we can't send push notifications.
To handle push notifications from Adapty, you can inherit from AdaptyPushHandler
like this:
class YourAdaptyPushHandler(context: Context) : AdaptyPushHandler(context) {
override val clickAction = "YOUR_NOTIFICATION_CLICK_ACTION"
override val smallIconResId = R.drawable.ic_notification_small_icon
}
public class YourAdaptyPushHandler extends AdaptyPushHandler {
public YourAdaptyPushHandler(@NotNull Context context) {
super(context);
}
@NotNull
@Override
public String getClickAction() {
return "YOUR_NOTIFICATION_CLICK_ACTION";
}
@Override
public int getSmallIconResId() {
return R.drawable.ic_notification_small_icon;
}
}
clickAction
refers to click_action parameter from Firebase notification message body. You also need to declare the same action in your AndroidManifest
in Activity you want to be launched after the push is clicked:
<activity android:name=".YourActivity">
<!-- your intent-filters-->
<intent-filter>
<action android:name="YOUR_NOTIFICATION_CLICK_ACTION" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
Optional properties of AdaptyPushHandler
(or getters in case of Java) you can override:
largeIcon
is a nullable Bitmap (ignored in case of null), refers tosetLargeIcon
from Notification.BuildercustomizedNotificationBuilder
is a nullableNotification.Builder
(ignored in case of null). In this case, you don't need to specifysetSmallIcon
,setContentIntent
,setContentTitle
andsetContentText
in your custom builder as it will be overwritten by Adapty SDK. If you overridecustomizedNotificationBuilder
with non-null value, propertylargeIcon
is ignored (so you need to specify it in your custom builder if needed).channelId
is the ID of the notification channel you want promo notifications to be associated with. You can override with the ID of the already existing channel (if the channel exists it won't be overwritten). If you pass the ID of an unexisting channel, Adapty will create a new one where ID and name are equal to ID you passed. If you don't overridechannelId
, Adapty will create its default channel called "Offers".
Setup your messaging service to work with promo notifications
You only need to do a few things — register the new Firebase token using .refreshPushToken()
method and delegate handling promo notifications to your AdaptyPushHandler
- just like in the example below:
class YourFirebaseMessagingService : FirebaseMessagingService() {
private val pushHandler: YourAdaptyPushHandler by lazy {
YourAdaptyPushHandler(this)
}
override fun onNewToken(pushToken: String) {
super.onNewToken(pushToken)
Adapty.refreshPushToken(pushToken)
}
override fun onMessageReceived(message: RemoteMessage) {
super.onMessageReceived(message)
if (!pushHandler.handleNotification(message.data)) {
/*
here is your logic for other notifications
that haven't been handled by Adapty
*/
}
}
}
Handle promo notifications in your Activity
Click on the notification will open the Activity where you specified your action. You can handle an Intent to your Activity like this:
class YourActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
// your logic
handleIntent(intent)
}
override fun onNewIntent(intent: Intent) {
super.onNewIntent(intent)
setIntent(intent)
handleIntent(intent)
}
private fun handleIntent(intent: Intent) {
if (Adapty.handlePromoIntent(intent) { promo, error ->
// your logic for callback
}
) {
// your logic for the case user did click on promo notification,
// for example show loading indicator
} else {
// your logic for other cases
}
}
}
public class YourActivity extends AppCompatActivity {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// your logic
handleIntent(getIntent());
}
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
setIntent(intent);
handleIntent(intent);
}
private void handleIntent(Intent intent) {
if (Adapty.handlePromoIntent(intent, (promo, error) -> {
// your logic for callback
// please note that promo can be null
return null;
})) {
// your logic for the case user did click on promo notification,
// for example show loading indicator
} else {
// your logic for other cases
}
}
}
Updated about 1 year ago