Android
Android Push Notifications
Push Notifications on Android are sent with the help of the FCM service. To work with it, two keys are required: a client and server key. If you have a project, move on to the second part of this article.

Creating a project in Firebase

Add a new project to the Firebase console.
Fill in the name and country of your project.
Congratulations, the project has been created! Now you need to indicate the package name of your Android app.
After a successful registration in Firebase, you can receive your keys that you will use in devtodev.

Obtaining the necessary keys

Go to the settings of your Android project.
Remember or copy Server key and Sender ID from the Cloud Messaging tab. You will need them to integrate push notifications and create push campaigns.

Implementation in the app

Download the generated by google-service.json file and add it to the project.
The Google services plugin for Gradle loads the google-services.json file that you just downloaded. Modify your build.gradle files to use the plugin.
    1.
    Project-level build.gradle (<project>/build.gradle):
    1
    buildscript {
    2
    dependencies {
    3
    // Add this line
    4
    classpath 'com.google.gms:google-services:4.3.3'
    5
    }
    6
    }
    Copied!
    2.
    App-level build.gradle (<project>/<app-module>/build.gradle):
    1
    dependencies {
    2
    ...
    3
    implementation 'com.devtodev:android:1.14.5'
    4
    implementation 'com.google.android.gms:play-services-base:17.1.0'
    5
    implementation 'com.google.firebase:firebase-core:17.2.3'
    6
    implementation 'com.google.firebase:firebase-messaging:20.1.0'
    7
    }
    8
    // Add to the bottom of the file
    9
    apply plugin: 'com.google.gms.google-services'
    Copied!
    3.
    Finally, press "Sync now" in the bar that appears in the IDE:
    4.
    In Activity (where the SDK initializes) add the push notifications initialization and the listener of push notifications processing.
    1
    public class MainActivity extends AppCompatActivity implements PushListener {
    2
    3
    @Override
    4
    protected void onCreate(Bundle savedInstanceState) {
    5
    super.onCreate(savedInstanceState);
    6
    setContentView(R.layout.activity_main);
    7
    8
    DevToDevPushManager.setPushListener(this);
    9
    DevToDevPushManager.init(getIntent());
    10
    }
    11
    12
    @Override
    13
    public void onRegisteredForPushNotifications(String s) {
    14
    // Insert the code for processing the received token
    15
    16
    }
    17
    18
    @Override
    19
    public void onFailedToRegisteredForPushNotifications(String s) {
    20
    // Insert the code for tracking integration errors
    21
    }
    22
    23
    @Override
    24
    public void onPushNotificationsReceived(Map<String, String> map) {
    25
    // Insert the code to track received notifications
    26
    }
    27
    28
    @Override
    29
    public void onPushNotificationOpened(PushMessage pushMessage, @Nullable ActionButton actionButton) {
    30
    // Insert the code to track opened notification
    31
    }
    32
    }
    Copied!
    5.
    Optional. To set the custom icons to be shown in push-notification on the SDK part, use the following methods: To set the small icon:
    1
    DevToDevPushManager.setCustomSmallIcon(int resourceId);
    Copied!
    To set the default color of the small icon:
    1
    DevToDevPushManager.setCustomSmallIconColor(int colorHexadecimal)
    Copied!
    To set the large icon:
    1
    DevToDevPushManager.setCustomLargeIcon(int resourceId);
    Copied!
    Use resources of your app. For example, R.drawable.ic_launcherIcons which set in the push-notification wizard have priority over the icons which set in these methods.
In case you use several push notifications services or would like to use your own implementation of FirebaseMessagingService, please add the call of the method for displaying messages sent from the devtodev system:
1
DevToDevPushManager.displayPushNotification(Context context, RemoteMessage remoteMessage);
Copied!
For example:
1
public class MyFirebaseMessagingService extends FirebaseMessagingService {
2
@Override
3
public void onMessageReceived(RemoteMessage remoteMessage) {
4
Map<String, String> data = remoteMessage.getData();
5
if (data != null) {
6
if (data.containsKey("_k")) {
7
DevToDevPushManager.displayPushNotification(this, remoteMessage);
8
} else {
9
showNotification(remoteMessage);
10
}
11
}
12
}
13
}
Copied!

Changing the application settings in devtodev system

1. Go to Firebase console and then to your project settings. On the Cloud messaging tab get the Firebase Cloud Messaging token of your project.
2. Proceed to Settings of your app in devtodev.
3. Go to Integration page and insert the previously received Firebase Cloud Messaging token to the FCM token field in Push notifications section.
4. If the Firebase Cloud Messaging token is correct, you will see the following result

Creating a new push notification in devtodev interface

    1.
    Open PUSH NOTIFICATIONS section and click on 'Add new campaign" button
    2.
    Fill in campaign name, select an app for delivery*
    3.
    Choose user group to send a message. You can choose existing segment or create a new one
    4.
    Enter notification details
    5.
    Make some tests and correct the message if it's required
    6.
    Schedule the delivery
    7.
    That's it!
You can create a campaign only after at least one push token comes from devtodev SDK integrated to your application. Otherwise the app will not be displayed in the list.
Last modified 1mo ago