javaandroidnotifications

Notifications aren't shown on Android 13 despite granted permission


I'm trying to migrate my app that worked on Android 7 to Android 13. I noticed that after the migration the app notifications didn't pop up anymore. Then I read the article: https://developer.android.com/develop/ui/views/notifications/notification-permission and figured out that users have to permit them explicitly. I implemented the permission request/grant, but the notifications still didn't show up. Below are the corresponding excerpts from the app source code.

public class MyActivity extends AppCompatActivity {

    private static final String CHANNEL_ID = "12345";
    public static final int NEWS_NOTIFICATION_ID = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
            ...
            // Prompt user to permit notifications
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
              if (!shouldShowRequestPermissionRationale(String.valueOf(PERMISSION_REQUEST_CODE))){
                getNotificationPermission();
              }
            }

        }

        ...

        public void getNotificationPermission(){
          try {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
                ActivityCompat.requestPermissions(this,
                    new String[]{Manifest.permission.POST_NOTIFICATIONS},
                    PERMISSION_REQUEST_CODE);
            }
          } catch (Exception e) {
            Log.e(TAG, "Unable to request permission", e);
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        if (requestCode == PERMISSION_REQUEST_CODE) {
            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                Log.d("Permission is granted in onRequestPermissionsResult()");
            } else {
                Log.d("Permission is not granted in onRequestPermissionsResult()");
            }
        }

        super.onRequestPermissionsResult(requestCode, permissions, grantResults);

    }


    private void showNotification(String msg) {
        Intent resultIntent = new Intent(getApplicationContext(), MyActivity.class);
        TaskStackBuilder stackBuilder = TaskStackBuilder.create(getApplicationContext());
        stackBuilder.addNextIntentWithParentStack(resultIntent);
        PendingIntent resultPendingIntent=null;
        resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_MUTABLE);

        Notification notification = new NotificationCompat.Builder(getApplicationContext(), CHANNEL_ID)
            .setTicker("ticker")
            .setSmallIcon(R.drawable.app_icon90x90)
            .setContentTitle(msg)
            .setContentText("").setContentIntent(resultPendingIntent)
            .setAutoCancel(true)
            .build();
        NotificationManager notificationManager = (NotificationManager) getApplicationContext().getSystemService(NOTIFICATION_SERVICE);
        notificationManager.notify(NEWS_NOTIFICATION_ID, notification);
    }

    ...
}

When I debug the app the following happens:

The app shows a dialog. I permit the notifications, then go to phone settings and see that the app is in the corresponding list and is enabled. Then I press a button in the app that triggers the method call:

showNotification("Hello world");

The notification doesn't show up and no error is shown.

What could be missing? Are notifications constructed somehow differently in Android 13 or are there some other phone settings that could prevent it from showing up? I can observe the problem on emulator as well as on my phone.


Solution

  • When I tried your code, I saw that it got "No Channel found" error. You should create a channel for notifications starting from API 26 level.

    Here is the edited version of your code:

        public void getNotificationPermission() {
            try {
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                    int importance = NotificationManager.IMPORTANCE_DEFAULT;
                    NotificationChannel channel = new NotificationChannel(CHANNEL_ID, "YOUR_CHANNEL_NAME", importance);
                    channel.setDescription("CHANNEL_DESCRIPTION");
    
                    NotificationManager notificationManager = getSystemService(NotificationManager.class);
                    notificationManager.createNotificationChannel(channel);
                }
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
                    ActivityCompat.requestPermissions(this,
                            new String[]{Manifest.permission.POST_NOTIFICATIONS},
                            PERMISSION_REQUEST_CODE);
                }
            } catch (Exception e) {
                Log.e(TAG, "Unable to request permission", e);
            }
        }
    

    When I tried this code, I saw that the notification was sent successfully.