# Android

## Platform integration

Push Notifications on Android are sent with the help of the FCM service.

You can find instructions on how to create a project in Firebase and integrate Firebase Services into your application in the [Firebase documentation](https://firebase.google.com/docs/android/setup).

In devtodev (Settings → Push notifications → Push notifications panel), specify the Firebase project ID and authorize devtodev to send messages and manage messaging subscriptions for your Firebase application.&#x20;

<figure><img src="https://2105883905-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LnGcP_ZeRJ1ipj9O8dF%2Fuploads%2FKIrB1eRsMwPJ6wzyLsjO%2Fimage.png?alt=media&#x26;token=ef84265e-7adf-4831-88c0-604f0a5f5c62" alt=""><figcaption></figcaption></figure>

To get the Firebase project ID, go to the Project Settings → General of your Android project in the Firebase Console. Copy the ID and paste it in devtodev settings.

![](https://2105883905-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LnGcP_ZeRJ1ipj9O8dF%2Fuploads%2FjGplvFD8BiywUlQrFAxe%2Fandroid_push_doc7.png?alt=media\&token=41b2e5bc-205a-4c35-ab7f-35072ef16b4f)

Download google-services.json and put it to Assets folder.

![](https://2105883905-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LnGcP_ZeRJ1ipj9O8dF%2Fuploads%2FBKnZ7Ze4nVAQSIamzZ9Z%2Fimage%20\(2\).png?alt=media\&token=bf67a39c-b8ac-4a7d-b79b-512372d78c32)

## Module initialization

1\. For the Messaging module to function you need the basic Analytics package. Before the notification initialization, you need to initialize the SDK. More about it you can read here: [Unity Integration](https://docs.devtodev.com/integration/integration-of-sdk-v2/sdk-integration/unity).

2\. After the DTDAnalytics initialization block add:

```csharp
DTDMessaging.Android.Initialize();
```

3\. To activate the Messaging module, call :

```csharp
DTDMessaging.Android.StartPushService();
```

Optional:

You can listen to basic notification module events. To do this, create a class that implements the **`IDTDPushListener`** interface and pass it to the **`DTDMessaging.Android.SetPushListener`** method.

Example of the class:

```csharp
public class MyPushListener : IDTDPushListener
{
    public void OnPushServiceRegistrationSuccessful(string deviceId)
    {
        Debug.Log(deviceId);
    }

    public void OnPushServiceRegistrationFailed(string error)
    {
        Debug.Log(error);

    }

    public void OnPushNotificationReceived(DTDPushMessage message)
    {
        Debug.Log(message);
    }

    public void OnInvisibleNotificationReceived(DTDPushMessage message)
    {
        //IOS only.
    }

    public void OnPushNotificationOpened(DTDPushMessage pushMessage, DTDActionButton actionButton)
    {
        Debug.Log(pushMessage.ToString());
        Debug.Log(actionButton.ToString());
    }
}
```

Full example of notification module initialization:

```csharp
public class MyPushListener : IDTDPushListener
{
    public void OnPushServiceRegistrationSuccessful(string deviceId)
    {
        Debug.Log(deviceId);
    }

    public void OnPushServiceRegistrationFailed(string error)
    {
        Debug.Log(error);
    }

    public void OnPushNotificationReceived(DTDPushMessage message)
    {
        Debug.Log(message);
    }

    public void OnInvisibleNotificationReceived(DTDPushMessage message)
    {
        Debug.Log(message);
    }

    public void OnPushNotificationOpened(DTDPushMessage pushMessage, DTDActionButton actionButton)
    {
        Debug.Log(pushMessage.ToString());
        Debug.Log(actionButton.ToString());
    }
}

public class NotificationExample : MonoBehaviour
{
    private const string APP_KEY = "***************"
    void Start()
    {
        DTDAnalytics.Initialize(APP_KEY);
        DTDMessaging.Android.SetPushListener(new PushListener());
        DTDMessaging.Android.Initialize();
        DTDMessaging.Android.StartPushService();
    }
}
```

### Set a custom icon and sound for push notifications

#### Unity 2019 and older

To set a custom sound, icon and its colour in a push notification, copy icons and sounds files in the `Assets/Plugins/Android/res/` folder and add the following strings to the manifest file code:

```csharp
<meta-data
 android:name="com.devtodev.push.default_small_icon"
 android:resource="@drawable/ic_icon_name" />

<meta-data
 android:name="com.devtodev.push.default_small_icon_color"
 android:resource="@color/icon_color" />
```

To set a large user icon in the push notification, add:

```csharp
<meta-data
android:name="com.devtodev.push.default_large_icon"
android:resource="@mipmap/ic_large_icon_name"/>
```

#### Unity 2020 and newer

* Delete the ***`\Assets\Plugins\Android\res`*** folder (together with the ***`.meta`*** file) - it will cause an error during assembly.
* Create a new folder in a separate folder outside of the project.
* In the new folder, create ***`AndroidManifest.xml`*** with the following content (replace **`company`** and **`package`** with you own names)

```csharp
<manifest package="com.company.package">
</manifest>
```

* Create a res folder in the same folder.
* Add your resources to the res folder while keeping the folder structure intact (drawable, xml, raw, etc.). An example of the resulting structure:

```
├─── AndroidManifest.xml
└─── res
    └─── drawable
        └─── smallIcon0.png
     └─── mipmap
        └─── largeIcon0.png
     └─── raw
        └─── iconsound.wav 
```

* Run the following code in the in the command line/terminal:

```
jar cvf resources.aar -C . .
```

* Place the resulting aar file to the ***`\Assets\Plugins\Android\ (edited)`*** folder
* Add the following strings to the project’s Android manifest file ( ***`\Assets\Plugins\Android\AndroidManifest.xml`***):

```csharp
<meta-data android:name="com.devtodev.push.default_small_icon" android:resource="@drawable/smallIcon0" />
<meta-data android:name="com.devtodev.push.default_large_icon" android:resource="@mipmap/largeIcon0" />
```

### External interface of the DTDMessaging module for Android platform

| Method                                                                                                  | Description                                                                                                                                                                                                                                                                                                                                              |
| ------------------------------------------------------------------------------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| **`void DTDMessaging.Android.Initialize()`**                                                            | The push notification initialization method                                                                                                                                                                                                                                                                                                              |
| **`void DTDMessaging.Android.StartPushService()`**                                                      | The push notification activation method. It passes the **`isAllowed`** current state                                                                                                                                                                                                                                                                     |
| **`void DTDMessaging.Android.PushIsAllowed (bool isAllowed)`**                                          | <p>A property responsible for the activation/deactivation of push notifications.When the state transitions, it sends a pt with <strong><code>isAllowed</code></strong> (<em><strong>true</strong></em> or <em><strong>false</strong></em>) status to the server.</p><p>The <strong><code>isAllowed</code></strong> flag status is stored in the SDK.</p> |
| **`void DTDMessaging.Android.GetPushState(Action<bool?> onGetPushState)`**                              | The method that returns the push module state to **`onGetPushState`** callback. If getting the current state is impossible, it returns null                                                                                                                                                                                                              |
| **`void DTDMessaging.Android.GetToken(Action<string> onGetToken)`**                                     | The method that returns push registration token to onGetToken callback                                                                                                                                                                                                                                                                                   |
| **`void DTDMessaging.Android.ProcessPushNotification (IDictionary<string, string> firebaseMessaging)`** | Used to pass the push notification to the **`FirebaseMessagingService`** if it was implemented by the client but not by the SDK                                                                                                                                                                                                                          |
| **`void DTDMessaging.SetPushListener (IDTDPushListener pushListener)`**                                 | It sets a listener for push notification event trapping                                                                                                                                                                                                                                                                                                  |

### Class for receiving Notification data (***`DTDPushMessage`***). Main class properties

| Property                                          | Description                                                                                                                                                                                                                                                                                                                                              |
| ------------------------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| **`IDictionary<string,string> GetData():`**       | Complete information passed with the push notification.                                                                                                                                                                                                                                                                                                  |
| **`DTDActionType ActionType:`**                   | <p>The property that returns the value of enum’s <strong><code>DTDActionType</code></strong>.</p><p>Possible values:</p><p><em><strong>App</strong></em> - app open</p><p><em><strong>Url</strong></em> - external link open</p><p><em><strong>Share</strong></em> - share content</p><p><em><strong>Deeplink</strong></em> - an in-app link opening</p> |
| **`string ActionString`**                         | The property that returns an optional action identifier                                                                                                                                                                                                                                                                                                  |
| **`IDictionary<string,string> AdditionalData()`** | Additional data sent to push notification                                                                                                                                                                                                                                                                                                                |

### Class for handling buttons clicked in the notification (***`DTDActionButton`***)

| Property                       | Description                                                                                                                                                                                                                                                                                                                                              |
| ------------------------------ | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| **`DTDActionType ActionType`** | <p>The property that returns the value of enum’s <strong><code>DTDActionType</code></strong>.</p><p>Possible values:</p><p><em><strong>App</strong></em> - app open</p><p><em><strong>Url</strong></em> - external link open</p><p><em><strong>Share</strong></em> - share content</p><p><em><strong>Deeplink</strong></em> - an in-app link opening</p> |
| **`string ActionString`**      | Property that returns an optional action identifier                                                                                                                                                                                                                                                                                                      |
| **`string ButtonId`**          | Property that returns the ID of the clicked button                                                                                                                                                                                                                                                                                                       |
| **`string ButtonText`**        | Property that returns the text of the clicked button                                                                                                                                                                                                                                                                                                     |
| **`string ButtonIcon`**        | Property that returns the button icon name                                                                                                                                                                                                                                                                                                               |
| **`bool IsBackground`**        | <p>The button-click app open mode<br></p>                                                                                                                                                                                                                                                                                                                |
