Notification Service Extension
On Android you can modify the notification and optionally silence it with a Notification Service Extension. Here is how to set it up:
Minimum CleverPush Android SDK Version: 1.5.0
For displaying notifications, we previously used NotificationExtenderService
, which is now deprecated. Please use NotificationServiceExtension
instead.
Deprecation Notice:
NotificationExtenderService
is deprecated from the android sdk version 1.33.7. It is recommended to migrate to NotificationServiceExtension
. Remove any references to NotificationExtenderService
in your code and replace them with NotificationServiceExtension
.
Create the MyNotificationServiceExtension
class:
import android.util.Log;
import androidx.core.app.NotificationCompat;
import com.cleverpush.NotificationReceivedEvent;
import com.cleverpush.NotificationServiceExtension;
import java.math.BigInteger;
public class MyNotificationServiceExtension implements NotificationServiceExtension {
@Override
public void onNotificationReceived(NotificationReceivedEvent event) {
Log.d("CleverPush", "CleverPush MyNotificationServiceExtension onNotificationReceived");
// call `event.preventDefault()` to not display notification
// event.preventDefault();
// to prevent the `default` notification channel creation, use `event.getNotification().setNotificationChannel()`
/*if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
NotificationChannel channel;
String channelId = "channel_id"; // replace with your desired channel id
CharSequence channelName = "Channel_Name"; // replace with your desired channel name
int importance = NotificationManager.IMPORTANCE_DEFAULT;
channel = new NotificationChannel(channelId, channelName, importance);
event.getNotification().setNotificationChannel(channel);
}*/
// modify notification
event.getNotification().setExtender(new NotificationCompat.Extender() {
@Override
public NotificationCompat.Builder extend(NotificationCompat.Builder builder) {
builder.setColor(new BigInteger("FF00FF00", 16).intValue()); // Set notification color to green
return builder;
}
});
}
}
import android.util.Log
import androidx.core.app.NotificationCompat
import com.cleverpush.NotificationReceivedEvent
import com.cleverpush.NotificationServiceExtension
import java.math.BigInteger
class MyNotificationServiceExtension : NotificationServiceExtension {
override fun onNotificationReceived(event: NotificationReceivedEvent) {
Log.d("CleverPush", "CleverPush MyNotificationServiceExtension onNotificationReceived")
// call `event.preventDefault()` to not display notification
// event.preventDefault()
// To prevent the `default` notification channel creation, use `event.notification.setNotificationChannel()`
/*if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val channelId = "channel_id" // replace with your desired channel id
val channelName = "Channel_Name" // replace with your desired channel name
val importance = NotificationManager.IMPORTANCE_DEFAULT
val channel = NotificationChannel(channelId, channelName, importance)
event.notification.setNotificationChannel(channel)
}*/
// modify notification
event.notification?.setExtender(object : NotificationCompat.Extender {
override fun extend(builder: NotificationCompat.Builder): NotificationCompat.Builder {
builder.setColor(BigInteger("FF00FF00", 16).intValue()) // Set notification color to green
return builder
}
})
}
}
Add this to your AndroidManifest.xml inside the <application>
tag:
<meta-data android:name="com.cleverpush.NotificationServiceExtension"
android:value="com.cleverpush.cleverpush_example_android.MyNotificationServiceExtension" />
Prevent Displaying Notification
It can be used to prevent displaying of notification. (event.preventDefault();
)
public class MyNotificationServiceExtension implements NotificationServiceExtension {
@Override
public void onNotificationReceived(NotificationReceivedEvent event) {
// call `event.preventDefault()` to not display notification
event.preventDefault();
...
}
}
class MyNotificationServiceExtension : NotificationServiceExtension {
override fun onNotificationReceived(event: NotificationReceivedEvent) {
// call `event.preventDefault()` to not display notification
event.preventDefault()
...
}
}
Prevent Default Notification Channel Creation
It can be used to prevent the Default
notification channel creation, use event.getNotification().setNotificationChannel()
public class MyNotificationServiceExtension implements NotificationServiceExtension {
@Override
public void onNotificationReceived(NotificationReceivedEvent event) {
// to prevent the `default` notification channel creation, use `event.getNotification().setNotificationChannel()`
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
NotificationChannel channel;
String channelId = "channel_id"; // replace with your desired channel id
CharSequence channelName = "Channel_Name"; // replace with your desired channel name
int importance = NotificationManager.IMPORTANCE_DEFAULT;
channel = new NotificationChannel(channelId, channelName, importance);
event.getNotification().setNotificationChannel(channel);
}
...
}
}
class MyNotificationServiceExtension : NotificationServiceExtension {
override fun onNotificationReceived(event: NotificationReceivedEvent) {
// To prevent the `default` notification channel creation, use `event.notification.setNotificationChannel()`
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val channelId = "channel_id" // replace with your desired channel id
val channelName = "Channel_Name" // replace with your desired channel name
val importance = NotificationManager.IMPORTANCE_DEFAULT
val channel = NotificationChannel(channelId, channelName, importance)
event.notification.setNotificationChannel(channel)
}
...
}
}
NotificationExtenderService Deprecated Code
- Create a new Class which extends
com.cleverpush.service.NotificationExtenderService
import androidx.core.app.NotificationCompat;
import com.cleverpush.Notification;
import com.cleverpush.service.NotificationExtenderService;
import java.math.BigInteger;
public class MyNotificationExtenderService extends NotificationExtenderService {
@Override
protected boolean onNotificationProcessing(Notification notification) {
// modify notification
notification.setExtender(new NotificationCompat.Extender() {
@Override
public NotificationCompat.Builder extend(NotificationCompat.Builder builder) {
builder.setColor(new BigInteger("FF00FF00", 16).intValue()); // Set notification color to green
return builder;
}
});
// return true to not display notification
return false;
}
}
import androidx.core.app.NotificationCompat
import com.cleverpush.Notification
import com.cleverpush.service.NotificationExtenderService
import java.math.BigInteger
class MyNotificationExtenderService:NotificationExtenderService() {
protected fun onNotificationProcessing(notification:Notification):Boolean {
// modify notification
notification.setExtender(object:NotificationCompat.Extender() {
fun extend(builder:NotificationCompat.Builder):NotificationCompat.Builder {
builder.setColor(BigInteger("FF00FF00", 16).toInt()) // Set notification color to green
return builder
}
})
// return true to not display notification
return false
}
}
You can now modify the Notification with a custom NotificationCompat.Extender.
Also, if you return true
the notification will not be displayed and remains silent.
- Lastly you will need to reference your new service in the
AndroidManifest.xml
under theapplication
tag
<service
android:name=".MyNotificationExtenderService"
android:permission="android.permission.BIND_JOB_SERVICE"
android:exported="false">
<intent-filter>
<action android:name="com.cleverpush.service.NotificationExtender" />
</intent-filter>
</service>
- When you decide to not let the SDK display the notification and instead display it yourself, you will need to call this method when a Notification has been opened. Otherwise clicks will not be tracked anymore:
CleverPush.getInstance(this).trackNotificationClicked(notification.getId());
CleverPush.getInstance(this).trackNotificationClicked(notification.id)