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
Related
I just started working with notifications and now I'm trying to remove the notification and launch the app once the notification has been tapped in the notificationcenter.
I tried to work with the following code:
import android.app.NotificationManager;
public class ExpandNotification {
private int NOTIFICATION = 546;
private NotificationManager mNM;
public void onCreate() {
mNM.cancel(NOTIFICATION);
setContentView(R.layout.activity_on);
//Toast.makeText(this, "stopped service", Toast.LENGTH_SHORT).show();
}
I think this code executes the other class when tapped?
PendingIntent contentIntent = PendingIntent.getActivity(this, REQUEST_CODE, new Intent(this, ExpandNotification.class), 0);
However the notification doesn't go away, nor does the application launch.
But I'm able to swipe it to left or right to remove it but that's not what I want..
To get the same effect using Notification.Builder or NotificationCompat.Builder call setAutoCancel(true) on the Builder instance.
Use the flag Notification.FLAG_AUTO_CANCEL
Notification notification = new Notification(icon, tickerText, when);
notification.setLatestEventInfo(context, contentTitle, contentText, pendingIntent);
// Cancel the notification after its selected
notification.flags |= Notification.FLAG_AUTO_CANCEL;
and to launch the app:
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
// Create a new intent which will be fired if you click on the notification
Intent intent = new Intent(context, App.class);
// Attach the intent to a pending intent
PendingIntent pendingIntent = PendingIntent.getActivity(context, intent_id, intent, PendingIntent.FLAG_UPDATE_CURRENT);
This answer is too much late but specially i write following solution because notification constructor become deprecated so use notification using builder , like following :
**.setAutoCancel(true)** is used to remove notification on click
and entire notification is like follwoing :
private void makeNotification(String title,String msg){
Intent resultIntent = new Intent(this, MasterActivity.class);
PendingIntent resultPendingIntent =
PendingIntent.getActivity(
this,
0,
resultIntent,
PendingIntent.FLAG_UPDATE_CURRENT
);
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setContentIntent(resultPendingIntent)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle(title)
.setAutoCancel(true)
.setContentText(msg);
int mNotificationId = 001;
NotificationManager mNotifyMgr =
(NotificationManager) getSystemService(NOTIFICATION_SERVICE);
mNotifyMgr.notify(mNotificationId, mBuilder.build());
}
Calling this method with title and message you get perfect notification.
You can directly add .setAutoCancel(true) this line into your code in order to remove notification on click.
This must be added in your builder variable.
Example:
mBuilder = new NotificationCompat.Builder(mContext);
mBuilder.setSmallIcon(R.mipmap.ic_launcher);
mBuilder.setContentTitle("Notification")
.setContentText("Hello")
.setSound(Settings.System.DEFAULT_NOTIFICATION_URI)
.setAutoCancel(true)
.setContentIntent(pendingIntent);
Best & simple way is set builder.setAutoCancel(true) it's cancel your notification after clicking on notification. I hope this code help you.
Builder for notification
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
builder.setSmallIcon(android.R.drawable.btn_star);
builder.setContentTitle("This is title of notification");
builder.setContentText("This is a notification Text");
builder.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher));
For open an activity on clicking notification
Intent intent = new Intent(Broadcastdemo.this, ThreadDemo.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 113,intent, PendingIntent.FLAG_UPDATE_CURRENT);
builder.setContentIntent(pendingIntent);
builder.setAutoCancel(true);
Show bulder in notification
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
manager.notify(114, builder.build());
Complete code for show a notification with icon, image, title, description, auto cancel and on click open an activity
public void ShowIntentNotification(View v)
{
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
builder.setSmallIcon(android.R.drawable.btn_star);
builder.setContentTitle("This is title of notification");
builder.setContentText("This is a notification Text");
builder.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher));
Intent intent = new Intent(Broadcastdemo.this, ThreadDemo.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 113,intent, PendingIntent.FLAG_UPDATE_CURRENT);
builder.setContentIntent(pendingIntent);
builder.setAutoCancel(true);
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
manager.notify(114, builder.build());
}
For my it was that
.setPriority(Notification.PRIORITY_HIGH);
that was causing the notification to not clear after click... make sure you use:
.setPriority(Notification.PRIORITY_DEFAULT);
And .setAutoCancel(true) should work.
I want to create a full screen notification.I have achieved a notification by using the following code. What changes do i need to make it a full screen notification.
private void showNotification(String data) {
Intent i = new Intent(this, MapsActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this,0,i,PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
.setAutoCancel(true)
.setContentTitle("FCM Test")
.setContentText(data)
.setSmallIcon(R.drawable.common_google_signin_btn_icon_dark)
.setContentIntent(pendingIntent);
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
manager.notify(0,builder.build());
}
You can convert simple notification to full screen notification just added a simple line of code builder.setFullScreenIntent(pendingIntent, true); following is full example. I hope this code help you
Full Screen Notification Code:
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
builder.setSmallIcon(android.R.drawable.btn_star);
builder.setContentTitle("This is title of notification");
builder.setContentText("This is a notification Text");
builder.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher));
Intent intent = new Intent(Broadcastdemo.this, ThreadDemo.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 113,intent, PendingIntent.FLAG_UPDATE_CURRENT);
builder.setContentIntent(pendingIntent);
builder.setAutoCancel(true);
builder.setFullScreenIntent(pendingIntent, true);
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
manager.notify(115, builder.build());
On your builder, just before setContentIntent(pendingIntent);
Simply add the following line: .setFullScreenIntent(pendingIntent, true);
try to add both setStyle and setPriority as the following: (it is working fine for me)
.setStyle(new Notification.BigTextStyle().BigText(message.Long_Message))
.setPriority((int)NotificationPriority.Max)
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());
I am making an application that uses notification in it but when the notification is about to appear this message is appearing continuously till i turn of the AVD "Unfortunately , System UI has stopped"
Intent viewIntent = new Intent(getBaseContext(), WorkshopActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(getBaseContext(), 0, viewIntent, PendingIntent.FLAG_UPDATE_CURRENT);
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(getBaseContext())
.setSmallIcon(R.drawable.web)
.setContentTitle(getResources().getString(R.string.app_name))
.setContentText("Scheduled")
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent).setTicker("Compass");
NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle();
notificationBuilder.setStyle(inboxStyle);
notificationBuilder.setStyle(inboxStyle);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
int NOTIFICATION_ID = 100;
notificationManager.notify(NOTIFICATION_ID, notificationBuilder.build());
can you please replace getBaseContext() with 'this' or at least getApplicationContext() because BaseContent has a very rare & spesific usage.
and
.setContentIntent(pendingIntent).setTicker("Compass");
after this can you build the notificationBuilder,
.setContentIntent(pendingIntent).setTicker("Compass").build();
maybe it needs to build before adding style and addign it to manager.
lastly,
notificationManager.notify(NOTIFICATION_ID, notificationBuilder);
I am building and easy android app.
I am having trouble with notification. When the app receive the notification it shows it properly as heads up with all the picture and the text, while on the lock screen, if the phone is locked, does not show the text. To see the text I have to swipe the notification down.
This is the code:
NotificationCompat.BigTextStyle bigText = new NotificationCompat.BigTextStyle();
bigText.bigText(body);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.ic_stat_luceterna_notifica_90)
.setContentTitle("Luceterna")
.setLargeIcon((((BitmapDrawable) this.getResources().getDrawable(R.mipmap.ic_launcher_lux)).getBitmap()))
.setAutoCancel(true)
.setVisibility(NotificationCompat.VISIBILITY_PUBLIC);
mBuilder.setStyle(bigText);
Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
mBuilder.setSound(alarmSound);
NotificationManager mNotificationManager = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
Can please anyone help me? I cannot find any answer to the problem.
Thanks in advance.
I know it's been a while since you asked this question, but I encountered same problem today, and found a sollution.
To solve this problem, you have to override NotificationCompat setContentTitle and
setContentDescritption
With BigTextStyle ones: setBigContentTitle and bigText
You have to use them both, with same texts inside. So, your code would look like:
String title = "Title";
String description = "Description";
NotificationCompat.BigTextStyle bigTextStyle = new NotificationCompat.BigTextStyle();
bigTextStyle.setBigContentTitle(title);
bigTextStyle.bigText(description);
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setPriority(NotificationCompat.PRIORITY_MAX)
.setContentTitle(title)
.setContentText(description)
.setSmallIcon(R.drawable.ic_launcher)
.setStyle(bigTextStyle);
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(0, mBuilder.build());
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher) // notification icon
.setContentTitle("Notification!") // title for notification
.setContentText("Hello word") // message for notification
.setAutoCancel(true); // clear notification after click
Intent intent = new Intent(this, MainActivity.class);
PendingIntent pi = PendingIntent.getActivity(this,0,intent,Intent.FLAG_ACTIVITY_NEW_TASK);
mBuilder.setContentIntent(pi);
NotificationManager mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(0, mBuilder.build());
An example to help you