how to get multiple messages in a single notification .This is my following snippet
NotificationCompat.Builder nBuilder = new NotificationCompat.Builder(context);//
Intent nIntent = new Intent(context, ConvActivity.class);
PendingIntent nPendingInten = PendingIntent.getActivity(context, 0, nIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
nBuilder.setSmallIcon(R.drawable.app_icon_indietext_new);
nBuilder.setLargeIcon(BitmapFactory.decodeResource(res, R.drawable.app_icon_indietext_new));
nBuilder.setContentTitle(senderNum);
nBuilder.setContentText(message);
nBuilder.setStyle(new NotificationCompat.InboxStyle()
.addLine(message)
.addLine(message))
.setGroup(GROUP_KEY_EMAILS)
.setGroupSummary(true)
.build();
TaskStackBuilder nStackBuilder = TaskStackBuilder.create(context);
nStackBuilder.addParentStack(ConvActivity.class);
nStackBuilder.addNextIntent(nIntent);
nBuilder.setContentIntent(nPendingInten);
NotificationManager nNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
nBuilder.setAutoCancel(true);
//nNotificationManager.notify(0, nBuilder.build());
nNotificationManager.notify(9999,nBuilder.build());
what is the procedure to get multiple notifications
You just have to use different IDs
mNotificationManager.notify(ID, notifyDetails);
and set multiple messages in those ID's
Related
In the above image the I am quite sure that the notification style is Notification.MessageStyle
which is working proerly in my code, but I am not able to set the text below it the one which in the image shows the email and another text which is showing the number of unread messages. How do I show the two textviews there?
My Code:
Person user = new Person.Builder().setIcon(IconCompat.createWithBitmap(BitmapFactory.decodeResource(context.getResources(), R.mipmap.ic_launcher))).setName("You").build();
NotificationCompat.MessagingStyle style = new NotificationCompat.MessagingStyle(user)
.setConversationTitle("Title")
.addMessage("hi", System.currentTimeMillis(), new Person.Builder().setName("Pemba").build())
.addMessage("hello", System.currentTimeMillis(), new Person.Builder().setName("Suku").build())
.addMessage("hello", System.currentTimeMillis(), new Person.Builder().build())
.setGroupConversation(true);
Intent intent = new Intent(context, MainActivity.class);
PendingIntent pIntent = PendingIntent.getActivity(context, (int) System.currentTimeMillis(), intent, 0);
PendingIntent contentIntent = PendingIntent.getActivity(context, 0,
new Intent(context, MainActivity.class), 0);
final NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(context, CHANNNEL_ID);
mBuilder.setSmallIcon(R.drawable.robot_noti)
.setColor(context.getResources().getColor(R.color.colorPrimary))
.addAction(android.R.drawable.radiobutton_on_background, "Button", pIntent)
.setStyle(style)
.setContentIntent(contentIntent)
.setDefaults(Notification.DEFAULT_SOUND)
.setOnlyAlertOnce(true)
.setAutoCancel(true);
final NotificationManager mNotificationManager =
(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(1, mBuilder.build());
You can use setContentText and setSubText
Look at this question.
For the number of messages which are unread:
Hope this helps
Notification work normally one problem is All notification go to last notification link. How can I fix it? Every notification go to it's own link. Every message notification go to he's/she's inbox. How can I do that?
Intent intent_activity_start = new Intent(this,MainActivity.class);
intent_activity_start.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
intent_activity_start.putExtra("links", c.getString("link"));
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent_activity_start,PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
builder.setSmallIcon(R.drawable.logo);
builder.setContentIntent(pendingIntent);
builder.setAutoCancel(true);
builder.setLargeIcon(getCircleBitmap(photoStream(c.getString("profile_photo"))));
builder.setContentTitle("Title");
builder.setContentText(c.getString("notify"));
builder.setSound(notificationSound);
builder.setStyle(new NotificationCompat.BigTextStyle()
.bigText(c.getString("notify")));
if(!c.getString("photo").equals("null")){
builder.setStyle(new NotificationCompat.BigPictureStyle()
.bigPicture(photoStream(c.getString("photo"))));
}
NotificationManager notificationManager = (NotificationManager) getSystemService(
NOTIFICATION_SERVICE);
notificationManager.notify(counter.incrementAndGet(), builder.build());
Nothing happens on clicking Accept or Reject button of the head-ups notification.
But when the head-up notification disappear and from clicking Accept and Reject from the notification panel is working.
Testing on Android 5.1.0.
Intent acceptIntent = new Intent(this, NotificationReceiver.class);
acceptIntent.setAction("com.android.test.Accept");
PendingIntent acceptPendingIntent = PendingIntent.getBroadcast(TestApplication.getAppContext(), 12345, acceptIntent, PendingIntent.FLAG_CANCEL_CURRENT);
Intent rejectIntent = new Intent(this, NotificationReceiver.class);
rejectIntent.setAction("com.android.test.Reject");
PendingIntent rejectPendingIntent = PendingIntent.getBroadcast(TestApplication.getAppContext(), 12345, rejectIntent, PendingIntent.FLAG_CANCEL_CURRENT);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
builder.setSmallIcon(R.drawable.fundu);
builder.setContentTitle("Test Notification");
builder.setContentText("Hello");
builder.setAutoCancel(true);
builder.setVisibility(NotificationCompat.VISIBILITY_PUBLIC);
builder.setCategory(NotificationCompat.CATEGORY_SERVICE);
builder.setDefaults(NotificationCompat.DEFAULT_ALL);
builder.setPriority(NotificationCompat.PRIORITY_MAX);
builder.addAction(R.drawable.ic_check_icon, "Accept", acceptPendingIntent);
builder.addAction(R.drawable.ic_action_close, "Reject", rejectPendingIntent);
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, builder.build());
Just setting the vibration, makes it working fine.
builder.setVibrate(new long[0]);
The key is to call setContentIntent on the Notification Builder and passing it a PendingIntent. The Full code with comments explaining each step is included below. See the part named "THIS IS THE PERTINENT PART" (The Full code is included for completeness sake.)
// Use Notification Builder to start things off
// There are other ways of acquiring a Notification Builder; this is just an example
String channelId = "channelId";
String title = "title";
String body = "body";
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(context, channelId);
notificationBuilder
.setSmallIcon(R.drawable.ic_alarm)
.setContentTitle(title)
.setContentText(body);
//--------------- THIS IS THE PERTINENT PART ---------------
// Prepare Intent for creating PendingIntent
Intent intent = new Intent(context, ActivityToStart.class);
// Create Pending Intent
int requestCode= 1234; // requestCode has to be a unique ID for EACH PendingIntent
TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
stackBuilder.addNextIntent(intent);
PendingIntent pendingIntent = stackBuilder.getPendingIntent(
requestCode,
PendingIntent.FLAG_UPDATE_CURRENT // use to prevent re-using current Activity Intent
);
notificationBuilder.setContentIntent(pendingIntent);
// Finally, create the Notification
Notification notification = notificationBuilder.build();
I sent some notification:
NotificationCompat.Builder mBuilder = new
Intent resultIntent = new Intent(this, AutoEdviserActivity.class);
resultIntent.putExtra("id",i);
PendingIntent resultPendingIntent =
PendingIntent.getActivity(
this,
0,
resultIntent,
PendingIntent.FLAG_UPDATE_CURRENT
);
mBuilder.setContentIntent(resultPendingIntent);
int mNotificationId = i+900;
NotificationManager mNotifyMgr =
(NotificationManager) getSystemService(NOTIFICATION_SERVICE);
mNotifyMgr.notify(mNotificationId, mBuilder.build());
With flag PendingIntent.FLAG_UPDATE_CURRENT, when I see more one notification and push those which I get early, in AutoEdviserActivity I getExtra from last notification.
Another flags as I understand, unsuitable to: FLAG_ONE_SHOT, FLAG_NO_CREATE, FLAG_CANCEL_CURRENT
How can I get different Extra from different Notifications?
I am using this code inside a service in order to get a notification if I have any new alerts but when I click on them I'm not getting to the view that I want:
if(newAlertCounter > 0) // alert user about new alerts
{
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_action_warning)
.setContentTitle(newAlertCounter + " new flood alerts!")
.setContentText("Tap to see where in your vecinity.");
// Sets an ID for the notification
int mNotificationId = 001;
// Gets an instance of the NotificationManager service
NotificationManager mNotifyMgr = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
// Builds the notification and issues it.
mNotifyMgr.notify(mNotificationId, mBuilder.build());
// notification click action
Intent notificationIntent = new Intent(this, ViewAlertsActivity.class);
PendingIntent resultPendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.setContentIntent(resultPendingIntent);
}
It shows up but its not clickable, so whats wrong with this?
put
mNotifyMgr.notify(mNotificationId, mBuilder.build());
after
mBuilder.setContentIntent(resultPendingIntent);
Move your
mNotifyMgr.notify(mNotificationId, mBuilder.build());
after
mBuilder.setContentIntent(resultPendingIntent);
When you call .build() you create the notification without the content intent. (and no, it will not be added after because the object which will be sent to notification system will be the Notification not the Builder)
if(newAlertCounter > 0) // alert user about new alerts
{
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_action_warning)
.setContentTitle(newAlertCounter + " new flood alerts!")
.setContentText("Tap to see where in your vecinity.");
// Sets an ID for the notification
int mNotificationId = 001;
// Gets an instance of the NotificationManager service
NotificationManager mNotifyMgr = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
// Builds the notification and issues it.
// notification click action
Intent notificationIntent = new Intent(this, ViewAlertsActivity.class);
PendingIntent resultPendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.setContentIntent(resultPendingIntent);
mNotifyMgr.notify(mNotificationId, mBuilder.build());
}