0
      private void sendNotif(String title , String text , int id) {
            // Create an explicit intent for an Activity in your app
            Intent intent = new Intent(this, MainActivity.class);
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
            PendingIntent pendingIntent = PendingIntent.getActivity(this, id, intent, PendingIntent.FLAG_UPDATE_CURRENT);
    
            NotificationCompat.Builder builder = new NotificationCompat.Builder(this, "mychannel")
                    .setSmallIcon(android.R.drawable.screen_background_light)
                    .setContentTitle(title)
                    .setContentText(text)
                    .setStyle(new NotificationCompat.BigTextStyle()
                            .bigText(text))
                    // Set the intent that will fire when the user taps the notification
                    .setContentIntent(pendingIntent)
                    .setChannelId("mychannel")
                    .setAutoCancel(true)
                    .setOngoing(false);
    
            NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
            
            // notificationId is a unique int for each notification that you must define
            notificationManager.notify(id , builder.build());
    
    
        }
    
        private void createNotificationChannel() {
            // Create the NotificationChannel, but only on API 26+ because
            // the NotificationChannel class is new and not in the support library
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                CharSequence name = "notif channel";
                String description = "this is the main notif channel";
                int importance = NotificationManager.IMPORTANCE_HIGH;
                NotificationChannel channel = new NotificationChannel("mychannel", name, importance);
                channel.setDescription(description);
                // Register the channel with the system; you can't change the importance
                // or other notification behaviors after this
                NotificationManager notificationManager = getSystemService(NotificationManager.class);
                notificationManager.createNotificationChannel(channel);
            }
}
     

I have two simple methods one to send notifications and other to a create a notification channel Everything seems alright to me, but no notifications are being sent also i would like to mention that the same channel is created twice (i took a look into the app's notifaction settings), although createNotificationChanne() is called once also the docs mentions that :

It's safe to call this repeatedly because creating an existing notification channel performs no operation.

but it did obviously

i am using android 10 and here's my build.gradle file :

apply plugin: 'com.android.application'

    android {
        compileSdkVersion 29
        buildToolsVersion "29.0.1"
        defaultConfig {
            applicationId ""
            minSdkVersion 19
            targetSdkVersion 29
            versionCode 1
            versionName "1.0"
            testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
        }
        buildTypes {
            release {
                minifyEnabled false
                proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
            }
        }
    }
    
    dependencies {
        implementation fileTree(dir: 'libs', include: ['*.jar'])
        implementation 'androidx.appcompat:appcompat:1.2.0'
        implementation 'androidx.constraintlayout:constraintlayout:2.0.1'
        implementation 'com.google.android.material:material:1.2.1'
        testImplementation 'junit:junit:4.12'
        androidTestImplementation 'androidx.test:runner:1.3.0'
        androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
 
        
    
    }

thanks in advance

AmirWG
  • 251
  • 1
  • 2
  • 10

1 Answers1

0

I've tried your code, It works pretty well. I think you might have forgotten to call the createNotificationChannel() before creating the notification.

private void sendNotif(String title , String text , int id) {
     createNotificationChannel()
     // create notification code goes here
}
theapache64
  • 10,926
  • 9
  • 65
  • 108
  • i have just checked , yes i did call the createNotificationChannel() before the sending the notifaction – AmirWG Sep 15 '20 at 03:11
  • i believe it must be a problem with my mobile – AmirWG Sep 15 '20 at 03:12
  • Which device you're using? Is it possible to send a reproducible zip file? Can you try with some other device or emulator? – theapache64 Sep 15 '20 at 03:12
  • i am using SM-A515U1 – AmirWG Sep 15 '20 at 03:14
  • i have no idea what a reproducible zip file is – AmirWG Sep 15 '20 at 03:14
  • @AmirWG Can you pls create a brand new project in android studio, add your notification logic inside the activity, run it if it works with your device, if not, zip the project directory and then share it. Can you try with the emulator too ? or do you have any other physical device ? can you try uninstall the app and try again ? – theapache64 Sep 15 '20 at 03:17
  • yes i do have other physical devices , it works just fine on my galaxy s4 , i have not tired with an emulator but i will give it a try – AmirWG Sep 15 '20 at 03:19
  • Alright. It might be some device specific notification settings that messed this up. – theapache64 Sep 15 '20 at 03:21