Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android push notification banner not showing up in some devices

I tried to push notify with NotificationCompat :

NotificationCompat.Builder b = new NotificationCompat.Builder(this);
            b.setAutoCancel(true)
                    .setDefaults(NotificationCompat.DEFAULT_ALL)
                    .setWhen(System.currentTimeMillis())
                    .setSmallIcon(this.getResources().
                        getIdentifier("ic_launcher", "mipmap", this.getPackageName()))
                    .setLargeIcon(BitmapFactory.decodeResource(this.getResources(),
                        this.getResources().
                        getIdentifier("ic_launcher", "mipmap", this.getPackageName())))
                    .setTicker("God Reacts")
                    .setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
                    .setPriority(Notification.PRIORITY_MAX)
                    .setContentTitle(data.get("lineOne"))
                    .setContentText(data.get("lineTwo"))
                    .setContentInfo("Spread the message !");

            PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
                    new Intent(this,getMainActivityClass()),
                    PendingIntent.FLAG_UPDATE_CURRENT);
            b.setContentIntent(contentIntent);
            NotificationManager nm = (NotificationManager) 
            this.getSystemService(Context.NOTIFICATION_SERVICE);
            nm.notify(1, b.build());

But in few devices (Samsung,MI etc) the notification banner is not shown. The notification slides in the action tray with sound and vibrate.

But in few devices it is shown perfectly when the app is closed/background/foreground.The device where it's popping up correctly uses marshmallow.Is is due to specific OS ? Or is it device related issue? What extra I need to add?

like image 815
rohan sethi Avatar asked Jul 21 '17 10:07

rohan sethi


Video Answer


1 Answers

The problem may arise in some devices of nougat version due to Battery optimization.
eg. Samsung Galaxy S8 and S8+, Oneplus, xaiomi etc. but in Android version of nougat, You can try this once, for that you have to check settings once,

step 1: Goto settings >

step 2: search for "Battery optimization" >

step 3: from here, tap on "apps not optimized" and switch to "all apps."

step 4: search for your app (of which you are not getting notification)

step 5: tap on your app name and set it as not optimized so that it can receive notification.

OR

step 1: Go to Settings > Apps

step 2: Click the : Menu in top right, select > [Special Access]

step 3: Select > Optimize Battery Usage >

step 4: Click the dropdown menu towards the top of the screen that says: "Apps not optimized [v]" -> change to [All Apps]

step 5: select your app and change it "Not optimized".

like image 103
Stuti Kasliwal Avatar answered Oct 19 '22 22:10

Stuti Kasliwal