javaandroidandroid-notifications

Unable to send android notification using Notification manager


I am trying to send a notification using the Notification Builder class, but I am getting an error: No channel found.

Here is my code:

Intent intent = new Intent(MainActivity.this,MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
PendingIntent pendingIntent = PendingIntent.getActivity(MainActivity.this, 0, intent, PendingIntent.FLAG_IMMUTABLE);

NotificationCompat.Builder builder = new NotificationCompat.Builder(MainActivity.this, "1")
        .setSmallIcon(R.drawable.ic_launcher_foreground)
        .setContentTitle("Title")
        .setContentText("content")
        .setStyle(new NotificationCompat.BigTextStyle()
                .bigText("Much longer text that cannot fit one line..."))
        .setPriority(NotificationCompat.PRIORITY_DEFAULT)
        .setContentIntent(pendingIntent)
        .setAutoCancel(true);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
    int importance = NotificationManager.IMPORTANCE_DEFAULT;
    String description = "description";
    CharSequence name = getString(R.string.channel_name);
    NotificationChannel channel = new NotificationChannel("1", name, importance);
    channel.setDescription(description);
}


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

notificationManager.notify(1, builder.build());

Logcat error:

1239-3921  NotificationService  system_server  E   No Channel found for pkg=com.example.myapplication, channelId=1, id=1, tag=null, opPkg=com.example.myapplication, callingUid=10263, userId=0, incomingUserId=0, notificationUid=10263, notification=Notification(channel=1 shortcut=null contentView=null vibrate=null sound=null defaults=0x0 flags=0x10 color=0x00000000 vis=PRIVATE semFlags=0x0 semPriority=0 semMissedCount=0)
1239-1396  NotificationService  system_server  E   No Channel found for pkg=com.example.myapplication, channelId=1, id=1, tag=null, opPkg=com.example.myapplication, callingUid=10263, userId=0, incomingUserId=0,

Solution

  • Try this NotificationHelper class and check if you are still getting errors.

    public class NotificationHelper {
    
    private static final String CHANNEL_ID = "notification_channel";
    private static final int NOTIFICATION_ID = 101;
    
    // Method to create the notification
    @RequiresApi(api = Build.VERSION_CODES.TIRAMISU)
    public static void showCreditNotification(Context context) {
        if (context == null){
            return;
        }
        // Create an intent that will launch MainActivity when the notification is clicked
        Intent intent = new Intent(context, SantaHomeActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
    
        // Create a pending intent for the notification click
        PendingIntent pendingIntent = PendingIntent.getActivity(
                context,
                0,
                intent,
                PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE
        );
    
        // Create the notification channel (required for Android O and above)
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            CharSequence name = getString(R.string.channel_name);
            String description = "description";
            int importance = NotificationManager.IMPORTANCE_DEFAULT;
            NotificationChannel channel = new NotificationChannel(CHANNEL_ID, name, importance);
            channel.setDescription(description);
    
            // Register the channel with the system
            NotificationManager notificationManager = context.getSystemService(NotificationManager.class);
            notificationManager.createNotificationChannel(channel);
        }
    
        // Build the notification
        NotificationCompat.Builder builder = new NotificationCompat.Builder(context, CHANNEL_ID)
                .setSmallIcon(R.drawable.icon) // Replace with your app's icon
                .setContentTitle(context.getResources().getString(R.string.notification_title))
                .setContentText(context.getResources().getString(R.string.notification_message))
                .setPriority(NotificationCompat.PRIORITY_MAX)
                .setContentIntent(pendingIntent) // Set the pending intent that opens the app
                .setAutoCancel(true); // Remove notification after click
    
        // Show the notification
        NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context);
        if (ActivityCompat.checkSelfPermission(context, Manifest.permission.POST_NOTIFICATIONS) != PackageManager.PERMISSION_GRANTED) {
            if (ActivityCompat.shouldShowRequestPermissionRationale((Activity) context, Manifest.permission.POST_NOTIFICATIONS)) {
                // Explain to the user why the permission is needed (e.g., using a Snackbar)
            } else {
                ActivityCompat.requestPermissions((Activity) context, new String[]{Manifest.permission.POST_NOTIFICATIONS}, NOTIFICATION_PERMISSION_REQUEST_CODE);
            }
            return;
        }
        notificationManager.notify(NOTIFICATION_ID, builder.build());
    }
    

    }