Android 13 (API level 33) and higher supports runtime permission for sending non-exempt(including Foreground Services (FGS)) notifications from an app POST_NOTIFICATIONS. This change helps users focus on the notifications that are most important to them.
Refer to the official documentation for more details.
The below-mentioned APIs are supported starting MoEngage core Android SDK version 12.3.01
When an application runs on Android 13 and wants to show notifications to the user, it must request the user's notification permission. You have two options: let MoEngage handle permissions for you or handle the notification permission with your code.
- MoEngage handles Notification permission.
- You just have to call a single line of code mentioned on this page.
- You maintain the notification permission logic.
- Notify MoEngage SDK if permission to push notifications is granted.
We recommend you let MoEngage handle push notification permissions.
Notify SDK of permission result
Once the application requests the user for notification permission notify the SDK of the user response using the below API. If you are letting MoEngage SDK handle the notification permissions, you should ignore this step.
MoEPushHelper.getInstance().pushPermissionResponse(applicationContext, isGranted)
MoEPushHelper.getInstance().pushPermissionResponse(applicationContext, isGranted);
Update the Permission request count(optional)
Once the application requests the user for notification permission, update the SDK of the request attempts.
Why does the SDK require permission attempt count?
SDK requires the attempt count to accurately track the number of times the permission request was attempted. If the user denies the permission request twice the application/SDK cannot request permission further i.e. if we request permission it would be automatically denied without the user seeing the request dialog. As an SDK we cannot be sure of the number of times the user has denied the permission we try to optimize the calls to request permission based on the attempt count.
MoEPushHelper.getInstance().updatePushPermissionRequestCount(applicationContext, requestCount)
MoEPushHelper.getInstance().updatePushPermissionRequestCount(applicationContext, requestCount);
Setup Notification Channels
If the application has already taken notification permission from the user call the below API to set up Notification Channels for showing push notifications.
MoEPushHelper.getInstance().setUpNotificationChannels(context)
MoEPushHelper.getInstance().setUpNotificationChannels(context);
Let MoEngage SDK handle Notification permission
MoEngage SDK provides helper APIs to show the permission request to the end-user or navigate the user to the settings screen for enabling notifications.
Use the below API to show the permission request dialog to the user.
When using the below API SDK automatically tracks the response, sets up the required notification channel, etc, mentioned above.
MoEPushHelper.getInstance().requestPushPermission(activity)
MoEPushHelper.getInstance().requestPushPermission(activity);
Navigate to Notification Settings (situational/optional)
Use the below API to show navigate the user to the Notification Settings for the application on the device.
Note: Below Android 8(API level 26) the user is directed to the Application Info screen of your application.
MoEPushHelper.getInstance().navigateToSettings(activity)
MoEPushHelper.getInstance().navigateToSettings(activity);