Search code examples
androidonesignal

How to block Onesignal push notifications on Android?


I know I can receive them in Broadcast OnReceive() but I cannot prevent Onesignal for creating a Notification.


Solution

  • You will need to setup a OneSignal NotificationExtenderService class and add it to your AndroidManifest.xml as a Service.

    import com.onesignal.OSNotificationPayload;
    import com.onesignal.NotificationExtenderService;
    
    public class NotificationExtenderBareBonesExample extends NotificationExtenderService {
       @Override
       protected boolean onNotificationProcessing(OSNotificationReceivedResult receivedResult) {
            // Read properties from result.
    
          // Returning true tells the OneSignal SDK you have processed the notification and not to display it's own.
          return true;
       }
    }
    

    AndroidManifest.xml

    <service
       android:name=".NotificationExtenderBareBonesExample"
       android:permission="android.permission.BIND_JOB_SERVICE"
       android:exported="false">
       <intent-filter>
          <action android:name="com.onesignal.NotificationExtender" />
       </intent-filter>
    </service>
    

    See the OneSignal Android Background Data and Notification Overriding documentation for more details.