Create own NotificationStyle - android

In my Android application I want to create a expendable notification. For this I have to set a Style for the notification like this:
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
builder.setSmallIcon(android.R.drawable.ic_menu_info_details);
builder.setContentTitle("My notification");
builder.setContentText("Hello World!");
builder.setStyle(...);
Android gives me some styles I can use with this method but I like to create my own, so I can load a layout in the notification. So how can I do this? If I create a subclass of Style there are only these two methods:
public Notification build();
public void setBuilder(Builder builder);
So how can I load my own layout in the notification? builder.setContent is insufficient for me because then I only have 64dp for the layout.

Found a solution for this problem. It's really easy. Just use this code here:
String packageName = this.getPackageName();
RemoteViews bigView = new RemoteViews(packageName, R.layout.test);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
builder.setSmallIcon(android.R.drawable.ic_menu_info_details);
builder.setContentTitle("My notification");
builder.setContentText("Hello World!");
Notification noti = builder.build();
noti.bigContentView = bigView;
NotificationManager mgr = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
mgr.notify(1234, noti);

Related

Custom notification being contracted

I am creating a custom notification using remoteviews. It works great, but if another notifications arrive then my custom notification gets contracted.
Is there any way to always have it expanded?
I use this code:
Notification.Builder builder = new Notification.Builder(MyContext);
builder.setTicker("My ticker");
... other builder values ...
RemoteViews oRemoteViews = new RemoteViews(Contexto.getPackageName(), R.layout.my_notification_layout);
oRemoteViews.setImageViewResource(R.id.myIcon, R.drawable.myicon);
oRemoteViews.setTextViewText(R.id.text1, "Other text here");
... other views values ...
Notification oNotification = builder.build();
oNotification.bigContentView = oRemoteViews;
NotificationManager oNotificationManager = (NotificationManager) Contexto.getSystemService(Context.NOTIFICATION_SERVICE);
oNotificationManager.notify("xxxxx", NOTIF_ID, oNotification);

How to use both BigTextStyle and BigPictureStyle in setStyle Notification?

I am trying to use both BigTextStyle and BigPictureStyle in my notification.But setStyle accepts only one style.
My code:
NotificationCompat.BigTextStyle bigTextStyle = new NotificationCompat.BigTextStyle();
mBuilder.setVisibility(1);
mBuilder.setSmallIcon(R.drawable.app_icon1);
mBuilder.setContentTitle(title.toString());
bigTextStyle.bigText(description.toString());
//mBuilder.setSubText(bigText.toString());
if (bigImage != null && !bigImage.toString().equals("")) {
mBuilder.setStyle(new NotificationCompat.BigPictureStyle().bigPicture(ImageUtil.getBitmapFromUrl(bigImage.toString())));
}
mBuilder.setStyle(bigTextStyle);
mBuilder.setPriority(Notification.PRIORITY_MAX);
mBuilder.setContentIntent(contentIntent);
How can i use both ?. I want to show text(with line breaks) along with the image!
Sorry for late reply..Actually i was also faced the same problem and got the solution, so i am thinking that it can help for other user.
As we can NOT use the both BigTextStyle and BigPictureStyle method of the NotificationCompat.Builder than we can create the CustomView.
We can use the setCustomBigContentView(RemoteViews) method of NotificationCompat.Builder and create our own view to show the Big Image with Big text.
Please check the below code for it:-
PendingIntent pendingIntent = PendingIntent.getActivity(this, (int) System.currentTimeMillis(), i,
PendingIntent.FLAG_ONE_SHOT);
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this);
notificationBuilder.setContentTitle("YOUR_APP_NAME");
notificationBuilder.setContentText(body);
notificationBuilder.setTicker("YOUR_APP_NAME");
notificationBuilder.setAutoCancel(true);
notificationBuilder.setSound(defaultSoundUri);
notificationBuilder.setCustomBigContentView(remoteView("YOUR_MESSAGE_TO_SHOW"));///IT IS THE MAIN METHOD WHICH WE USE TO INFLATE OR CREATE THE CUSTOM VIEW
notificationBuilder.setSmallIcon(getNotificationIcon(notificationBuilder));
notificationBuilder.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify((int) System.currentTimeMillis(), notificationBuilder.build());
Below is the RemoteViews which we have called from our setCustomBigContentView() method
private RemoteViews remoteView(String message)
{
RemoteViews views;
views = new RemoteViews(getPackageName(), R.layout.YOUR_LAYOUT_HERE);
views.setImageViewBitmap(R.id.YOUR_BIG_IMAGE_ID_FROM_LAYOUT, bitmap);
views.setImageViewBitmap(R.id.YOUR_APP_ID_FROM_LAYOUT, BitmapFactory.decodeResource(getResources(), R.drawable.APP_ICON_OF_YOUR_APP));
views.setTextViewText(R.id.YOUR_BIG_TEXTVIEW_ID_FROM_LAYOUT, message);
return views;
}
I have created the custom notification like it

Android:Modify Pending Notification's Content

I am trying to modify existing notifications in android.
What I have in my app
When a notification is already in system tray and another notification appears, the second one overwrites the first notification content.
What I am looking for
If second Notification arrives then instead of overwriting the first I need to change title to show 2 New Messages and go on incrementing as notifications arrive.
Code Implemented
Bitmap icon = BitmapFactory.decodeResource(ctx.getResources(),
R.drawable.icon);
Intent launchActivity = new Intent(ctx, CordovaApp.class);
launchActivity.putExtra("heading",newsHeader);
launchActivity.putExtra("content",newsText);
PendingIntent pi = PendingIntent.getActivity(ctx,0, launchActivity, PendingIntent.FLAG_NO_CREATE);
ParseAnalytics.trackAppOpened(launchActivity);
if(pi==null){
Log.d(TAG, "Pending Intenet is null.");
}else{
Log.d(TAG, "Pending Intenet is not null.");
}
Notification noti = new NotificationCompat.Builder(ctx)
.setContentTitle(newsHeader)
.setContentText(newsText)
.setSmallIcon(R.drawable.icon)
.setLargeIcon(icon)
.setContentIntent(pi)
.setAutoCancel(true)
.build();
NotificationManager nm = (NotificationManager)ctx.getSystemService(Context.NOTIFICATION_SERVICE);
nm.notify(0, noti);
Update
I implemented the solution mentioned below by #yogendra and now I am getting two separate notifications. Instead of getting stacked. Below is updated code
Notification noti = new NotificationCompat.Builder(ctx)
.setContentTitle(newsHeader)
.setContentText(newsText)
.setSmallIcon(R.drawable.icon)
.setGroup(GROUP_KEY_EMAILS)
.setLargeIcon(icon)
.setContentIntent(pi)
.setLights(Color.parseColor("green"), 5000, 5000)
.setAutoCancel(true)
.setPriority(2)
.setTicker("Notification from App")
.setGroupSummary(true)
.build();
NotificationManager nm = (NotificationManager)ctx.getSystemService(Context.NOTIFICATION_SERVICE);
int timeSeconds = (int)System.currentTimeMillis()%Integer.MAX_VALUE;
Log.i(TAG,"Timing function called "+timeSeconds);
nm.notify(timeSeconds, noti);
See your code
nm.notify(0, noti);
where
notify(int id, Notification notification)
Here 0 is the ID of notification which is to be managed with respect to each notification. If you want to show a different notification your notification id should be unique each time. If you try to post a notification with the same notification id your previously displayed notification will be replaced with the latest notification.
Solution
Now you need to display a custom notification with a custom layout and update the counter each time .
Source code to create a Custom Notification.
create global variable in your class :
private int count = 0;
private ArrayList<String> notificationList = new ArrayList<String>();
private String GROUP_KEY_EMAILS = "email";
/call method createNotification when you need to create notification and pass message what you need to show on message./
private void createNotification(String notificationMassage) {
notificationList.add(notificationMassage);
NotificationManager mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
Bitmap largeIcon = BitmapFactory.decodeResource(getResources(),
R.drawable.ic_launcher);
// Create builder
Builder summaryNotification = new NotificationCompat.Builder(this)
.setContentTitle(notificationList.size()+" new messages")
.setSmallIcon(R.drawable.settings)
.setLargeIcon(largeIcon)
.setGroup(GROUP_KEY_EMAILS)
.setGroupSummary(true)
.setAutoCancel(true);
// Create style
InboxStyle nStyle = new NotificationCompat.InboxStyle();
nStyle.setBigContentTitle(notificationList.size()+" new messages");
nStyle.setSummaryText("Summery Text...<you can set as blank>");
for (String Str : notificationList) {
nStyle.addLine(Str);
}
summaryNotification.setStyle(nStyle);
mNotificationManager.notify(0, summaryNotification.build());
count++;
}
/**
please clear notification array list after tap on notification.
*/
For more detail please refer below link:
https://developer.android.com/training/wearables/notifications/stacks.html#AddGroup
https://developer.android.com/training/wearables/notifications/stacks.html#AddGroup

Android : Unable to display multiple lines of text in Notification

I am trying to display multiple lines of Text using BigTextStyle in Notification but unable to do so. I am using the code below.
public void sendNotification(View view) {
String msgText = "Jeally Bean Notification example!! "
+ "where you will see three different kind of notification. "
+ "you can even put the very long string here.";
NotificationManager notificationManager = getNotificationManager();
PendingIntent pi = getPendingIntent();
android.app.Notification.Builder builder = new Notification.Builder(
this);
builder.setContentTitle("Big text Notofication")
.setContentText("Big text Notification")
.setSmallIcon(R.drawable.ic_launcher).setAutoCancel(true)
.setPriority(Notification.PRIORITY_HIGH)
.addAction(R.drawable.ic_launcher, "show activity", pi);
Notification notification = new Notification.BigTextStyle(builder)
.bigText(msgText).build();
notificationManager.notify(0, notification);
}
public NotificationManager getNotificationManager() {
return (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
}
public PendingIntent getPendingIntent() {
return PendingIntent.getActivity(this, 0, new Intent(this,
MainActivity.class), 0);
}
I can't even see 'msgText' in the notification. Any idea why?
Thanks for helping.
Solved!
Code was fine, its just that there was not enough space for big notification. When I disconnected data cable, it got displayed in desired manner. :-)
Thanks to all who tried to help.
Just set notification style to BigText
NotificationCompat.BigTextStyle bigStyle =
new NotificationCompat.BigTextStyle();
bigStyle.setBigContentTitle(title);
bigStyle.bigText(messageBody);
builder.setStyle(bigStyle);
Try to set content intent value as well.
Notification.Builder builder = new Notification.Builder(this);
builder.setContentTitle("Big text Notification")
.setContentText("Big text Notification")
.setSmallIcon(R.drawable.ic_launcher)
.setAutoCancel(true)
.setContentIntent(pi) // <- this new line
.setPriority(Notification.PRIORITY_HIGH)
.addAction(R.drawable.ic_launcher, "show activity", pi);
.setPriority(Notification.PRIORITY_HIGH) - this line solved my problem. The multiline conserve size.

Android notification button in normal view

I want to add a pause button and if possible a remove button in the notifications.
Like this:
How can I do this?
This is my Code for the notification:
notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
context = getApplicationContext();
builder = new NotificationCompat.Builder(context)
.setContentTitle("Go!")
.setContentText("Timer set to " + waitingtime/1000 + " seconds")
.setTicker("Started!")
.setWhen(System.currentTimeMillis())
.setDefaults(Notification.DEFAULT_SOUND)
.setAutoCancel(false)
.setOngoing(true)
.setSmallIcon(R.drawable.notlogosmall);
You will need to add a custom view, it is like how you show views on a home screen widget:
Look at this method on the Notification Builder
http://developer.android.com/reference/android/support/v4/app/NotificationCompat.Builder.html#setContent(android.widget.RemoteViews)
Pass it your RemoteView with the two buttons you want in the layout.
small example:
RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.custom_notification);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher)
.setContent(remoteViews);

Categories

Resources