I am using this code for notification. its work on all version but in lollipop it show the white icon in place of my notification icon.
private void sendNotification(String msg) {
//Notification notifyObj = null;
mNotificationManager = (NotificationManager)
this.getSystemService(Context.NOTIFICATION_SERVICE);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
new Intent(this, MenuActivity.class), Intent.FLAG_ACTIVITY_NEW_TASK);
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.appicon)
.setContentTitle("New Notification")// Vibration
.setVibrate(new long[] { 1000, 1000, 1000, 1000, 1000 })
.setLights(Color.RED, 3000, 3000)
.setWhen(System.currentTimeMillis())
.setStyle(new NotificationCompat.BigTextStyle()
.bigText(msg))
.setContentText(msg);
mBuilder.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION));
mBuilder.setContentIntent(contentIntent);
mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
}
You need to check SDK version before setting small icon.
Instead of
.setSmallIcon(R.drawable.appicon)
use
.setSmallIcon(CallNotiIcon())
and inside CallNotiIcon method, use this
private int CallNotiIcon() {
boolean Icon = Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP);
return Icon ? R.drawable.appicon : R.drawable.<NEW ICON FOR LOLIPOP>;
}
Make sure you check http://developer.android.com/design/style/iconography.html
Related
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);
}
I try to find this one in this forum and I found a couple but no one worked for me. I just need to hide it in the notification panel where appears next to the large icon but if I hide it, it also dissapears from the notification bar.
Is there any way to show only the large icon as is happening in the first and fourth notifications?
This is the code that I am using:
mNotificationManager = (NotificationManager)
this.getSystemService(Context.NOTIFICATION_SERVICE);
Intent intent;
Notification.Builder mBuilder =
new Notification.Builder(this)
.setSmallIcon(R.mipmap.small_icon).setTicker(getApplicationContext().getResources().getString(R.string.app_name))
.setLargeIcon(largeIcon)
.setAutoCancel(true)
.setVibrate(new long[]{1000, 1000, 1000, 1000, 1000})
.setLights(Color.GREEN, 1000, 1000)
.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
.setContentTitle(extras.getString("title"))
.setStyle(new Notification.BigTextStyle().bigText(extras.getString("message")))
.setContentText(extras.getString("message"));
edit.putString(Config.FRAGMENT, extras.getString("fragment"));
edit.commit();
intent = new Intent(this, NavigationActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
intent, 0);
mBuilder.setContentIntent(contentIntent);
mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
You can make it invisible after building the notification :
int smallIconId = context.getResources().getIdentifier("right_icon", "id", android.R.class.getPackage().getName());
if (smallIconId != 0) {
notification.contentView.setViewVisibility(smallIconId, View.INVISIBLE);
notification.bigContentView.setViewVisibility(smallIconId, View.INVISIBLE);
}
working for me.
try it
Notification.Builder builder=new Notification.Builder(this);
builder.setContentIntent(intent)
.setSmallIcon(R.drawable.ic_notification).setTicker(context.getResources().getString(R.string.app_name))
.setContentTitle(context.getResources().getString(R.string.app_name))
.setContentText(message);
Notification notification = builder.build();
When I add .setDefaults(DEFAULT_SOUND) or DEFAULT_LIGHTS or DEFAULT_ALL, it shows the heads up notification, but doesnt show the light set with .setLights(0x0FF00FF0, 300, 100). If I remove the .setDefaults it doesn't show the heads up, but shows the lights.
Intent notificationIntent = new Intent(Intent.ACTION_VIEW);
notificationIntent.setData(Uri.parse("http://www.wgn.com"));
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
Notification notification = new NotificationCompat.Builder(this)
.setCategory(Notification.CATEGORY_MESSAGE)
.setContentTitle("Yi Warning")
.setContentText("Battery is below 20%")
.setSmallIcon(R.drawable.ic_launcher)
.setAutoCancel(true)
.setVisibility(1)
.setContentIntent(contentIntent)
.setPriority(Notification.PRIORITY_MAX)
.setDefaults(Notification.DEFAULT_SOUND)
.setLights(0x0FF00FF00, 300, 100).build();
NotificationManager notificationManager =
(NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationManager.notify(NOTIFICATION_ID, notification);
Removed
.setDefaults(Notification.DEFAULT_SOUND)
and added
.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
Now everything is working great :D
I'm working on implementation of Pomodoro app for Android Wear.
I want to make similar to standard timer UI/UX, I guess it's implemented using displaying/updating notification with timer as title, so I'm showing notification and updating it periodically from Service:
private void updateNotification() {
Intent stopActionIntent = new Intent(this, PomodoroActivity.class);
stopActionIntent.setAction(PomodoroActivity.ACTION_STOP);
PendingIntent stopActionPendingIntent = PendingIntent.getActivity(this, 0, stopActionIntent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Action stopAction = new NotificationCompat.Action.Builder(
R.drawable.ic_stop,
getString(R.string.stop_pomodoro),
stopActionPendingIntent)
.build();
Notification notification = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher)
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.bg_pomodoro_timer))
.setContentTitle(convertDiffToTimer(System.currentTimeMillis(), timerDeadlineMs))
.setPriority(Notification.PRIORITY_MAX)
.setOngoing(true)
.extend(new NotificationCompat.WearableExtender().addAction(stopAction))
.build();
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
notificationManager.notify(1, notification);
}
Unpleasure problem with such solution is blinking of notificaiton.
Any ideas how escape blinking? Or maybe other way to achieve target behaviour?
To update ongoing/existing notification -
Use Same Id of Notification in Builder
Use .setOnlyAlertOnce(true)
NotificationCompat.Builder notificationBuilder;
public void generateNotificationForTimer(String timeInString, boolean isFirstTime) {
if (isFirstTime) {
notificationBuilder = new NotificationCompat.Builder(this)
.setStyle(new NotificationCompat.BigPictureStyle())
.setOnlyAlertOnce(true)
.setContentTitle("Timer Notification Demo")
.setContentText("Time - " + timeInString)
.setSmallIcon(R.drawable.common_signin_btn_icon_dark);
NotificationManagerCompat.from(this).notify(110, notificationBuilder.build());
} else {
notificationBuilder.setContentText(timeInString);
NotificationManagerCompat.from(this).notify(110, notificationBuilder.build());
}
}
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