I'm trying to group my notifications but apparently they keep coming separately.
NotificationManager mNotificationManager = (NotificationManager)
this.getSystemService(Context.NOTIFICATION_SERVICE);
Intent resultIntent = new Intent(this, SplashScreenBaseActivity.class);
resultIntent.putExtra(Arguments.ARG_NOTIFICATION_TYPE, NEWS_TYPE);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent =
stackBuilder.getPendingIntent(
0,
PendingIntent.FLAG_UPDATE_CURRENT
);
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher_small)
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher))
.setContentTitle(contentTitle)
.setContentText(contentText)
.setAutoCancel(true)
.setGroup(NEWS_GROUP)
.setGroupSummary(true)
.setDefaults(-1);
mBuilder.setContentIntent(resultPendingIntent);
Random random = new Random();
NOTIFICATION_ID = random.nextInt(9999 - 1000) + 1000;
mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
Related
I'm added Notification code in my android service.
Tried
Notifications Code in Android
Log.e("TAG","In 2");
Intent viewIntent = new Intent(getApplicationContext(), Restarter.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, viewIntent , PendingIntent.FLAG_UPDATE_CURRENT);
Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.logo)
.setContentTitle(getResources().getString(R.string.app_name))
.setAutoCancel(true)
.setContentIntent(pendingIntent);
NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle();
notificationBuilder.setStyle(inboxStyle);
inboxStyle.setBigContentTitle("sdjshfjnds");
inboxStyle.addLine("sdjjsdfn");
notificationBuilder.setStyle(inboxStyle);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
int NOTIFICATION_ID = 100;
notificationManager.notify(NOTIFICATION_ID, notificationBuilder.build());
I m getting log message but not notification.
This is the old answer, it's 2015 year. Now it's not actual. Please check the actual documentation: https://developer.android.com/training/notify-user/build-notification
I have search and found solution but that solution is not showing multi line push notification.
My code is below,
int id = (int) System.currentTimeMillis();
Intent intent = new Intent(this, Activity.class);
Bundle bundle = new Bundle();
bundle.putString("id", u_id);
intent.putExtras(bundle);
PendingIntent resultPendingIntent =
PendingIntent.getActivity(
this,
0,
intent,
PendingIntent.FLAG_CANCEL_CURRENT
);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
this);
Notification notification = mBuilder.setSmallIcon(getNotificationIcon()).setTicker(getResources().getString(R.string.app_name)).setWhen(0)
.setAutoCancel(true)
.setContentTitle(getResources().getString(R.string.app_name))
.setStyle(new NotificationCompat.BigTextStyle().bigText(messageBody))
.setContentIntent(resultPendingIntent)
.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
.setLargeIcon(BitmapFactory.decodeResource(getResources(), getNotificationIcon()))
.setContentText(messageBody).build();
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(id, notification);
How can I show multi line text in push notification?
Intent intent = new Intent(this, target.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
PendingIntent.FLAG_ONE_SHOT);
Bitmap image= BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher);
Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.youricon)
.setContentTitle("Title")
.setLargeIcon(image)
.setContentText("Message")
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent)
.setStyle(new NotificationCompat.BigPictureStyle().bigPicture(bitmap).setSummaryText("multiline text"));
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(m/* ID of notification */, notificationBuilder.build());
Image in big picture style not showing in proper way. From top and bottom it cuts.
Here is code to generate notification:
private void notificationWithImage(String url, String msg, int smallLogo) {
try {
Bitmap icon1 = BitmapFactory.decodeResource(getResources(),
R.drawable.ic_launcher);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
this).setAutoCancel(true)
.setContentTitle("XXXXXXXAPP")
.setSmallIcon(smallLogo)
.setLargeIcon(icon1);
NotificationCompat.BigPictureStyle bigPicStyle = new NotificationCompat.BigPictureStyle();
// bigPicStyle.bigPicture(Picasso.with(getApplicationContext()).load(url).resize(320, 256).centerInside().get());
Bitmap bitmap_image = BitmapFactory.decodeResource(this.getResources(), R.drawable.ic_edited_noti);
bigPicStyle.bigPicture(bitmap_image);
bigPicStyle.setBigContentTitle("XXXXXXXAPP");
bigPicStyle.setSummaryText(msg);
mBuilder.setStyle(bigPicStyle);
if (getLoginDetailFromPrefs())
resultIntent = new Intent(this, Perspective.class);
else
resultIntent = new Intent(this, LoginActivity.class);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
if (getLoginDetailFromPrefs())
stackBuilder.addParentStack(Perspective.class);
else
stackBuilder.addParentStack(LoginActivity.class);
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.setContentIntent(resultPendingIntent);
NotificationManager mNotificationManager = (NotificationManager)
getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(1, mBuilder.build());
} catch (Exception e) {
e.printStackTrace();
}
}
I added screen shot with question
The thing that you want can be done perfectly by the use RemoteView in Notification & then Apply customView in Builder.
This is a template code to use RemoteViews : -
RemoteViews remoteViews = new RemoteViews(getApplicationContext().getPackageName(), R.layout.notification_layout);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(getApplicationContext())
.setSmallIcon(R.drawable.notification_small)
.setColor(getApplicationContext().getResources().
getColor(R.color.colorPrimary))
.setCustomBigContentView(remoteViews)
.setLargeIcon(bitmap)
.setContent(remoteViews)
.setOngoing(true)
.setContentTitle("")
.setAutoCancel(false)
.setTicker(getString(R.string.app_name), remoteViews)
.setPriority(Notification.PRIORITY_HIGH)
.setContentText("");
NotificationManager mNotificationManager =(NotificationManager)
getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(1, mBuilder.build());
I have generated multiple Local notifications by matching data from database.
Now i want that on clicking on the notification it will take to the particular products.
Code:----------
for (int i=0;i < alertList.size();i++) {
Random random = new Random();
int m = random.nextInt(9999 - 1000) + 1000;
msg=alertList.get(i).getProf_name()+" have to take "+alertList.get(i).getMed_name()+" ;"+alertList.get(i).getMed_id();
Log.d("MediMSg",""+msg);
Log.d("MediMSg",""+msg);
Intent notificationIntent = new Intent(context, MedicineNotificationActivity.class);
notificationIntent.putExtra("pushmsg",msg);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
stackBuilder.addParentStack(MedicineNotificationActivity.class);
stackBuilder.addNextIntent(notificationIntent);
PendingIntent pendingIntent = stackBuilder.getPendingIntent(100, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
Notification notification = builder.setContentTitle("Test")
.setContentText(msg)
.setTicker("Notification from Test")
.setSmallIcon(getNotificationIcon())
.setPriority(Notification.PRIORITY_MAX)
.setAutoCancel(true)
.setColor(0x0091ea)
.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
.setContentIntent(pendingIntent).build();
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(m,notification);
Log.d("Random",""+m);
}
Here's my code :
public static void pushNotification(Activity currentActivity, String title, String content[]){
NotificationManager nm = (NotificationManager) currentActivity.getSystemService(Context.NOTIFICATION_SERVICE);
Intent resultIntent = new Intent(currentActivity.getBaseContext(), FileSharingActivity.class);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(currentActivity);
stackBuilder.addParentStack(FileSharingActivity.class);
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0,PendingIntent.FLAG_UPDATE_CURRENT);
nm.notify(nextId++, new Notification.InboxStyle(new Notification.Builder(currentActivity.getBaseContext())
.setTicker("Ticker")
.setSmallIcon(R.drawable.ic_launcher)
.setWhen(System.currentTimeMillis())
.setContentTitle("Content title")
.setContentText("Content text")
.setNumber(4)
.setContentIntent(resultPendingIntent))
.addLine("First Message")
.addLine("Second Message")
.addLine("Third Message")
.addLine("Fourth Message")
.setBigContentTitle("Here Your Messages")
.setSummaryText("+3 more")
.build());
}
Here's the result :
Device is S3 running Android version 4.4.2.
Why is the InboxStyle not applied?
Maybe you can try this:
Notification notif = new Notification.Builder(mContext)
.setContentTitle("5 New mails from " + sender.toString())
.setContentText(subject)
.setSmallIcon(R.drawable.new_mail)
.setLargeIcon(aBitmap)
.setStyle(new Notification.InboxStyle() //Style is here
.addLine(str1)
.addLine(str2)
.setContentTitle("")
.setSummaryText("+3 more"))
.build();
More info here: http://developer.android.com/reference/android/app/Notification.InboxStyle.html
EDIT:
I do it like this in my Android Project:
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent, 0);
Notification n = new Notification.Builder(this)
.setContentTitle(titel)
.setContentText(text)
.setTicker(ticker)
.setSmallIcon(R.drawable.ic_launcher)
.setContentIntent(pIntent)
.setAutoCancel(true)
.getNotification();
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
n.flags |=Notification.FLAG_AUTO_CANCEL;
notificationManager.notify(0, n);
I had to swipe down the notification to see the expanded information.
The code works just fine.