# Push Notification

## Overview

You can set up push notifications to notify your end users for incoming chat messages. For managing push notifications, you must have a [Firebase account](https://console.firebase.google.com/).

> Note: If you already have a Firebase account and code to retrieve firebase registration token for the client app instance, then skip this section and move to [Push notification setup](#push-notification-setup) section.

### 1) Get the `google-services.json` file

1. If you haven't already added firebase to your project, add [Firebase to your Android project](https://firebase.google.com/docs/android/setup)
2. Once you're done with above setup, download the `google-services.json file` from firebase console, click Settings icon -> Project settings -> General -> Scroll to bottom -> Select your app -> Click the `google-services.json` button.
3. Place the `google-services.json` file under the root directory of your app.

### 2) Obtain server key from firebase

1. From firebase console, click Settings icon -> Project settings
2. Once you clicked project settings select **Cloud Messaging** tab under Settings. Under Project Credentials, copy your **Server Key** which is highlighted blue in the following image.

> Note: Make sure you copy the Server key and not the Legacy key or Sender ID.

### 3) Add your server key to Kommunicate

Go to [Push notification section](https://dashboard.kommunicate.io/settings/pushnotification) in Kommunicate dashboard and update the FCM server key under the 'GCM/FCM Key' section. You can find it in the Settings -> Cloud Messaging -> Server Key section of your Firebase console.

### 4) Gradle file configuration

1. Open your project level(root level) `build.gradle` file and under `buildscript` node, add the below classpath inside `dependencies`:

```
 classpath 'com.google.gms:google-services:3.1.1'
```

1. Open your app level `build.gradle` and at the bottom of the file apply the plugin as below:

```
apply plugin: 'com.google.gms.google-services'
```

If you do not have any push notification setup, kommunicate will do it internally for you. If you already have the setup, follow the next section.

## Already have the push notification setup?

If you already have the push notification setup, follow the below steps.

### 1) Send the device token to Kommunicate

The first step in the setup would be to send the deviceToken to Kommunicate. This needs to be done in `onNewToken` method of your `FirebaseMessagingService` subclass. The `onNewToken` method is called whenever firebase updates the deviceToken on that device.

> Note: `onNewToken` method provides `registrationId`, this is the deviceToken.

Use the below code to send the token to Kommunicate.

```
Kommunicate.updateDeviceToken(context, registrationId);
```

### 2) Receive push notifications

For Receiving FCM Notifications in your app, add the following code in your `FirebaseMessagingService` inside `onMessageReceived(RemoteMessage remoteMessage)`:

```
if (Kommunicate.isKmNotification(this, remoteMessage.getData())) {
    return;
}
```

## Set notification small icon

To set the notification small icon, place the below metadata in your `AndroidManifest.xml` file under `tag` :

```
 <meta-data 
    android:name="com.applozic.mobicomkit.notification.smallIcon"
    android:resource="" /> 
```

> Note: Android version 6.0 onwards, android automatcially converts the small icon color to monochrome. While some custom ROM devices do show colored icons, it is recommended that you use a shaped icon with some transparent background instead of round solid icons as a small icon.
