This guide is for the v4 Android SDK. We recommend upgrading to our latest v5 User Model SDKs. See Mobile SDKs API Migration Guides to upgrade the OneSignal SDK.
Add the below Notification Extension Code if you want to do one of the following:
  • Receive data in the background with or without displaying a notification.
  • Override specific notification settings depending on client side app logic such as custom accent color, vibration pattern, or other any other NotificationCompat options available. See Android’s documentation on the NotificationCompat options.
🚧 Requires writing Native Android code & Upgraded SDK.Must be using OneSignal SDK Versions:
  • Android 4.0.0 - 4.9.9
  • React Native 4.0.0 - 4.9.9
  • Flutter 3.0.0 - 3.9.9
  • Cordova/Ionic 3.0.0 - 3.9.9
  • Unity 3.0.0 - 3.9.9
  • Xamarin 4.0.0 - 4.9.9

Step 1. Create a class for the Service Extension

Create a class that extends OSRemoteNotificationReceivedHandler and implement the remoteNotificationReceived method. The method remoteNotificationReceived parameters are context of type Context and notificationReceivedEvent of type OSNotificationReceivedEvent.
package your.package.name
  
import android.content.Context;
import android.util.Log;
import org.json.JSONObject;

import com.onesignal.OSNotification;
import com.onesignal.OSMutableNotification;
import com.onesignal.OSNotificationReceivedEvent;
import com.onesignal.OneSignal.OSRemoteNotificationReceivedHandler;

@SuppressWarnings("unused")
public class NotificationServiceExtension implements OSRemoteNotificationReceivedHandler {

    @Override
    public void remoteNotificationReceived(Context context, OSNotificationReceivedEvent notificationReceivedEvent) {
        OSNotification notification = notificationReceivedEvent.getNotification();

        // Example of modifying the notification's accent color
        OSMutableNotification mutableNotification = notification.mutableCopy();
        mutableNotification.setExtender(builder -> {
            // Sets the accent color to Green on Android 5+ devices.
            // Accent color controls icon and action buttons on Android 5+. Accent color does not change app title on Android 10+
            builder.setColor(new BigInteger("FF00FF00", 16).intValue());
            // Sets the notification Title to Red
            Spannable spannableTitle = new SpannableString(notification.getTitle());
            spannableTitle.setSpan(new ForegroundColorSpan(Color.RED),0,notification.getTitle().length(),0);
            builder.setContentTitle(spannableTitle);
            // Sets the notification Body to Blue
            Spannable spannableBody = new SpannableString(notification.getBody());
            spannableBody.setSpan(new ForegroundColorSpan(Color.BLUE),0,notification.getBody().length(),0);
            builder.setContentText(spannableBody);
            //Force remove push from Notification Center after 30 seconds
            builder.setTimeoutAfter(30000);
            return builder;
        });
        JSONObject data = notification.getAdditionalData();
        Log.i("OneSignalExample", "Received Notification Data: " + data);

        // If complete isn't called within a time period of 25 seconds, OneSignal internal logic will show the original notification
        // To omit displaying a notification, pass `null` to complete()
        // This will also prevent the notification received (confirmed delivery) event and click events on the push notification if handled differently.
        notificationReceivedEvent.complete(mutableNotification);
    }
}

Step 2. Add the following to your AndroidManifest.xml.

Add OneSignal class name and your class value as meta-data within the AndroidManifest.xml file under the application tag. Ignore any β€œunused” warnings.
<application ...> 
  <!-- name doesn't change, value = your class fully name spaced-->
  <meta-data android:name="com.onesignal.NotificationServiceExtension"
     android:value="com.onesignal.example.NotificationServiceExtension" />
</application>