Double notification sound for bundled notification on Android O - android

I am trying to implement bundled notification. After going through lots of tutorials and blogs, I got to know that I have to generate two notifications. One is regular notification, another one is summary notification. I followed everything as stated on those blog posts. Everything seems to work. But I get double notification sound for each notification on Android O. I am not able to fix this issue anyway. I have searched for any similar issue that other people might have faced. But I haven't found anything helpful.
Below are some code snippet of generating notification
Regular Notification
public Notification getSmallNotification(String channelId, String title, String body, Intent intent) {
PendingIntent resultPendingIntent =
PendingIntent.getActivity(
mContext,
ID_SMALL_NOTIFICATION,
intent,
PendingIntent.FLAG_UPDATE_CURRENT
);
NotificationCompat.Builder builder = new NotificationCompat.Builder(mContext, channelId);
builder.setTicker(title)
.setWhen(System.currentTimeMillis())
.setAutoCancel(true)
.setContentIntent(resultPendingIntent)
.setContentTitle(title)
.setContentText(body)
.setSmallIcon(R.drawable.ic_gw_notification)
.setColor(ContextCompat.getColor(mContext, R.color.color_bg_splash))
.setGroup(channelId);
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N) {
builder.setDefaults(Notification.DEFAULT_SOUND);
}
Notification notification = builder.build();
notification.flags |= Notification.FLAG_AUTO_CANCEL;
return notification;
}
Summary Notification
public Notification getSummaryNotification(String channelId, String title, String body) {
NotificationCompat.Builder builder = new NotificationCompat.Builder(mContext, channelId)
.setContentTitle(title)
.setContentText(body)
.setSmallIcon(R.drawable.ic_gw_notification)
.setColor(ContextCompat.getColor(mContext, R.color.color_bg_splash))
.setShowWhen(true)
.setGroup(channelId)
.setGroupSummary(true);
return builder.build();
}
Then I call this two functions simultaneously
notification = gwNotificationManager.getSmallNotification(channelId, title, body, intent);
notificationUtils.getManager().notify(channelId, (int) uniqueId, notification);
Notification summaryNotification = gwNotificationManager.getSummaryNotification(channelId, groupTitle, groupBody);
notificationUtils.getManager().notify(channelId, 0, summaryNotification);
How can I resolve the double sound issue? Any help would be much appreciated!

Another solution is to set in the builder:
setGroupAlertBehavior(NotificationCompat.GROUP_ALERT_SUMMARY)
See documentation

I have the same problem on Android 8+ and I solved it by creating additional channel with low importance:
manager.createNotificationChannel(new NotificationChannel(silentChannelId, name, NotificationManager.IMPORTANCE_LOW));
And then creating summary notification using this channel:
new NotificationCompat.Builder(context, silentChannelId)

Related

Android 11 - Foreground service notification isn't visible

I have an android app that makes use of Foreground Services. My foreground service is supposed to display a notification.
I have updated one of my devices to Android 11 and the foreground service's notification is not being displayed. The foreground notification works as expected and is visible on all previous versions.
My code for starting a foreground service with a notification is as follows:
String channelName = "MyChannel";
NotificationChannel chan = new NotificationChannel("com.my.package", channelName, NotificationManager.IMPORTANCE_NONE);
chan.setLightColor(Color.BLUE);
chan.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.createNotificationChannel(chan);
RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.my_layout);
remoteViews.setImageViewResource(R.id.my_notification_id, R.drawable.my_icon);
remoteViews.setTextViewText(R.id.my_title, "Title");
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(context, "com.my.package");
notificationBuilder.setOngoing(true)
.setSmallIcon(R.drawable.ic_image_icon)
.setContentTitle("My Title")
.setPriority(NotificationManager.IMPORTANCE_MIN)
.setCategory(Notification.CATEGORY_SERVICE)
.setStyle(new android.support.v4.media.app.NotificationCompat.DecoratedMediaCustomViewStyle()
.setMediaSession(new MediaSessionCompat(context, MY_TAG).getSessionToken()))
.setColor(0x169AB9)
.setWhen(System.currentTimeMillis())
.setOnlyAlertOnce(true)
.setColorized(true);
PendingIntent contentIntent = PendingIntent.getActivity(context, MYMConstants.NOTIFICATION_GO_TO_ACTIVITY_REQUEST_CODE,
new Intent(context, MainActivity.class), PendingIntent.FLAG_UPDATE_CURRENT);
notificationBuilder.setContent(remoteViews);
notificationBuilder.setContentIntent(contentIntent);
Notification notification = notificationBuilder.build();
startForeground(10000, notification);
The foreground service is started as follows:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
context.startForegroundService(serviceIntent);
} else {
context.startService(serviceIntent);
}
I searched for the official documentation on Android 11 and underlying changes to Foreground services, but I couldn't find anything. Could someone help me out with this?
Nothing has changed the way how notifications are added to Foreground Service as part of Android 11.
I did find where the problem lies though. This piece of code above was causing the problem.
.setStyle(new android.support.v4.media.app.NotificationCompat.DecoratedMediaCustomViewStyle()
.setMediaSession(new MediaSessionCompat(context, MY_TAG).getSessionToken()))
Removing this fixed the issue.
To utilize setStyle(), we need to replace android.support.v4.media.app by androidx.media.app.

Open particular activity/fragment after clicking on Firebase notification when app is closed

I know this question seems duplicate, but for my requirement, I have searched many posts online, but nothing worked for me.
My requirement
I'm using the Firebase to get the push notifications. When app was opened means everything working fine but my problem is, app is in background/closed if any push notification came means i want to open the particular activity or fragment when clicking on that notification, but it always opening the launcher/main activity.
For this, I have tried the following scenarios
Scenario 1
Used the bundle to transfer notification data from FirebaseMessagingService to the launcher/Main activity and based on the bundle data i'm redirecting to the particular activity/fragment
Scenario 2
By using Sharedpreference
Scenario 3
By using intent.putExtra
following the above scenarios everything working fine when app was opened but if my app was closed means it always redirecting to the main/launcher activity
My code
Intent intent = new Intent(this, MainActivity.class);
intent.putExtra("reqTo", messageBody);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
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, notificationBuilder.build());
So, does anyone know how to open the particular activity/Fragment when clicking on the Firebase push notification when the app is closed.
public void showNotificationMessage(final String title, final String message, Intent intent) {
// Check for empty push message
if (TextUtils.isEmpty(message))
return;
// notification icon
final int icon = R.drawable.logo;
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
final PendingIntent resultPendingIntent =
PendingIntent.getActivity(
mContext,
0,
intent,
PendingIntent.FLAG_CANCEL_CURRENT
);
final NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
mContext);
final Uri alarmSound = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE
+ "://" + mContext.getPackageName() + "/raw/notification");
showSmallNotification(mBuilder, icon, title, message, resultPendingIntent, alarmSound);
playNotificationSound();
}
private void showSmallNotification(NotificationCompat.Builder mBuilder, int icon, String title, String message, PendingIntent resultPendingIntent, Uri alarmSound) {
NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle();
inboxStyle.addLine(message);
Notification notification;
notification = mBuilder.setSmallIcon(icon).setTicker(title).setWhen(0)
.setAutoCancel(true)
.setContentTitle(title)
.setContentIntent(resultPendingIntent)
.setSound(alarmSound)
.setStyle(inboxStyle)
.setSmallIcon(R.drawable.logo)
.setLargeIcon(BitmapFactory.decodeResource(mContext.getResources(), icon))
.setContentText(message)
.build();
NotificationManager notificationManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(Config.NOTIFICATION_ID, notification);
}
private void showBigNotification(Bitmap bitmap, NotificationCompat.Builder mBuilder, int icon, String title, String message, PendingIntent resultPendingIntent, Uri alarmSound) {
NotificationCompat.BigPictureStyle bigPictureStyle = new NotificationCompat.BigPictureStyle();
bigPictureStyle.setBigContentTitle(title);
bigPictureStyle.setSummaryText(Html.fromHtml(message).toString());
bigPictureStyle.bigPicture(bitmap);
Notification notification;
notification = mBuilder.setSmallIcon(icon).setTicker(title).setWhen(0)
.setAutoCancel(true)
.setContentTitle(title)
.setContentIntent(resultPendingIntent)
.setSound(alarmSound)
.setStyle(bigPictureStyle)
.setSmallIcon(R.drawable.logo)
.setLargeIcon(BitmapFactory.decodeResource(mContext.getResources(), icon))
.setContentText(message)
.build();
NotificationManager notificationManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(Config.NOTIFICATION_ID_BIG_IMAGE, notification);
}
You can check app closed or not like this
if (!NotificationUtils.isAppIsInBackground(getApplicationContext())) {
This is very similar to my answer given here: https://stackoverflow.com/a/41441631/1291714
In summary though, you need to use Firebase Cloud Messaging and not Firebase Notifications in order to receive a message in the background and do custom processing. You need to send "data" messages to the client and then in your service, you will then always receive the callback, whether the app is in the foreground or background.
With Firebase Notifications, you will only receive the callback when the app is in the Foreground. When the app is in the background, the system will handle and display the notification for a user. You won't be able to customise this notification to open up a different intent.
Read more here: https://firebase.google.com/docs/notifications/android/console-audience

How to stack multiple push notifications using GCM Client?

I want to stack notifications from a news app. Right now I get 3 different icons in the status bar for 3 notifications. I am following this guide from google to implement the GCM Client.
https://developer.android.com/google/gcm/client.html
Heres my code:
public class GcmIntentService extends IntentService {
....
private void sendNotification(String title, String body) {
....
PendingIntent pIntent = TaskStackBuilder.create(context).addNextIntentWithParentStack(intent).getPendingIntent(requestId, flags);
Notification mBuilder = new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.ic_stat_notify)
.setContentTitle(title)
.setContentText(body)
.setContentIntent(pIntent)
.setAutoCancel(true).build();
NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify((int) (new Date().getTime()/1000), mBuilder);
}
}
I have tried playing around with setGroup and NotificationCompat.InboxStyle but that didn't work either since a new instance of the service seems to be getting created every time I receive a notification.
Thanks in advance for your help.

it is impossible to group notifications

I use a notice in my project. I want to group notifications. Please tell me how to do it? I tried to do it by example enter link description here
but nothing came of it. each notification is displayed separately.
here is my code:
private static int id =0;
final static String GROUP_KEY_GUEST = "group_key_guest";
...
private void generateNotification(Context context, String title, String message) {
if(notificationManager==null){
notificationManager = NotificationManagerCompat.from(context);
}
Intent intent = new Intent(context,MyActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
Notification notification = new NotificationCompat.Builder(context)
.setContentIntent(pendingIntent)
.setSmallIcon(R.drawable.ic_stat_gcm)
.setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.drawable.ic_stat_gcm))
.setTicker("Новое сообщение")
.setWhen(System.currentTimeMillis())
.setAutoCancel(true)
.setContentTitle(title)
.setContentText(message)
.setGroup(GROUP_KEY_GUEST)
.setGroupSummary(true)
.build();
notificationManager.notify(id++, notification);
}
#Override
protected void onMessage(Context context, Intent intent) {
String title = intent.getStringExtra("title");
String message = intent.getStringExtra("content");
generateNotification(context,title, message);
}
the link that you posted in question is related to grouping the notifications on wearable devices not on mob/tab devices. to group your notifications into one notification on mob/tab use BigTextStyle http://developer.android.com/reference/android/app/Notification.BigTextStyle.html
Perhaps the issue is that group notifications just support Android L. Please make sure your device version is Android L(20). Or you can run the project on an android L phone, then try it again.

Is possible set Expanded Notification as default in Big Text Notifications?

I followed this Sample Code
In Big Text Notifications section, he said that need expand to see Big text notification form, as image im below :
I wonder that we can not set Expanded Notification as default in Big Text Notifications?
People who know it is can or not,
If can,
Please tell me how to do it,
Thanks,
The documentation states:
A notification's big view appears only when the notification is
expanded, which happens when the notification is at the top of the
notification drawer, or when the user expands the notification with a
gesture.
So my answer is no, you can't expand it by default.
There is however a trick to push the notification to the top of the list where it would be expanded. Simply set the Priority to Notification.PRIORITY_MAX and chances are that your app's notification will make it to the top.
Notification noti = new Notification.Builder()
... // The same notification properties as the others
.setStyle(new Notification.BigPictureStyle().bigPicture(mBitmap))
.build();
You change
.setStyle(new NotificationCompat.BigTextStyle().bigText(th_alert))
along with the announcement
notification = new NotificationCompat.Builder(context)
Here is an example:
You can set Code
Intent intent = new Intent(context, ReserveStatusActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
NotificationManager notificationManager =
(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
intent = new Intent(String.valueOf(PushActivity.class));
intent.putExtra("message", MESSAGE);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
stackBuilder.addParentStack(PushActivity.class);
stackBuilder.addNextIntent(intent);
// PendingIntent pendingIntent =
stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
// android.support.v4.app.NotificationCompat.BigTextStyle bigStyle = new NotificationCompat.BigTextStyle();
// bigStyle.bigText((CharSequence) context);
notification = new NotificationCompat.Builder(context)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle(th_title)
.setContentText(th_alert)
.setAutoCancel(true)
// .setStyle(new Notification.BigTextStyle().bigText(th_alert) ตัวเก่า
// .setStyle(new NotificationCompat.BigTextStyle().bigText(th_title))
.setStyle(new NotificationCompat.BigTextStyle().bigText(th_alert))
.setContentIntent(pendingIntent)
.setNumber(++numMessages)
.build();
notification.sound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
notificationManager.notify(1000, notification);
notificationBuilder.setStyle(new NotificationCompat.BigTextStyle().bigText("Your Long Text here"))
simply setStyle of your notification builder.
No need of doing any kind of modification in the any file for showing of multiple lines of Notification while using Push Notification V5, remove the field "style", from the object you are sending. Automatically the Multiple lines of notification will be seen. For more information, upvote the answer, ask your query. I will help you out.
For reference, visit this question
From this notification code you have got images, and big text or more.
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context);
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
mBuilder.setSmallIcon(R.drawable.small_logo);
mBuilder.setColor(Color.parseColor("#D74F4F"));
} else {
mBuilder.setSmallIcon(icon);
}
mBuilder.setTicker(title).setWhen(when);
mBuilder.setAutoCancel(true);
mBuilder.setContentTitle(title);
mBuilder.setContentIntent(intent);
mBuilder.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION));
mBuilder.setLargeIcon(BitmapFactory.decodeResource(context.getResources(), icon));
mBuilder.setContentText(msg);
mBuilder.setPriority(Notification.PRIORITY_MAX);
if (Utils.validateString(banner_path)) {
mBuilder.setStyle(notiStyle);
} else {
mBuilder.setStyle(new NotificationCompat.BigTextStyle().bigText(msg));
}
Notification noti = mBuilder.build();
notificationManager.notify(0, noti);

Categories

Resources