Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Notification is not showing in Android 9

I am using this code to show notification in my android Application. This is working fine in all android version but no notification is showing in Android 9.

I tried to implement this with different method but nothing worked.

    public void showNotification(String heading, String description, String imageUrl, Intent intent){
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    createChannel();
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
            PendingIntent.FLAG_ONE_SHOT);
    Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this,"channelID")
            .setSmallIcon(R.drawable.logo)
            .setContentTitle(heading)
            .setContentText(description)
            .setAutoCancel(true)
            .setSound(defaultSoundUri)
            .setContentIntent(pendingIntent);

    NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    int notificationId = (int) ((new Date().getTime() / 1000L) % Integer.MAX_VALUE);
    notificationManager.notify(notificationId, notificationBuilder.build());
}

public void createChannel(){
    if (Build.VERSION.SDK_INT < 26) {
        return;
    }
    NotificationManager notificationManager =
            (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
    NotificationChannel channel = new NotificationChannel("channelID","name", NotificationManager.IMPORTANCE_DEFAULT);
    channel.setDescription("Description");
    notificationManager.createNotificationChannel(channel);
}

Thanks..

like image 919
Puneet Kumar Avatar asked Oct 16 '18 04:10

Puneet Kumar


People also ask

Why are my notifications not showing up on Android?

Cause of Notifications Not Showing up on AndroidDo Not Disturb or Airplane Mode is on. Either system or app notifications are disabled. Power or data settings are preventing apps from retrieving notification alerts. Outdated apps or OS software can cause apps to freeze or crash and not deliver notifications.

How do I find old notifications on Android 9?

Enable and check notification history on AndroidSwipe up from the homescreen to open the app drawer menu. Find and open the Settings app (it looks like a gear icon). Open Notifications. Select Notification history.

How do I fix notification not available?

Pick “Apps & Notifications”. Then “Advanced”. Then Special app access. Now pick “Android” and turn it on.


1 Answers

Hi this is very Late Answer but I still want to mark the mistake in the code as well as the working code snippet so that It can help others.

you are having two instance of notificationManager on in the showNotification(..) function and another in the createChannel() function. You must create channel in the same instance of the notificationManager so your working code will be like:

  public void showNotification(String heading, String description, String imageUrl, Intent intent){
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
                PendingIntent.FLAG_ONE_SHOT);
        Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this,"channelID")
                .setSmallIcon(R.drawable.ic_launcher_background)
                .setContentTitle(heading)
                .setContentText(description)
                .setAutoCancel(true)
                .setSound(defaultSoundUri)
                .setContentIntent(pendingIntent);

        NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        int notificationId = 1;
        createChannel(notificationManager);
        notificationManager.notify(notificationId, notificationBuilder.build());
    }

    public void createChannel(NotificationManager notificationManager){
        if (Build.VERSION.SDK_INT < 26) {
            return;
        }
        NotificationChannel channel = new NotificationChannel("channelID","name", NotificationManager.IMPORTANCE_DEFAULT);
        channel.setDescription("Description");
        notificationManager.createNotificationChannel(channel);
    }

Call Like:

showNotification("Heading","Description","",new Intent(this,MainActivity.class));

The Results: enter image description here

like image 112
Jawad Zeb Avatar answered Oct 17 '22 01:10

Jawad Zeb