Getting Started
Integrate Firebase into Your Android Project
Create a Firebase Project:
- Go to the Firebase console: https://console.firebase.google.com/ Click on "Add project" and provide the necessary details to create a new Firebase project. Once the project is created, you'll be redirected to the project dashboard. Add Your Android App to the Firebase Project:
- Click on the "Add app" button (Android icon) on the project dashboard. Enter your Android app's package name (e.g., com.example.myapp) and app nickname (optional). Click "Register app" to generate a google-services.json file. Download google-services.json:
After registering your app, you'll be prompted to download the google-services.json file. Save it to the app module of your Android project.
In your root-level (project-level) Gradle file (<project>/build.gradle.kts or <project>/build.gradle), add the Google services plugin as a dependency:
plugins {
// ...
id("com.google.gms.google-services") version "4.3.15" apply false
// ...
}
Integrate EZ Push SDK into Your Project:
Add the following dependency to your app-level build.gradle.kts file:
plugins {
id("com.android.application")
// Add the Google services Gradle plugin
id("com.google.gms.google-services")
// ...
}
// ...
dependencies {
implementation("cloud.ez-push:sdk:4.0.0")
}
Inside the application element (usually just after the activity elements), add the meta-data element as follows:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.yourapp">
<application
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name">
<meta-data
android:name="ezPush_licenseKey"
android:value="your_license_key" />
</application>
</manifest>
To start using the ezPush, initialize it in your application's onCreate method:
// ...
private val coroutineScope = CoroutineScope(SupervisorJob() + Dispatchers.Default)
override fun onCreate() {
super.onCreate()
EZPush.configure(this, coroutineScope) {
logLevel = LogLevel.ALL
icon = R.drawable.ic_launcher_foreground
channelName = "ChannelName"
}
EZPush.init(this)
}
override fun onTerminate() {
super.onTerminate()
coroutineScope.cancel()
}
// ...
In this case, a simple notification will be displayed. The reaction to the click will be the invocation of Intent.ACTION_VIEW with deeplink.
Exception handling
If you want to log the EzPush Exceptions then you can implement our ExceptionHandlerCallback. It allows you to handle exceptions which are categorized into three tiers: LOW, MEDIUM and HIGH. You can use it like this:
EZPush.setExceptionHandlerCallback { exception, severity ->
when (severity) {
Severity.LOW -> Log.d(
"ExceptionHandler",
"This was a Severity.LOW Exception $exception"
)
Severity.MEDIUM -> Log.d(
"ExceptionHandler",
"This was a Severity.MEDIUM Exception $exception"
)
Severity.HIGH -> Log.d(
"ExceptionHandler",
"This was a Severity.HIGH Exception $exception"
)
}
}
Integrating EZPush with Existing Notification Implementation
To seamlessly integrate EZPush with your existing notification implementation, you can start by extending the Firebase Messaging Service and creating a PushServiceWrapper. This wrapper service allows you to combine the functionality of EZPush with your current notification system.
class PushServiceWrapper : FirebaseMessagingService() {
// Lazy initialization of the messaging services
private val messagingServices: List<FirebaseMessagingService> by lazy {
listOf(YourOwnImplementation(), EzPushReceivePushService()).onEach {
it.injectContext(this)
}
}
override fun onNewToken(token: String) {
super.onNewToken(token)
// Propagate the new token to all messaging services
messagingServices.forEach {
it.onNewToken(token)
}
}
override fun onMessageReceived(message: RemoteMessage) {
super.onMessageReceived(message)
messagingServices.forEach {
it.onMessageReceived(message)
}
}
// Inject the context into the messaging services
private fun <T : Service> T.injectContext(context: T) {
setField("mBase", context)
}
// Utility method to set a field in a class using reflection
private fun Any.setField(name: String, value: Any): Boolean =
javaClass.findDeclaredField(name)?.let {
try {
it.isAccessible = true
it.set(this, value)
true
} catch (e: NoSuchFieldException) {
false
} catch (e: SecurityException) {
false
}
} ?: false
// Find a declared field in a class or its superclass
private fun Class<*>.findDeclaredField(name: String): Field? {
var clazz: Class<*>? = this
do {
try {
return clazz?.getDeclaredField(name)
} catch (e: NoSuchFieldException) {
// Continue searching in the superclass if the field is not found
}
clazz = clazz?.superclass
} while (clazz != null)
return null
}
}
To complete the integration of PushServiceWrapper with your Android application, you need to register it in your AndroidManifest.xml file. Replace the default implementation with PushServiceWrapper to ensure that your application utilizes this extended service for Firebase Cloud Messaging (FCM).
<application>
<!-- Other application components and settings -->
<service
android:name=".PushServiceWrapper"
android:exported="false">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
<!-- Additional service entries, if any -->
</application>
This integration allows you to leverage the power of EZPush alongside your existing notification system, providing a seamless experience for both your users and your development team.