With multiple notifications second notification showing in collapse mode Android - 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());

Related

Notification Code added in Service but not working

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

no sound of firebase notifications on marshmallows, but works on oreo

I don't know why when I send a notification to marshmallow, there is no sound, but when I send it to oreo, there is a sound notification.
My firebase code is below
Intent intent = new Intent(this, MainActivity.class);
if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.O) {
intent.addFlags(FLAG_ACTIVITY_CLEAR_TOP);
intent.putExtra("idata", idata);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
PendingIntent.FLAG_ONE_SHOT);
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle(title)
.setContentText(messageBody)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
} else {
Context c = getApplicationContext();
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// The id of the channel.
final String CHANNEL_ID = "default";
// The user-visible name of the channel.
final String CHANNEL_NAME = "Default";
NotificationChannel defaultChannel = new NotificationChannel(CHANNEL_ID, CHANNEL_NAME, NotificationManager.IMPORTANCE_HIGH);
notificationManager.createNotificationChannel(defaultChannel);
intent.setFlags(FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_CLEAR_TOP);
intent.putExtra("idata", idata);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
final NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, CHANNEL_ID)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle(title)
.setContentText(messageBody)
.setWhen(System.currentTimeMillis())
.setSound(defaultSoundUri)
.setDefaults(Notification.DEFAULT_SOUND | Notification.DEFAULT_VIBRATE)
.setAutoCancel(true)
.setPriority(NotificationCompat.PRIORITY_MAX)
.setContentIntent(pendingIntent);
notificationManager.notify("myapp", 0, notificationBuilder.build());
}
I searched all over the internet, but there were no results for this, I really needed this, please help me, any help would really be appreciated. thanks
I had the same problem, but the channel id is only used by oreo & higer android versions. If no sound playing in less than oreo, then make sure you have added "sound": "default"//or any custom filename you have in ur res/ folder.
I accidently missed it in my firebase payload.
Addding sound:"default" solved my issue.
Example:
const notificationContent = {
notification: {
icon: "default",
priority:"high",
sound: "default",
android_channel_id: "myChannelID",
title: "new update",
},
data: {//Used on onMessage() function
priority:"high",
android_channel_id: "myChannelID",
click_action: "FLUTTER_NOTIFICATION_CLICK",
title: "new update",
},
};

push Notification in android with android studio

I do the next for call a Notification on android studio
when I send a message out of the app, only show the Icon and the text, but does not play sound or vibration
when I am inside the app, play sound and vibrate.
Any help?
I have on manifest
Method for show notification:
Intent intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
PendingIntent.FLAG_ONE_SHOT);
//Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
Uri defaultSoundUri = Uri.parse("android.resource://"+this.getPackageName()+"/"+R.raw.power);
//Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
Notification.Builder notificationBuilder = new Notification.Builder(this)
.setSmallIcon(R.mipmap.colantaico)
.setContentTitle(Titulo)
.setContentText(messageBody)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setVibrate(new long[] { 1000, 1000, 1000, 1000, 1000 })
.setLights(Color.RED, 3000, 3000)
.setContentIntent(pendingIntent);
Notification mNotification = notificationBuilder.build();
mNotification.flags |= Notification.FLAG_INSISTENT;
mNotification.sound = defaultSoundUri;
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0 /* ID of notification */, mNotification);
In the Manifest
<uses-permission android:name="android.permission.VIBRATE" />
<service
android:name=".RecibidorNotificacionesFCM">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT"/>
</intent-filter>
</service>
private static final int NOTIFICATION_ID = 1;
Intent openIntent = new Intent(this, MainActivity.class);;
PendingIntent contentIntent = PendingIntent.getActivity(this, NOTIFICATION_ID,
openIntent, PendingIntent.FLAG_ONE_SHOT);
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setDefaults(Notification.DEFAULT_ALL)
.setVibrate(new long[]{100, 250, 100, 250, 100, 250})
.setAutoCancel(true)
.setColor(this.getResources().getColor(R.color.activity_toolbar_color))
.setContentTitle("Test Notification")
.setStyle(new NotificationCompat.BigTextStyle()
.bigText(Html.fromHtml("Notification text")))
.setPriority(Notification.PRIORITY_MAX)
.setContentText(Html.fromHtml("Notification text")))
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
mBuilder.setSmallIcon(R.drawable.notification_icon1);
} else {
mBuilder.setSmallIcon(R.drawable.notification_icon);
}
mBuilder.setContentIntent(contentIntent);
mBuilder.setDeleteIntent(LocalNotificationEventReceiver.getDeleteIntent(this));
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = mBuilder.build();
notificationManager.notify(NOTIFICATION_ID, notification);

Android push notification showing text in single line

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());

FCM : I want to group all my notifications into one. How can I achieve this?

I have used FCM to receive notifications. When I receive multiple notifications it fills up the notification status bar. How do I group them into one?
My code:
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
PendingIntent.FLAG_UPDATE_CURRENT);
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setContentTitle("ABC")
.setPriority(Notification.PRIORITY_HIGH)
.setContentText(messageBody)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setGroup(GROUP_KEY_NOTIFICATIONS)
.setContentIntent(pendingIntent);
Using setGroup() it is not working.
Edit:
.setGroupSummary(true)
is working for OS lollipop but not in marshmallow. Can you please help
To group notifications, you have to insert notifications in database and using NotificationCompat.InboxStyle you can group notifications.
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
PendingIntent.FLAG_UPDATE_CURRENT);
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setContentTitle("ABC")
.setPriority(Notification.PRIORITY_HIGH)
.setContentText(messageBody)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setGroup(GROUP_KEY_NOTIFICATIONS)
.setContentIntent(pendingIntent);
NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle();
for (int i = 0; i < data.size(); i++)
{
inboxStyle.addLine(data.get(i));
}
inboxStyle.addLine("");
notificationBuilder.setStyle(inboxStyle);
Where data is list fetched from database containing all notifications.

Categories

Resources