Search code examples
androidandroid-studioandroid-appcompatandroidxonesignal

Facing problem with OneSignal Notification Extender Service NotificationCompat after migrating to androidX


I'm using OneSignal Notification service to send a notification. As per documentation, I'm doing setup it worked well before for lower Android SDK. After migrating to androidX it's showing incompatible types for NotificationCompat. I didn't find a proper solution to this problem. Please comment on your answer. Find the Code below

MyNotificationExtenderService.java

package ak.wp.meto.notification;

import android.util.Log;
import androidx.core.app.NotificationCompat;

import com.onesignal.NotificationExtenderService;
import com.onesignal.OSNotificationDisplayedResult;
import com.onesignal.OSNotificationReceivedResult;

import java.math.BigInteger;

public class MyNotificationExtenderService extends NotificationExtenderService {
    @Override
    protected boolean onNotificationProcessing(OSNotificationReceivedResult receivedResult) {
        // Read Properties from result
        OverrideSettings overrideSettings = new OverrideSettings();
        overrideSettings.extender = new NotificationCompat.Extender() {
            
            @Override
            public NotificationCompat.Builder extend(NotificationCompat.Builder builder) {
                // Sets the background notification color to Red on Android 5.0+ devices.
                return builder.setColor(new BigInteger("FFFF0000", 16).intValue());
            }
        };

        OSNotificationDisplayedResult displayedResult = displayNotification(overrideSettings);
        Log.d("OneSignalExample", "Notification displayed with id: " + displayedResult.androidNotificationId);

        return true;
    }
}

Solution

  • BINGO!!! I modified my code in this way

    MyNotificationExtenderService.java

    package ak.wp.meto.notification;
    
    import android.app.NotificationManager;
    import android.app.PendingIntent;
    import android.content.ContentValues;
    import android.content.Context;
    import android.content.Intent;
    import android.database.sqlite.SQLiteDatabase;
    import android.media.RingtoneManager;
    import android.net.Uri;
    import androidx.appcompat.app.AppCompatActivity;
    import androidx.core.app.NotificationCompat;
    import com.onesignal.NotificationExtenderService;
    import com.onesignal.OSNotificationReceivedResult;
    import ak.wp.meto.R;
    import ak.wp.meto.activity.MainActivity;
    import ak.wp.meto.activity.PostDetailsActivity;
    import ak.wp.meto.data.constant.AppConstant;
    
    public class MyNotificationExtenderService extends NotificationExtenderService {
        @Override
        protected boolean onNotificationProcessing(OSNotificationReceivedResult receivedResult) {        
            Uri defaultSoundUri = 
            RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
            NotificationCompat.Builder notificationBuilder = new 
                     NotificationCompat.Builder(this)
                    .setSmallIcon(R.drawable.ic_stat_ic_notification)
                    .setContentTitle(title)
                    .setContentText(messageBody)
                    .setAutoCancel(true)
                    .setVibrate(new long[]{1000, 1000})
                    .setSound(defaultSoundUri);
            NotificationManager notificationManager = (NotificationManager) 
            getSystemService(Context.NOTIFICATION_SERVICE);
            notificationManager.notify(0, notificationBuilder.build());
            return false;
        }
    }
    

    This worked for me