Push Notifications on Android are sent with the help of the FCM service.
How to create a project in Firebase and integrate Firebase Services into your application, you can find in Firebase documentation.
Next, you need to specify the FCM Server key in the push notifications integration settings panel in the application settings section in devtodev service (App → Settings → Push notifications → Push notifications panel)
To get the FCM Server key, go to the Project Settings of your Android project in the Firebase Console and copy the Server key from the Cloud Messaging tab.
Messaging Module Integration
The Messaging module is available as an AAR (recommended) and JAR library. The library is available in the MavenCentral and GitHub repository.
1. If you use Gradle for the applications build, add mavenCentral() into gradle.build file of your application and specify the following relationship in dependencies block:
defaultConfig {//other existing fields versionName ="1.0"//your app version (required)}dependencies {// Requirementimplementation("androidx.appcompat:appcompat:*.*.*")implementation("com.google.code.gson:gson:*.*.*")implementation("com.google.firebase:firebase-messaging:*.*.*")implementation("com.google.android.gms:play-services-ads-identifier:*.*.*")// if you use AAR (recommended) or JAR downloaded from GitHub, please add:implementation(fileTree(mapOf("dir" to "libs", "include" to listOf("*.aar"))))// or just add the dependency, get the latest version from// https://mvnrepository.com/artifact/com.devtodev/android-analyticsimplementation("com.devtodev:android-analytics:*.*.*")// https://mvnrepository.com/artifact/com.devtodev/android-messagingimplementation("com.devtodev:android-messaging:*.*.*")// Optional (recommended)implementation("com.android.installreferrer:installreferrer:*.*")}
defaultConfig {//other existing fields versionName "1.0"//your app version (required)}dependencies {// Requirement implementation 'androidx.appcompat:appcompat:*.*.*' implementation 'com.google.code.gson:gson:*.*.*' implementation 'com.google.android.gms:play-services-ads-identifier:*.*.*' implementation 'com.google.firebase:firebase-messaging:*.*.*'// if you use AAR (recommended) or JAR downloaded from GitHub, please add: implementation fileTree(dir: "libs", include: ["*.aar"]) // or just add the dependency, get the latest version from// https://mvnrepository.com/artifact/com.devtodev/android-analytics implementation 'com.devtodev:android-analytics:*.*.*'// https://mvnrepository.com/artifact/com.devtodev/android-messaging implementation 'com.devtodev:android-messaging:*.*.*'// Optional (recommended) implementation 'com.android.installreferrer:installreferrer:*.*'}
2. To the app manifest add the following:
<!-- permission.POST_NOTIFICATIONS for API level 33 or higher --><uses-permissionandroid:name=“android.permission.POST_NOTIFICATIONS”/><application> <serviceandroid:name="com.devtodev.push.internal.logic.DTDFcmMessagingService"android:exported="true"> <intent-filter> <actionandroid:name="com.google.firebase.MESSAGING_EVENT"/> </intent-filter> </service> <receiverandroid:name="com.devtodev.push.internal.logic.PushClickReceiver"android:enabled="true"android:exported="true"> <intent-filter> <actionandroid:name="com.devtodev.android.push.CLICKED" /> </intent-filter> </receiver></application>
3. To add a user icon to your push notification and change its color, add the following strings to the manifest file code:
6. Call the DTDMessaging.startPushService() method to activate the Messaging module.
Android 13 or higher
When using Android 13 or higher, notifications are disabled by default. The app won’t receive notifications until you request a new permission (POST_NOTIFICATIONS) and the user grants this permission to your app.
For notifications to work properly, add the following line to the manifest file:
You can check the operation of the POST_NOTIFICATIONS permission in your app by inserting this example in the code:
@RequiresApi(33)privatefunnotificationPermissionIsGranted(): Boolean {val res = context.checkCallingOrSelfPermission(POST_NOTIFICATIONS)return res == PackageManager.PERMISSION_GRANTED}
@RequiresApi(33)privateBooleannotificationPermissionIsGranted() {int res =context.checkCallingOrSelfPermission(POST_NOTIFICATIONS);return res ==PackageManager.PERMISSION_GRANTED;}
If the check results in a negative answer (access is not granted), call this method:
Its execution will call a dialog that in turn will ask the user to opt in:
This code is going to help you get the user’s decision:
overridefunonRequestPermissionsResult( requestCode: Int, permissions: Array<String?>, grantResults: IntArray) {super.onRequestPermissionsResult(requestCode, permissions, grantResults)if (requestCode == yourCode) {if (grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED) {//permission is granted } else {//permission is not granted, notifications are not available } }}
@OverridepublicvoidonRequestPermissionsResult(int requestCode, @NonNullString[] permissions, @NonNullint[] grantResults) { super.onRequestPermissionsResult(requestCode, permissions, grantResults);if (requestCode == yourCode) {if (grantResults.length>0&& grantResults[0] ==PackageManager.PERMISSION_GRANTED) {//permission is granted } else {//permission is not granted, notifications are not available } }}
Note. In SDK ver. 2.1.5 or higher, if the permission is not granted, you will see this message in the log from DTDMessaging: “Notifications don’t work. Permission android.permission.POST_NOTIFICATIONS is not granted”.
External interface of the DTDMessagingmodule
Object
Description
DTDMessaging
The main object for push notification initialization.
DTDMessaging.initialize(Context context)
The push notification initialization method.
DTDMessaging.startPushService()
The push notification activation method. It passes the isAllowed current state.
DTDMessaging.pushNotificationsAllowed = true or false
A property responsible for the activation/deactivation of push notifications.
Functions as a getter (describes the current state) and a setter (sets the current state).
When the state transitions, it sends a pt with isAllowed (true or false) status to the server.
The isAllowed flag status is stored in the SDK.
DTDMessaging.setIntent(Intent intent)
A method of passing a user intent to the SDK using PushMessage.
Written in the manifest file
<meta-data android:name="com.devtodev.push.default_small_icon" android:resource="@drawable/smallIcon" />
Sets a small custom user icon.
Written in the manifest file
<meta-data android:name="com.devtodev.default_small_icon_color" android:resource="@color/colorPrimary" />
Sets a color of the small custom user icon.
Written in the manifest file
<meta-data android:name="com.devtodev.push.default_large_icon" android:resource="@mipmap/largeIcon" />
Sets a large custom user icon.
DTDMessaging.getToken()
Returns a push notification registration token (firebaseToken).