Push Notification issue in android marshmallow and lollipop - android

I tried push notification its working fine in android JellyBean but same code not working marshmallow and lollipop.
Device getting other app notification like whatsapp,gmail.. cant say problem with settings also.
googled it some icon changes new versions but am not getting notification also, thanks guys :)
My Notification Code:
NotificationIntent = new Intent(this, Activity.class);
NotificationIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
NotificationPendingIntent = PendingIntent.getActivity(this, 0, NotificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationBuilder = new Notification.Builder(this)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("GREEK NEWS")
.setContentText(content)
.setDefaults(Notification.DEFAULT_ALL)
.setAutoCancel(true)
.setContentIntent(NotificationPendingIntent);
NotificationManager = (android.app.NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
NotificationManager.notify(86, NotificationBuilder.build());
} else {
NotificationManager.notify(86, NotificationBuilder.getNotification());
}

Try change icon of nofitication
.setSmallIcon(getNotificationIcon())
and create method
private int getNotificationIcon() {
boolean useWhiteIcon = (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP);
return useWhiteIcon ? R.drawable.notification_icon : R.mipmap.ic_launcher;
}
Hope. It help you !!!

try to use following method. I checked for version if lollipop or > then lollipop,i used vector based icon and also used NotificationCompat.Builder to generate notification.Tested in all versions.Hope this will help you.
public void simpleNotification(Context context,String message)
{
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
Intent intent = new Intent(context, SplashActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
//intent.putExtra("isFromBadge", false);
PendingIntent pIntent = PendingIntent.getActivity(context, 1, intent, PendingIntent.FLAG_UPDATE_CURRENT);
Bitmap bmp = BitmapFactory.decodeResource(context.getResources(), R.drawable.app_icon);
boolean whiteIcon = (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context)
.setContentText(message)
.setStyle(new NotificationCompat.BigTextStyle().bigText(message))
.setAutoCancel(true)
.setLights(Color.RED, 100, 1900)
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
.setDefaults(Notification.DEFAULT_SOUND);
mBuilder.setContentTitle(context.getResources().getString(R.string.app_name));
mBuilder.setSmallIcon(whiteIcon ? R.drawable.ic_notification : R.drawable.app_icon);
if(whiteIcon)
mBuilder.setColor(ContextCompat.getColor(context,R.color.colorPrimary));
//mBuilder.setColor(context.getResources().getColor(R.color.colorPrimary));
mBuilder.setContentIntent(pIntent);
Notification notification = mBuilder.build();
// hide the notification after its selected
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notificationManager.notify(1, notification);
}

Related

Notification auto disappear after showing

public class AlarmReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationManager notificationManager = (NotificationManager) context.getSystemService(NOTIFICATION_SERVICE);
intent = new Intent(context, AlertDialog.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
intent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND); // added new line
intent.putExtra("STARTED_BY_RECEIVER", true);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 100, intent, PendingIntent.FLAG_UPDATE_CURRENT);
String CHANNEL_ID = "channel id";// The id of the channel.
CharSequence name = "Channel";
int importance = NotificationManager.IMPORTANCE_HIGH;
NotificationChannel mChannel = new NotificationChannel(CHANNEL_ID, name, importance);
Notification notification = new Notification.Builder(context)
.setContentIntent(pendingIntent)
.setContentText("Some Text")
.setContentTitle("Notification")
.setAutoCancel(true)
.setOngoing(true)
.setSmallIcon(R.drawable.ic_notification_praying)
.setChannelId(CHANNEL_ID)
.build();
notificationManager.createNotificationChannel(mChannel);
notificationManager.notify(100 , notification);
} else {
NotificationManager notificationManager = (NotificationManager) context.getSystemService(NOTIFICATION_SERVICE);
intent = new Intent(context, AlertDialog.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
intent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND); // added new line
intent.putExtra("STARTED_BY_RECEIVER", true);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 100, intent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder builder = (NotificationCompat.Builder) new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.ic_notification_praying)
.setContentIntent(pendingIntent)
.setContentText("Some Text")
.setContentTitle("Notification")
.setAutoCancel(true)
.setOngoing(true);
Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
builder.setSound(alarmSound);
notificationManager.notify(100, builder.build());
}
}
I've made an application which show notification on time that user chose in main activity and Notification should disappear only when user tap on notification otherwise not..and it was working fine unless I've updated my phone to android Oreo (8.0) now notification show at time but auto disappear after sometime.. How to make notification not auto disappear (in android Oreo) but only when user tap on it?
alright I figure this out.. it was battery optimize option which was killing my application so that's why notification was disappearing.. I removed my application from auto optimize now it's working fine.

Cancel is not working in Activity to cancel a particular Push notification android

Here I'm showing a push notifications in android fromService class which is showing fine and getting dismiss if we tap on complete Remoteview but when I'm opening an Activity from buttons/texts of RemoteView, notifications don't get cancelled.
In Service:
Intent notificationRecordFollowupintent = new Intent(this, RecordFollowUpActivity.class);
notificationRecordFollowupintent.putExtra("notify_id", m);
PendingIntent contentRecordFollowupIntent = PendingIntent.getActivity(this, m, notificationRecordFollowupintent, PendingIntent.FLAG_UPDATE_CURRENT);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
builder.setSmallIcon(R.mipmap.ic_launcher)
.setWhen(System.currentTimeMillis())
.setDefaults(NotificationCompat.DEFAULT_SOUND)
.setStyle(bigTextStyle)
.setCustomBigContentView(contentView)
.setCustomContentView(contentView)
.setGroup("Follow Up Alert")
.setNumber(++numMessages)
.setOngoing(true)
.setAutoCancel(true)
.setContentIntent(contentRecordFollowupIntent);
} else {
builder.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("Follow Up Alert")
.setContentText("" + name)
.setWhen(System.currentTimeMillis())
.setDefaults(NotificationCompat.DEFAULT_SOUND)
.setStyle(bigTextStyle)
.setCustomContentView(contentView)
.setCustomBigContentView(contentView)
.setContentIntent(contentRecordFollowupIntent)
.setAutoCancel(true);
}
Notification notification = builder.build();
notification.flags |= Notification.FLAG_AUTO_CANCEL;
nm.notify(getResources().getString(R.string.app_name), m, notification);
In Activity:
Intent intent = getIntent();
int notificationId = intent.getIntExtra("notify_id", -1);
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.cancel(notificationId);
I'm getting same Id also still it doesn't get cancelled.
NotificationManager.cancelAll() to remove all notification. NotificationManager.cancel(notificationId) to remove particular notification.

How to show notification in front rather than on statusbar with notification icon when app is in foreground?

My code is as shown below:
private void sendNotification(String messageBody) {
Intent intent = new Intent(this, OrderHistory.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.putExtra(Consts.NOTIFICATION_ORDER_ID, notificationMap.get(Consts.NOTIFICATION_ORDER_ID));
intent.putExtra(Consts.NOTIFICATION_ORDER_STATUS,
notificationMap.get(Consts.NOTIFICATION_ORDER_STATUS));
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent,
PendingIntent.FLAG_ONE_SHOT);
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("QuFlip")
.setContentText(messageBody)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, notificationBuilder.build());
}
Now, whenever the notification comes,it shows android bydefault app icon in statusbar. But what I want to do is, I want to show proper notification, just like when the app is not running.
Try setLargeIcon(R.mipmap.ic_launcher).
If it's won't help, try to add:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
notificationBuilder.setPriority(Notification.PRIORITY_HIGH);
}
Set the PRIORITY_HIGH and DEFAULT_SOUND flags:
notification.setPriority(Notification.PRIORITY_HIGH);
notification.setDefaults(NotificationCompat.DEFAULT_SOUND);
It also works if you set DEFAULT_VIBRATE but that's more intrusive.

Notification auto cancel not working for Android lollipop

I want to auto cancel my notification when user clicks on notification. The following code works good in all the devices except Android lollipop device. In Lollipop device, notification goes only when user swipes it off.
#SuppressWarnings("deprecation")
public void sendNotification(int id){
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("Test")
.setContentText("Jump to next screen")
.setDefaults(Notification.DEFAULT_SOUND)
.setAutoCancel(true);
mBuilder.getNotification().flags |= Notification.FLAG_AUTO_CANCEL | Notification.FLAG_ONLY_ALERT_ONCE;
// Creates an explicit intent for an Activity in your app
Intent resultIntent = new Intent(this, NextActivity.class);
resultIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK|Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent resultPendingIntent = PendingIntent.getActivity(getApplicationContext(), 0,
resultIntent, 0);
//PendingIntent.FLAG_UPDATE_CURRENT
mBuilder.setContentIntent(resultPendingIntent);
NotificationManager mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// id, if there is a need to update the notification later on.
mNotificationManager.notify(id, mBuilder.build());
Log.v(TAG, "Notification ID value " + id);
//mNotificationManager.cancel(id);
}
What is missing in this code?
Looks good to me. My auto cancel still works on 5.0.2. Let me give you a portion of my code:
public static void notif(int id, String title, String text, Context context, Class activity) {
// get an instance of the NotificationManager service
if (mNotifyMgr == null)
mNotifyMgr = (NotificationManager) context.getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);
Intent intent = new Intent(context, activity);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
PendingIntent notifIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(context)
.setSmallIcon(R.mipmap.launcher)
.setContentTitle(title)
.setContentText(text)
.setContentIntent(notifIntent);
mNotifyMgr.notify(id, mBuilder.build());
}
modify following:
setAutoCancel(true) -> setAutoCancel(false);
then on action activity do the following
NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.cancelAll();
if you want to set special condition you can set by
intent.putExtra("key", "value")

Can't display more text in a notification

I am trying to show a notification in the title bar with a long text.
PendingIntent contentIntent = PendingIntent.getActivity(context,
NOTIFICATION_ID, notificationIntent,
PendingIntent.FLAG_ONE_SHOT);
NotificationManager nm = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
NotificationCompat.Builder builder = new NotificationCompat.Builder(
context);
builder.setContentIntent(contentIntent)
.setSmallIcon(R.drawable.icon_push).setTicker(alert)
.setContentTitle(title).setContentText(alert)
.setWhen(System.currentTimeMillis()).setAutoCancel(true)
.setDefaults(Notification.DEFAULT_ALL)
.setSound(Settings.System.DEFAULT_NOTIFICATION_URI);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
builder.setStyle(new NotificationCompat.BigTextStyle()
.bigText(title));
}
Notification n = builder.build();
nm.notify(id, n);
But the builder.setStyle(new NotificationCompat.BigTextStyle()
.bigText(title));
The setStyle seems to do nothing, I am testing it on android 4.1
You should remove this:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
because this is compatible, it's automatically set right.
And this is 100% working code:
NotificationManager notificationManager = (NotificationManager) this
.getSystemService(Context.NOTIFICATION_SERVICE);
NotificationCompat.Builder builder = new NotificationCompat.Builder(
this);
builder.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("titletitletitletitletitletitletitletitletitletitletitletitle").setContentText("contentcontentcontentcontentcontentcontentcontent")
.setWhen(System.currentTimeMillis()).setAutoCancel(true)
.setDefaults(Notification.DEFAULT_ALL)
.setSound(Settings.System.DEFAULT_NOTIFICATION_URI)
.setStyle(new NotificationCompat.BigTextStyle()
.bigText("bigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbig"));
Notification notification = builder.build();
notificationManager.notify(1, notification);
If your variable names are correct and your variable named title is only the title then your problem is that you are using bigText(title) instead of bigText(aReallyBigText);
use:Notification.Builder(context).setFullScreenIntent(pendingIntent, true),
manual to make the notification full screen

Categories

Resources