Can't display more text in a notification - android

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

Related

Android - notification does not appear in status bar

When starting an IntentService to upload a file in the background, I want to show a notification to the user that the upload is in progress by calling showNotification() from inside my service:
private void showNotification() {
Notification notification = new Notification.Builder(this)
.setSmallIcon(R.drawable.ic_cloud_upload_black_24dp)
.setWhen(System.currentTimeMillis())
.setContentTitle("Uploading")
.setContentText("Your upload is in progress.")
.setOngoing(true)
.build();
mNotificationManager.notify(NOTIFICATION_ID, notification);
}
Now here's my problem: the notification appears on the lock screen, but not in the status bar when the screen is unlocked. What am I missing?
Deployment target is API level 24, so a missing NotificationChannel should not be the cause.
try it :
PendingIntent pendingIntent = PendingIntent.getActivity(getmContext(), 0, new Intent(getmContext(), MainActivity.class), 0);
android.app.Notification pp = new NotificationCompat.Builder(getmContext())
.setSmallIcon(R.drawable.ic_launcher_background)
.setContentText(getMessage())
.setContentIntent(pendingIntent)
.build();
NotificationManager notificationManager = (NotificationManager) getmContext().getSystemService(NOTIFICATION_SERVICE);
notificationManager.notify(0, pp);
This is my code which is working fine with target SDK 25
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
mContext);
NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle();
inboxStyle.addLine(message);
Notification notification;
notification = mBuilder.setSmallIcon(icon).setTicker(title).setWhen(0)
.setAutoCancel(true)
.setContentTitle(title)
.setContentIntent(resultPendingIntent)
.setStyle(inboxStyle)
.setSmallIcon(R.mipmap.ic_launcher)
.setLargeIcon(BitmapFactory.decodeResource(mContext.getResources(), icon))
.setContentText(message)
.build();
NotificationManager notificationManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(NOTIFICATION_ID, notification);
where NOTIFICATION_ID
public static final int NOTIFICATION_ID = 100;
I feel so dumb. Turns out the notification was displayed all the time, but with a black icon on black background.
Changed the icon colour in the xml from
android:fillColor="#FF000000"
to
android:fillColor="#FFFFFFFF"
and it works like a charm

Trying to use notification inboxStyle to show more lines similar to GMAIL app in notification but it doesn't work

This is the code that I have:
NotificationManager notifManager = (NotificationManager) getActivity().getSystemService(Context.NOTIFICATION_SERVICE);
Notification.InboxStyle inboxStyle = new Notification.InboxStyle();
inboxStyle.setBigContentTitle("Title - Notification");
inboxStyle.setSummaryText("TEST");
inboxStyle.addLine("LINE");
Notification noti = new Notification.Builder(getActivity())
.setContentTitle("PSNGR")
.setContentText("PITSTOP BLA").setSmallIcon(R.drawable.notification_icon)
.setStyle(inboxStyle)
.setContentIntent(null).build();
noti.flags |= Notification.FLAG_AUTO_CANCEL;
notifManager.notify(0, noti);
But nothing from the InboxStyle notification works. I only get PSNGR and PITSTOP BLA, why is that?
PS: Also tried with BitTextStyle() but also nothing is seen:
For this code:
NotificationManager notifManager = (NotificationManager) getActivity().getSystemService(Context.NOTIFICATION_SERVICE);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(getActivity())
.setContentTitle("PSNGR")
.setSmallIcon(R.drawable.ic_launcher)
.setStyle(new NotificationCompat.BigTextStyle().bigText("MATA"))
.setStyle(new NotificationCompat.BigTextStyle().bigText("MATA2"))
.setAutoCancel(true)
.setContentIntent(null);
notifManager.notify(0, notificationBuilder.build());
I only see: "PSNGR"
I used RemoteViews instead of the normal notification:
NotificationManager notifManager = (NotificationManager) getActivity().getSystemService(Context.NOTIFICATION_SERVICE);
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(getActivity()).setSmallIcon(R.drawable.ic_launcher);
RemoteViews mContentView = new RemoteViews(getActivity().getPackageName(), R.layout.pitstop_notification);
mContentView.setImageViewResource(R.id.image, R.drawable.notification_icon);
mContentView.setTextViewText(R.id.title, "Custom notification");
mContentView.setTextViewText(R.id.text, "This is a custom layout");
mContentView.setTextViewText(R.id.text2, "This is a custom layout2");
mBuilder.setContent(mContentView);
notifManager.notify(0, mBuilder.build());

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.

BigContentView does not seem to be updated

I am currently working on an android app. I need to notify a user that they have arrived at a place in the normal notification and when the notification is expanded it shows extra info. The usual :)
I am using 2 different remoteViews, one to set the contentView and one to set the bigContentView.
NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
// build notification
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(context)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentIntent(notificationPendingIntent)
.setColor(Color.CYAN)
.setPriority(NotificationCompat.PRIORITY_MAX)
.setAutoCancel(true)
.setContentTitle("Content Title")
.setContentText("Content Text");
int NOTIFICATION_ID = 1;
RemoteViews rv = new RemoteViews(context.getPackageName(), R.layout.remoteview_notification);
rv.setImageViewResource(R.id.remoteview_notification_icon, R.mipmap.ic_launcher);
rv.setTextViewText(R.id.remoteview_notification_headline, "Headline");
rv.setTextViewText(R.id.remoteview_notification_short_message, notificationContent.getHeading());
Notification notification = mBuilder.build();
if (android.os.Build.VERSION.SDK_INT >= 16) {
notification.contentView = rv; //Alternative to .setContent(rv);
}
mNotificationManager.notify(NOTIFICATION_ID, notification);
RemoteViews rvBig = new RemoteViews(context.getPackageName(), R.layout.remoteview_big_notification);
rvBig.setImageViewResource(R.id.remoteview_big_notification_icon, R.mipmap.ic_launcher);
rvBig.setTextViewText(R.id.remoteview_big_notification_headline, notificationContent.getHeading());
rvBig.setTextViewText(R.id.remoteview_big_notification_full_message, notificationContent.getBodyText());
if (android.os.Build.VERSION.SDK_INT >= 16) {
notification.bigContentView = rvBig;
}
mNotificationManager.notify(NOTIFICATION_ID, notification);
The problem is my expanded notification does not show up when I drag down? Any help will be appreciated. It feels like it is just something small that is not yet set/ set correctly?
I believe that my intentId might not be unique throughout my app. I have seen people use the current system to ensure uniqueness.
NOTIFICATION_ID = system.currenttimemillis();
PendingIntent pendingIntent = PendingIntent.getActivity(this, NOTIFICATION_ID, intent, PendingIntent.FLAG_UPDATE_CURRENT);
notificationManager.notify(NOTIFICATION_ID, builder.build());

How to set notification icon on android lollipop version

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

Categories

Resources