how to hide data/message of push notification in android - android

I have successfully implement the fcm notification using java server but I want to show only one string in notification instead of all message so how to hide data and send only some data of message.
for e.g "refresh data common" I want to show only refresh data in notification message and hide common but common word requires on app side.
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.space)*/
.setLargeIcon(BitmapFactory.decodeResource(mContext.getResources(), icon))
.setContentText(message)
.build();
NotificationManager notificationManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(Config.NOTIFICATION_ID, notification);
}
In this code I have pass only "refresh data" but it show all"{message=refresh data common}".

Related

How to change "Category" in Oreo onwards notification?

Below is the Settings screen of Facebook app. In my app it displays "Category", here it is "Kalis Martinez". I want to achieve that in my app.
What my app is displaying is as below:
What I am doing is as below.
NotificationManager manager = (NotificationManager)
getSystemService(Context.NOTIFICATION_SERVICE);
manager.createNotificationChannelGroup(new
NotificationChannelGroup(groupId, groupName));
String GROUP_KEY_WORK_EMAIL = "com.android.example.WORK_EMAIL";
Notification notification = new Notification.Builder(getApplicationContext(), SECONDARY_CHANNEL)
.setContentTitle(title)
.setContentText(body)
//build summary info into InboxStyle template
.setStyle(new Notification.InboxStyle()
.addLine("Alex Faarborg Check this out")
.addLine("Jeff Chang Launch Party")
.setBigContentTitle("2 new messages")
.setSummaryText("janedoe#example.com"))
//specify which group this notification belongs to
.setGroup(GROUP_KEY_WORK_EMAIL)
//set this notification as the summary for the group
.setGroupSummary(true)
.setSmallIcon(getSmallIcon())
.setAutoCancel(true);
manager.notify(id, notification.build());
setGroup() and setGroupSummary() do grouping while displaying notifications in System tray.
How to change that label?
Use this for create a notification Group to change title like this
// The id of the group.
String groupId = "my_group_01";
// The user-visible name of the group.
CharSequence groupName = getString(R.string.group_name);
NotificationManager mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.createNotificationChannelGroup(new NotificationChannelGroup(groupId, groupName));
Or check about Notification Channels

Android - notification does not appear in status bar

When starting an IntentService to upload a file in the background, I want to show a notification to the user that the upload is in progress by calling showNotification() from inside my service:
private void showNotification() {
Notification notification = new Notification.Builder(this)
.setSmallIcon(R.drawable.ic_cloud_upload_black_24dp)
.setWhen(System.currentTimeMillis())
.setContentTitle("Uploading")
.setContentText("Your upload is in progress.")
.setOngoing(true)
.build();
mNotificationManager.notify(NOTIFICATION_ID, notification);
}
Now here's my problem: the notification appears on the lock screen, but not in the status bar when the screen is unlocked. What am I missing?
Deployment target is API level 24, so a missing NotificationChannel should not be the cause.
try it :
PendingIntent pendingIntent = PendingIntent.getActivity(getmContext(), 0, new Intent(getmContext(), MainActivity.class), 0);
android.app.Notification pp = new NotificationCompat.Builder(getmContext())
.setSmallIcon(R.drawable.ic_launcher_background)
.setContentText(getMessage())
.setContentIntent(pendingIntent)
.build();
NotificationManager notificationManager = (NotificationManager) getmContext().getSystemService(NOTIFICATION_SERVICE);
notificationManager.notify(0, pp);
This is my code which is working fine with target SDK 25
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
mContext);
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)
.setStyle(inboxStyle)
.setSmallIcon(R.mipmap.ic_launcher)
.setLargeIcon(BitmapFactory.decodeResource(mContext.getResources(), icon))
.setContentText(message)
.build();
NotificationManager notificationManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(NOTIFICATION_ID, notification);
where NOTIFICATION_ID
public static final int NOTIFICATION_ID = 100;
I feel so dumb. Turns out the notification was displayed all the time, but with a black icon on black background.
Changed the icon colour in the xml from
android:fillColor="#FF000000"
to
android:fillColor="#FFFFFFFF"
and it works like a charm

Not able to add same Users message in Same group of Android Nougat Push Notifications as whatsapp

Here I'm trying to show chat messages in groups like whatsapp does in Android 7.0 or more but they're not getting displayed like that. I'm sending local push notifications by getting chat messages from Node.js. In android, I'm using socket client to fetch them and sending push notifications to the other user if their apps are in background. Multiple messages are going in one group but when I try to create a new group when some one else send messages its adding in a same group.
It just updates the title of push notification as I'm doing here:
public static void bundledNotification(String message, String name, String uId) {
Context context = ApplicationClass.context();
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context);
++numberOfBundled;
issuedMessages.add(message);
//Build and issue the group summary. Use inbox style so that all messages are displayed
NotificationCompat.Builder summaryBuilder = new NotificationCompat.Builder(context)
.setContentTitle(name)
.setContentText(message)
.setSmallIcon(R.mipmap.ic_launcher)
.setGroupSummary(true)
.setGroup(uId);
NotificationCompat.InboxStyle inboxStyle =
new NotificationCompat.InboxStyle();
inboxStyle.setBigContentTitle("Messages:");
for (CharSequence cs : issuedMessages) {
inboxStyle.addLine(cs);
}
summaryBuilder.setStyle(inboxStyle);
notificationManager.notify(NOTIFICATION_BUNDLED_BASE_ID, summaryBuilder.build());
//Pending Intent
Intent notificationIntent = new Intent(context, ChatMessageActivity.class);
notificationIntent.putExtra("uid", Integer.parseInt(uId));
notificationIntent.putExtra(context.getString(R.string.notify_id), Integer.parseInt(uId));
TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
// Adds the back stack
stackBuilder.addParentStack(ChatMessageActivity.class);
// Adds the Intent to the top of the stack
stackBuilder.addNextIntent(notificationIntent);
PendingIntent contentIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
//issue the Bundled notification. Since there is a summary notification, this will only display
//on systems with Nougat or later
NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
.setContentTitle(name)
.setContentText(message)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentIntent(contentIntent)
.setGroupSummary(true)
.setStyle(new NotificationCompat.BigTextStyle()
.bigText(name))
.setGroup(uId);
//Each notification needs a unique request code, so that each pending intent is unique. It does not matter
//in this simple case, but is important if we need to take action on a specific notification, such as
//deleting a message
int requestCode = NOTIFICATION_BUNDLED_BASE_ID + numberOfBundled;
if (messagesMap.get(uId) != null) {
messagesMap.get(uId).add(message);
} else {
ArrayList<String> messageList = new ArrayList<String>();
messageList.add(message);
messagesMap.put(uId, messageList);
notificationManager.notify(NOTIFICATION_BUNDLED_BASE_ID + numberOfBundled, builder.build());
}
AppPreferences.setChatNotificationNumber(context, AppUtils.PUSH_CHATS_NOTIFICATION_NUMBER, requestCode);
NotificationCompat.Builder childBuilder = new NotificationCompat.Builder(context)
.setContentTitle(name)
.setContentText(message)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentIntent(contentIntent)
.setStyle(new NotificationCompat.BigTextStyle()
.bigText(message))
.setGroup(uId);
notificationManager.notify(requestCode,
childBuilder.build());
}
From above code it's always creating a one more notification at first time and second time it starts adding messages into old notification. But when I'm trying to add old messages into the thread of same user it creates a new message under main group of notification. Thanks in advance.
Use same setGroup and notification(NOTIFICATION_BUNDLED_BASE_ID) id(group wise)
Context context = ApplicationClass.context();
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context);
++numberOfBundled;
issuedMessages.add(message);
//Build and issue the group summary. Use inbox style so that all messages are displayed
NotificationCompat.Builder summaryBuilder = new NotificationCompat.Builder(context)
.setContentTitle(name)
.setContentText(message)
.setSmallIcon(R.mipmap.ic_launcher)
.setGroupSummary(true)
.setGroup(1001);
NotificationCompat.InboxStyle inboxStyle =
new NotificationCompat.InboxStyle();
inboxStyle.setBigContentTitle("Messages:");
for (CharSequence cs : issuedMessages) {
inboxStyle.addLine(cs);
}
summaryBuilder.setStyle(inboxStyle);
notificationManager.notify(1001, summaryBuilder.build());

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

Group notifications on Android

I want to show notifications like on picture. If there is more than one, I want to show a counter too. I didn't find info in official doc. Now I just update my notification by id:
((NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE))
.notify(PUSH_NOTIFICATION_ID, notification);
How can I do it ?
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(getApplicationContext());
mBuilder.setSmallIcon(R.mipmap.ic_launcher);
mBuilder.setContentTitle(topic);
mBuilder.setContentText(new String(message.getPayload()));
mBuilder.setAutoCancel(true);
mBuilder.mNumber = 1;//get your number of notification from where you have save notification
NotificationManager mNotifyMgr = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
mNotifyMgr.notify(notify_id, mBuilder.build());
To create a stack, call setGroup() for each notification you want in the stack and specify a group key.
final static String GROUP_KEY_EMAILS = "group_key_emails";
// Build the notification, setting the group appropriately
Notification notif = new NotificationCompat.Builder(mContext)
.setContentTitle("New mail from " + sender1)
.setContentText(subject1)
.setSmallIcon(R.drawable.new_mail)
.setGroup(GROUP_KEY_EMAILS)
.build();
// Issue the notification
NotificationManagerCompat notificationManager =
NotificationManagerCompat.from(this);
notificationManager.notify(notificationId1, notif);
Reference: https://developer.android.com/training/wearables/notifications/stacks.html

Categories

Resources