Monday, July 31, 2017

Android push notification banner not showing up in some devices

Leave a Comment

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?

3 Answers

Answers 1

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".

Answers 2

in chinese devices like MI or OPPO ,etc . The system shut downs notification services for non-whitelisted apps .So there is nothing much you can do about it . Simply request them to whitelist your app ( bleak chance of happening in general ) or use a hack of always keeping your app in memory somehow , say by running some blank background service 24*7

Answers 3

First Try with minimum notification property . Try following code which is working fine in my app . If it works than you can debug your code by enabling on by one property of notification .

 private void sendNotification(String messageBody) { Intent intent = new Intent(this, NotificationActivity.class);     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)             .setSmallIcon(R.drawable.ic_action_notification)             .setContentTitle("Touchrooms Notification")             .setContentText(messageBody)             .setAutoCancel(true)             .setSound(defaultSoundUri)             .setContentIntent(pendingIntent);      NotificationManager notificationManager =             (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);      notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());  } 
If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment