Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Push notification not work for both Nougat & Oreo into same time

i have 2 function that's Push Notification.

For Nougat (7.0) (not work higher than 24)

  Uri SoundUri = RingtoneManager.getDefaultUri((RingtoneManager.TYPE_NOTIFICATION));


                    Notification notification = new Notification.Builder(first.this)
                            .setVibrate(new long[] { 350, 350})
                            .setSmallIcon(R.drawable.testicon)
                            .setContentTitle("test tittle")
                            .setContentText("test content")
                            .setSound(SoundUri).build();

                    NotificationManager notifmanager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
                    notifmanager.notify(0,notification);

For Oreo or higher + (8.0) (not work for Nougat api 24)

 CharSequence name = getString(R.string.channel_name);// The user-visible name of the channel.
        int importance = NotificationManager.IMPORTANCE_HIGH;

        String CHANNEL_ID = "my_channel_01";// The id of the channel.
        NotificationChannel mChannel = new NotificationChannel(CHANNEL_ID, name, importance);
        int notifyID = 1;

        Notification notification = new NotificationCompat.Builder(this)
                .setVibrate(new long[] { 350, 350})
                .setSmallIcon(R.drawable.testicon)
                .setContentTitle("test tittle")
                .setChannelId(CHANNEL_ID).build();

        NotificationManager mNotificationManager =
                (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

        mNotificationManager.createNotificationChannel(mChannel);

        mNotificationManager.notify(notifyID , notification);

now i use Gradle

android {
compileSdkVersion 29
buildToolsVersion "29.0.2"
defaultConfig {
    applicationId "com.test.testttt"
    minSdkVersion 17
    targetSdkVersion 29
    versionCode 1
    versionName "1.0"
    testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}

is there Push Notification (method / function) that accepted by almost all android version's (so i wanna make it accepted atleast api +17)

like image 443
James.Alocard Avatar asked Nov 24 '25 13:11

James.Alocard


1 Answers

Put check if OS version is Oreo or greater then use channel otherwise do not create channel and set its id on notification. Channel is needed for Oreo and above with notification.

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
    notification.setChannelId(CHANNEL_ID);
}
like image 146
Saif Avatar answered Nov 27 '25 04:11

Saif



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!