# Android

{% hint style="danger" %}
**This generation of SDK is deprecated and is no longer supported.**\
Information about the [current version can be found here](https://docs.devtodev.com/integration/integration-of-sdk-v2/push-notifications/android).
{% endhint %}

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.&#x20;

## Creating a project in Firebase

Add a new project to the [Firebase console](https://console.firebase.google.com/).&#x20;

![](https://www.devtodev.com/upload/images/android_push_doc1.png)

Fill in the name and country of your project.

![](https://www.devtodev.com/upload/images/android_push_doc2.png)

Congratulations, the project has been created! Now you need to indicate the package name of your Android app.

![](https://www.devtodev.com/upload/images/android_push_doc4.png)

![](https://www.devtodev.com/upload/images/%D0%A1%D0%BD%D0%B8%D0%BC%D0%BE%D0%BA%20%D1%8D%D0%BA%D1%80%D0%B0%D0%BD%D0%B0%202018-09-25%20%D0%B2%2013.44.45.png)\
After a successful registration in Firebase, you can receive your keys that you will use in devtodev.&#x20;

## Obtaining the necessary keys

Go to the settings of your Android project.

![](https://www.devtodev.com/upload/images/android_push_doc7.png)

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.&#x20;

![](https://www.devtodev.com/upload/images/android_push_doc8.png)

## Implementation in the app

Download the generated by google-service.json file and add it to the project.&#x20;

The Google services plugin for [Gradle](https://gradle.org/) 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):&#x20;

   ```
   buildscript {
     dependencies {
       // Add this line
       classpath 'com.google.gms:google-services:4.3.3'
     }
   }
   ```
2. App-level build.gradle (\<project>/\<app-module>/build.gradle):

   ```
   dependencies {
      ...
      implementation 'com.devtodev:android:1.14.5'
      implementation 'com.google.android.gms:play-services-base:17.1.0'
      implementation 'com.google.firebase:firebase-core:17.2.3'
      implementation 'com.google.firebase:firebase-messaging:20.1.0'
   }
   // Add to the bottom of the file
   apply plugin: 'com.google.gms.google-services'
   ```
3. Finally, press "Sync now" in the bar that appears in the IDE: \
   ![](https://www.gstatic.com/mobilesdk/160330_mobilesdk/images/android_studio_gradle_changed_butterbar@2x.png)
4. In Activity (where the SDK initializes) add the push notifications initialization and the listener of push notifications processing.

   ```
   public class MainActivity extends AppCompatActivity implements PushListener {

       @Override
       protected void onCreate(Bundle savedInstanceState) {
           super.onCreate(savedInstanceState);
           setContentView(R.layout.activity_main);

           DevToDevPushManager.setPushListener(this);
           DevToDevPushManager.init(getIntent());
       }

       @Override
       public void onRegisteredForPushNotifications(String s) {
           // Insert the code for processing the received token

       }

       @Override
       public void onFailedToRegisteredForPushNotifications(String s) {
           // Insert the code for tracking integration errors
       }

       @Override
       public void onPushNotificationsReceived(Map<String, String> map) {
           // Insert the code to track received notifications
       }

       @Override
       public void onPushNotificationOpened(PushMessage pushMessage, @Nullable ActionButton actionButton) {
           // Insert the code to track opened notification
       }
   }
   ```
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:

   ```
   DevToDevPushManager.setCustomSmallIcon(int resourceId);
   ```

   To set the default color of the small icon:

   ```
   DevToDevPushManager.setCustomSmallIconColor(int colorHexadecimal)
   ```

   To set the large icon:

   ```
   DevToDevPushManager.setCustomLargeIcon(int resourceId);
   ```

   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.

{% hint style="warning" %}
**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:
{% endhint %}

```
DevToDevPushManager.displayPushNotification(Context context, RemoteMessage remoteMessage);
```

For example:

```
public class MyFirebaseMessagingService extends FirebaseMessagingService {
    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        Map<String, String> data = remoteMessage.getData();
        if (data != null) {
            if (data.containsKey("_k")) {
                DevToDevPushManager.displayPushNotification(this, remoteMessage);
            } else {
                showNotification(remoteMessage);
            }
        }
    }
}
```

## Changing the application settings in devtodev system

1\. Go to [Firebase console](https://console.firebase.google.com/) and then to your project settings. On the Cloud messaging tab get the Firebase Cloud Messaging token of your project.

![](https://www.devtodev.com/upload/images/6\(9\).png)

2\. Proceed to Settings of your app in devtodev.

![](https://2105883905-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LnGcP_ZeRJ1ipj9O8dF%2F-M4SP9Ew7FnBSZFBkMjk%2F-M4SRFz2Ydu2T5p4b1Ye%2Fimage.png?alt=media\&token=b968c51a-e81f-432b-a1f2-f0b2bfca9189)

3\. Go to Integration page and insert the previously received Firebase Cloud Messaging token to the FCM token field in Push notifications section.

![](https://2105883905-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LnGcP_ZeRJ1ipj9O8dF%2F-M4SP9Ew7FnBSZFBkMjk%2F-M4ST26oGxcbNPkMp3Kz%2Fimage.png?alt=media\&token=6c011fd4-3357-4f42-bb2c-dfa6f614bf13)

4\. If the Firebase Cloud Messaging token is correct, you will see the following result

![](https://2105883905-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LnGcP_ZeRJ1ipj9O8dF%2F-M4SP9Ew7FnBSZFBkMjk%2F-M4STdF3AeQYCDGAPua2%2Fimage.png?alt=media\&token=a31c30fa-1563-4ade-85e6-315beb4fde1f)

## 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!

{% hint style="warning" %}
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.
{% endhint %}
