Android
Android Integration
Platform integration
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.

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

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.
2. After the DTDAnalytics initialization block add:
DTDMessaging.Android.Initialize();
3. To activate the Messaging module, call :
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:
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:
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:
<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:
<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 (replacecompany
andpackage
with you own names)
<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)
folderAdd the following strings to the project’s Android manifest file (
\Assets\Plugins\Android\AndroidManifest.xml
):
<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
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)
A property responsible for the activation/deactivation of push notifications.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.
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
DTDPushMessage
). Main class propertiesIDictionary<string,string> GetData():
Complete information passed with the push notification.
DTDActionType ActionType:
The property that returns the value of enum’s DTDActionType
.
Possible values:
App - app open
Url - external link open
Share - share content
Deeplink - an in-app link opening
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
)
DTDActionButton
)DTDActionType ActionType
The property that returns the value of enum’s DTDActionType
.
Possible values:
App - app open
Url - external link open
Share - share content
Deeplink - an in-app link opening
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
The button-click app open mode
Last updated
Was this helpful?