简体   繁体   中英

notification channel is not sending notifications

      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

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
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM