Search code examples
androidnotificationssamsung-mobilenotifyxiaomi

Notification in Samsung and Xiaomi show This app hasn't send you any notification


I have a problem with notifications where the notification doesn't appear on my xiaomi and samsung devices but works for the other device. I've tried what people recommend like trying auto start, managing battery settings, but it still doesn't appear on both devices. I also tried to use the notify library but the results were the same. Strangely, when I checked the notification setting and then clicked on the application, it appeared as shown below

samsung

This is my notification code :

public void showNotification() {
    String appname = "App Name";
    String title = "Notification Title";
    String text = "This is the notification text";
    String iconUrl = "http://url.to.image.com/image.png";

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

    TaskStackBuilder stackBuilder = TaskStackBuilder.from(MainActivity.this);
    stackBuilder.addParentStack(NewsActivity.class);
    stackBuilder.addNextIntent(new Intent(MainActivity.this, NewsActivity.class));
    PendingIntent pendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);

    NotificationCompat.Builder builder = new NotificationCompat.Builder(MainActivity.this);
    builder.setContentTitle(title).setContentInfo(appname).setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.logo_wanda_flp)).setContentText(text).setContentIntent(pendingIntent);
    builder.setSmallIcon(R.drawable.logo_wanda_flp);

    notifyManager.notify("textid", 123, builder.getNotification());
}

Solution

  • The code won't work on Android 8 (API 26) and above without first registering a notification channel. On Android before that, it will work just fine. Some of the code snippets below are taken from here.


    Creating a notification channel

    Register notification channels inside your activity before attempting to show any notification. Alternatively, you can register them inside a custom Application class before any activities or services start (but this won't be shown here for the sake of simplicity).

    @Override
    public void onCreate(Bundle savedInstanceState) {
        createNotificationChannel(); // Registering a notification channel
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_activity);
    }
    
    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 = getString(R.string.channel_name);
            String description = getString(R.string.channel_description);
            int importance = NotificationManager.IMPORTANCE_DEFAULT;
            NotificationChannel channel = new NotificationChannel(CHANNEL_ID, 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);
        }
    }
    

    Deliver the notification

    public void showNotification() {
        // ...
        // NotificationCompat.Builder builder = new NotificationCompat.Builder(MainActivity.this);
        // Replace with line below.
        NotificationCompat.Builder builder = new NotificationCompat.Builder(MainActivity.this, CHANNEL_ID);
        // ...
    }