NotificationCompat.BigTextStyle Content disappears on new notification - android

String ns = Context.NOTIFICATION_SERVICE;
NotificationManager mNotificationManager = (NotificationManager) this.getSystemService(ns);
int icon = R.drawable.ic_launcher;
CharSequence tickerText = title;
long when = System.currentTimeMillis();
Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
if(alarmSound == null){
alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE);
if(alarmSound == null){
alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
}
}
Intent intent = new Intent();
PendingIntent pendingIntent
= PendingIntent.getActivity(this, 0, intent, 0);
NotificationCompat.BigTextStyle bigxtstyle =
new NotificationCompat.BigTextStyle();
bigxtstyle.bigText(text);
bigxtstyle.setBigContentTitle(title);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
.setStyle(bigxtstyle)
.setSmallIcon(icon)
.setAutoCancel(true)
.setSound(alarmSound)
.setDeleteIntent(pendingIntent)
.setContentIntent(PendingIntent.getActivity(this, 0, new Intent(), 0));
Notification noti = mBuilder.build();
mNotificationManager.notify(notificationid++, noti);
This code works, and displays the text as advertised with word wrapping. However, when a subsequent notification happens the previous notification loses its text. Can anyone help resolve this issue? It could be something I'm setting incorrectly, I am new to the android apis.

Android only displays one expanded notification by default. Your notification doesn't lose its content, it just gets compressed and only shows the normal one-line content. But it seems that you don't specify this, therefore the compressed notification is empty.
You may use setContentTitle() and setContentText() to set the one-line texts.

Related

With multiple notifications second notification showing in collapse mode Android

I'm showing multiple notifications in my android application.
When there is single notification it is showing correctly.
But when I generate second notification, second notification showing in collapse by default.
If I manually expand second notification it is showing as:
I want to show a notifications in expanded mode by default.
Here is my code:
int num = (int) System.currentTimeMillis();
PendingIntent pendingIntent = PendingIntent.getActivity(this, num /* Request code */, intent,
PendingIntent.FLAG_ONE_SHOT);
NotificationCompat.BigTextStyle bigTextStyle = new NotificationCompat.BigTextStyle();
bigTextStyle.setBigContentTitle(title);
bigTextStyle.bigText(messageBody);
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.corco)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setStyle(bigTextStyle)
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(num /* ID of notification */, notificationBuilder.build());
Am i missing something? or doing something wrong?
Thanks in advance.
I resolved it. I was not setting any ContentTitle and ContentText for collapse notification.
Here is my updated code:
int num = (int) System.currentTimeMillis();
PendingIntent pendingIntent = PendingIntent.getActivity(this, num /* Request code */, intent,
PendingIntent.FLAG_ONE_SHOT);
NotificationCompat.BigTextStyle bigTextStyle = new NotificationCompat.BigTextStyle();
bigTextStyle.setBigContentTitle(title);
bigTextStyle.bigText(messageBody);
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.corco)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setStyle(bigTextStyle)
.setContentTitle(title)
.setContentText(messageBody)
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(num /* ID of notification */, notificationBuilder.build());

Android Notification Big Picture Style and Big Text Style

I've build a push notification using Big Picture Style as show here.
Is it possible to mix Big picture Style and Big Text Style as shown in the attached photo? How do I do it?
You should be able to do it this way:
Notification notif = new Notification.Builder(context)
.setContentTitle("Title")
.setContentText("content")
.setSmallIcon(R.drawable.ic_small)
.setLargeIcon(bitmap)
.setStyle(new Notification.BigPictureStyle()
.bigPicture(bigBitmap)
.setBigContentTitle("big title"))
.build();
Source
View More... text in your norification is subtext.
So while using bigpicturestyle notification you need to set
bigPicStyle.setSummaryText(mContent);
or
mBuilder.setSubText(mSubText);
Setting both at same time doesn't work.
Example of how to create a BigPictureStyle Notification:
int NOTIFICATION_ID = 1;
String ns = Context.NOTIFICATION_SERVICE;
//Get the bitmap to show in notification bar
Bitmap bitmap_image = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
Bitmap big_bitmap_image = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
NotificationCompat.BigPictureStyle style = new NotificationCompat.BigPictureStyle()
.bigPicture(big_bitmap_image)
.setSummaryText(getResources().getString(R.string.content));
NotificationCompat.Builder nb = new NotificationCompat.Builder(this);
nb.setContentTitle("Notification title"))
.setContentText("Notification Content")
.setSmallIcon(R.drawable.ic_launcher)
.setLargeIcon(bitmap_image)
.setTicker("Notification ticker!")
//API Level min 16 is required
.setStyle(style)
.build();
Intent notificationIntent = new Intent(this, MainActivity2.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
TaskStackBuilder TSB = TaskStackBuilder.create(this);
TSB.addParentStack(MainActivity.class);
TSB.addNextIntent(notificationIntent);
PendingIntent resultPendingIntent = TSB.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
nb.setContentIntent(resultPendingIntent);
nb.setAutoCancel(true);
NotificationManager mNotificationManager =
(NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(NOTIFICATION_ID, nb.build());
this is the complete code:
https://github.com/Jorgesys/Android-Notifications

Notification in Kitkat

I am creating a notification upon click I am asking it cancel a service. It just works fine below 4.4 (Kitkat). My app supports 2.2 (API 8 onwards)
On Kitkit (Nexus 5) The service isn't called at all. I am not where I am going wrong here? It just works fine even on version 4.3?
Here is what I have tried and working on every phone except Nexus 5(Kitkat)
createNotification("Click here to cancel notification!", "TestApp");
I am calling the above immediately after calling a particular service.
private void createNotification(String body,String title)
{
NotificationManager nm = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
int unique_id = 007;
Intent nintent = new Intent(this,ServicetocancelAlarm.class);
PendingIntent pin = PendingIntent.getService(this,0, nintent, 0);
//set notify image
Notification n = new Notification(R.drawable.ic_launcher, body,java.lang.System.currentTimeMillis());
n.contentIntent = pin;
n.setLatestEventInfo(this, title, body, pin);
n.defaults = Notification.DEFAULT_ALL;
nm.notify(unique_id, n);
}
Can somebody help me out fixing this issue with KITKAT?
Update:
I tried the following:
Notification("Message", "This is Android Notification Message");
And Method
#SuppressWarnings("deprecation")
private void Notification(String notificationTitle, String notificationMessage)
{
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
android.app.Notification notification = new android.app.Notification(R.drawable.ic_launcher, "A New Message",
System.currentTimeMillis());
Intent notificationIntent = new Intent(this, TransparentActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
notification.setLatestEventInfo(MainActivity.this, notificationTitle, notificationMessage, pendingIntent);
notificationManager.notify(10001, notification);
}
This piece of code works fine on debug mode but if Export the apk and install it. It just doesn't work at all. It doesn't get to the new activity. I am not sure what is wrong here.
I solved the problem by trying the following code:
int currentapiVersion = android.os.Build.VERSION.SDK_INT;
if (currentapiVersion >= 16)
{
Context context = RepeatService.this;
Intent notificationIntent = new Intent(context, ServicetocancelAlarm.class);
PendingIntent contentIntent = PendingIntent.getService(context, 0, notificationIntent, PendingIntent.FLAG_CANCEL_CURRENT);
NotificationManager nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
Resources res = context.getResources();
Notification.Builder builder = new Notification.Builder(context);
builder.setContentIntent(contentIntent)
.setSmallIcon(R.drawable.ic_launcher)
.setLargeIcon(BitmapFactory.decodeResource(res, R.drawable.ic_launcher))
.setTicker(res.getString(R.string.ticker))
.setWhen(System.currentTimeMillis())
.setAutoCancel(true)
.setContentTitle(res.getString(R.string.app_name))
.setContentText(res.getString(R.string.cancelText));
Notification n = builder.build();
nm.notify(007, n);
}
Else part has the usual notification code that just works fine below API 16..
try it out!!

Android Local Notification

I need to show a local notification in android in title bar when certain event occurs. I want the notification also play certain sound as well.
On tap of notification I want app should not be launched and notification should be cleared.
I am using this code but on tap of notification it opens my app again.
CharSequence title = "App title";
CharSequence message = "This to alert you that estimated time is getting complete.";
NotificationManager notificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
Notification notification = new Notification(R.drawable.ic_launcher, "Title", System.currentTimeMillis());
Intent notificationIntent = new Intent(this, JobView.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
notification.setLatestEventInfo(JobView.this, title, message, null);
notificationManager.notify(1010, notification);
CharSequence title = "App title";
CharSequence message = "This to alert you that estimated time is getting complete.";
NotificationManager notificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
Notification notification = new Notification(R.drawable.ic_launcher, "Title", System.currentTimeMillis());
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
builder.setSound(Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.YOUR_SOUND_FILE);
notificationManager.notify(1234, builder.build());
Just replace the File Reference from your Raw folder in the code and the code should work fine.
I hope this helps.
Try this:
CharSequence title = "App title";
CharSequence message = "This to alert you that estimated time is getting complete.";
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher)
.setAutoCancel(true)
.setContentTitle(title);
mBuilder.setSound(Uri.parse("android.resource://"
+ this.getPackageName() + "/" + R.raw.sound));
mBuilder.setContentText(message);
mBuilder.setTicker(message);
mBuilder.setWhen(System.currentTimeMillis());
NotificationManager notificationManager =
(NotificationManager)getSystemService(NOTIFICATION_SERVICE);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, new Intent(), 0);
mBuilder.setContentIntent(pendingIntent);
notificationManager.notify(1010, mBuilder.build());
Change R.raw.sound with path to your sound file

Notification Big Text Android GCM

I'm having trouble getting Android's Big Text notifications to work as documented here: NotificationCompat.BigTextStyle. Here's the code I'm using to display notifications. I know all the data is coming back correctly because I can get it to display on the console and as the traditional content text and title. Am I doing something wrong? Do I need to define bigTestStyle somewhere else as well? Hopefully one of you has done this before and knows what could be missing. Thanks.
My code:
NotificationCompat.BigTextStyle bigTextStyle = new NotificationCompat.BigTextStyle();
bigTextStyle.bigText(extras.getString("message"));
NotificationCompat.Builder bigTextNotification = new NotificationCompat.Builder(context)
.setContentTitle("My App")
.setContentIntent(pendingIntent)
.setContentText("Message:")
.setSound(soundUri)
.setTicker(extras.getString("message"))
.setWhen(System.currentTimeMillis())
.setSmallIcon(R.drawable.splash)
.setAutoCancel(true)
.setVibrate(new long[] { 0, 100, 200, 300 })
.setStyle(bigTextStyle);
final int notificationId = (int) (System.currentTimeMillis() / 1000L);
NotificationManager thisone = (NotificationManager) getApplicationContext()
.getSystemService(Context.NOTIFICATION_SERVICE);
thisone.notify(notificationId, bigTextNotification.build());
From NotificationCompat.BigTextStyle documentation :
Helper class for generating large-format notifications that include a lot of text.
If the platform does not provide large-format notifications, this method has no effect. The user will always see the normal notification view.
Perhaps your platform doesn't support large-format notifications.
EDIT :
On the other hand, it's possible your problem is here :
NotificationCompat.BigTextStyle bigTextStyle = new NotificationCompat.BigTextStyle();
bigTextStyle.bigText(extras.getString("message"));
You are not using the return value of bigText.
Try to change it to :
NotificationCompat.BigTextStyle bigTextStyle = new NotificationCompat.BigTextStyle();
bigTextStyle = bigTextStyle.bigText(extras.getString("message"));
or to :
NotificationCompat.BigTextStyle bigTextStyle = new NotificationCompat.BigTextStyle().bigText(extras.getString("message"));
EDIT2 :
Costom Notification Layout for older Android versions :
protected void onMessage(Context context, Intent intent) {
// Extract the payload from the message
Bundle extras = intent.getExtras();
if (extras != null) {
String message = (String) extras.get("message");
String title = (String) extras.get("title");
// add a notification to status bar
NotificationManager mManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
Intent myIntent = new Intent(this,MyActivity.class);
Notification notification = new Notification(R.drawable.notification_image, title, System.currentTimeMillis());
notification.flags |= Notification.FLAG_AUTO_CANCEL;
RemoteViews contentView = new RemoteViews(getPackageName(), R.layout.custom_notification);
contentView.setImageViewResource(R.id.image, R.drawable.notification_image);
contentView.setTextViewText(R.id.title, title);
contentView.setTextViewText(R.id.text, message);
notification.contentView = contentView;
notification.contentIntent = PendingIntent.getActivity(this.getBaseContext(), 0, myIntent, PendingIntent.FLAG_CANCEL_CURRENT);
mManager.notify(0, notification);
PowerManager pm = (PowerManager)
context.getSystemService(Context.POWER_SERVICE);
WakeLock wl = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP, "TAG");
wl.acquire(15000);
}
}

Categories

Resources