I am creating a Chat Application with Firebase. I send Notifications with Firebase Cloud Functions and recieve them on my phone. But for every message I get a new notification. And now I want to know if it is possible to create a Notification like in WhatsApp, with multible lines for each message in one Notification. My Code for a Notification:
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
String notification_tag = remoteMessage.getData().get("senderUiD");
Intent intent = new Intent(getApplicationContext(), Chat_Room.class);
PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 0, intent, 0);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(getApplicationContext())
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle(remoteMessage.getData().get("username"))
.setContentText(remoteMessage.getNotification().getBody())
.setVibrate(new long[]{1500, 0, 1500, 0})
.setLights(Color.BLUE, 2000, 1000)
.setWhen(remoteMessage.getSentTime())
.setAutoCancel(true)
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setStyle(new NotificationCompat.BigTextStyle().bigText(remoteMessage.getNotification().getBody()))
.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
.setContentIntent(pendingIntent);
notificationManager.notify(notification_tag, 0, notificationBuilder.build());
Thanks for your help ;)
When You Are Receiving Notification Message Your Notification In override last Notification
generate Rendam number
Random ran = new Random();
int notification_tag = r`an.nextInt(6) + 5;`
notificationManager.notify(notification_tag, 0, notificationBuilder.build());
also, fallow few step for grouping notification
NotificationCompat.Builder:contains the UI specification and action information
NotificationCompat.Builder.build() :used to create notification (Which returns Notification object)
Notification.InboxStyle: used to group the notifications belongs to same ID
NotificationManager.notify():to issue the notification.
Related
I'm using fcm for push notification.
when app is open and notification received , notification work correctly.
but when app is closed and notification is recieved, some features like vibrate , sound , addaction , ... not working.
Intent intent = new Intent(this, FullscreenActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);
String urlAddress = "https://cafebazaar.ir/";
Intent intent2 = new Intent(Intent.ACTION_VIEW, Uri.parse(urlAddress));
intent2.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent2.setPackage("com.farsitel.bazaar");
PendingIntent pendingIntent2 = PendingIntent.getActivity(this, 0, intent2, PendingIntent.FLAG_ONE_SHOT);
Uri soundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
Notification notificationBuilder = new Notification.Builder(this)
.setSmallIcon(R.drawable.macanads)
.setContentTitle(title)
.setContentText(message)
.setAutoCancel(true)
.setColor(Integer.parseInt(color))
.setSound(soundUri)
.setVibrate(new long[] { 1000, 1000, 1000})
.addAction(android.R.drawable.ic_menu_directions,"بروزرسانی",pendingIntent2)
.setContentIntent(pendingIntent).build();
}
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, notificationBuilder);
}
My problem has been solved.you must not put JSON key 'notification' in your request to firebase API but instead use 'data'
To make firebase library to call your onMessageReceived() in the following cases
App in foreground
App in background
App has been killed
you must not put JSON key 'notification' in your request to firebase API but instead use 'data', see below.
The following message will not call your onMessageReceived() when your app is in the background or killed, and you can't customize your notification.
I have an online JSON file that shows the latest country earthquakes, the listView adapter in my app receives data from this JSON file and the refresh is manually by user`s swipe, but now I am adding an automatic and timely basis refresh mode to my list, the questions is:
How can I create a system notification after a new quake is detected?
Write this below code for sending System notification in your app while your data is loaded from network request.
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
Notification notification = new Notification.Builder(getApplicationContext())
.setContentTitle("App Name / Title Message")
.setContentText("Description Message hat data is updated")
.setDefaults(Notification.DEFAULT_ALL)
.setSmallIcon(R.drawable.ic_notification_icon)
.setColor(getResources().getColor(R.color.icon_color))
.setPriority(Notification.PRIORITY_HIGH) // will show notification even you are in app
.setContentIntent(pIntent)
.setAutoCancel(true)
.build();
notification.flags |= Notification.FLAG_ONLY_ALERT_ONCE | Notification.FLAG_SHOW_LIGHTS;
NotificationManager notificationManager = (NotificationManager)
getApplicationContext().getSystemService(NOTIFICATION_SERVICE);
notificationManager.notify(1, notification);
//you can use unique identification int number for notificationID instead of 1
putting this code block will show notification.
Just build a Notification like this
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this, CHANNEL_ID)
.setSmallIcon(R.drawable.notification_icon)
.setContentTitle(textTitle)
.setContentText(textContent)
.setPriority(NotificationCompat.PRIORITY_DEFAULT);
And then just show it
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
// notificationId is a unique int for each notification that you must define
notificationManager.notify(notificationId, mBuilder.build());
And for anything other extras check the official documentation
In order for this to work you somehow should push when a new earthquake happens to your phone. If you have backend, you can send a push notification through the cloud messaging.
If you don't have backend just create a background service and pull data every now and then and when data says new earthquake jsut follow the notification creation from above.
I'm working on a movie app that notifies you when there is an upcoming movie. But I'm focused on when someone sends a movie recommendation it shows a notification and when it is clicked it search and display the movie. is that possible? How?
You can use FCM (Firebase Cloud Messaging) to send Push notification from server once user recommend some movie you can send notification through server.
See link for complete detail.
FCM
Intent notificationIntent = new Intent(this, YourActivityWhichYouWantToOpen.class);
notificationIntent.putExtra("movieName", "NameOfRecomendedMoview");
notificationIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.ic_launcher)
.setAutoCancel(true)
.setPriority(2)
.setTicker(title)
.setContentText(body)
.setContentIntent(PendingIntent.getActivity(this, iUniqueId, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT))
.setWhen(System.currentTimeMillis())
.setContentTitle(title)
.setDefaults(Notification.DEFAULT_ALL);
NotificationManager mNotificationManager = (NotificationManager) this
.getSystemService(Context.NOTIFICATION_SERVICE);
builder.setLights(Color.GREEN, 500, 500);
mNotificationManager.notify(MESSAGE_NOTIFICATION_ID, builder.build());
Using new intent in activity you take movie name when user click on notification.
I need display notifications when my server return specific values. I have a service running on android and taskschedule running every time sending request to server, when the server return positive value i need display message on celular display, similar receive message of whatsapp (display icon and notification on display). Anyone have a sample?
I trying this:
PendingIntent resultPendingIntent = PendingIntent.getActivity(
this, 0, resultIntent, PendingIntent.FLAG_UPDATE_CURRENT);
But my application is running as a service.
Please use Using Service to run background and create notification
private void processStartNotification() {
// Do something. For example, fetch fresh data from backend to create a rich notification?
final NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
builder.setContentTitle("Scheduled Notification")
.setAutoCancel(true)
.setColor(getResources().getColor(R.color.colorAccent))
.setContentText("This notification has been triggered by Notification Service")
.setSmallIcon(R.drawable.notification_icon);
PendingIntent pendingIntent = PendingIntent.getActivity(this,
NOTIFICATION_ID,
new Intent(this, NotificationActivity.class),
PendingIntent.FLAG_UPDATE_CURRENT);
builder.setContentIntent(pendingIntent);
builder.setDeleteIntent(NotificationEventReceiver.getDeleteIntent(this));
final NotificationManager manager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
manager.notify(NOTIFICATION_ID, builder.build());
}
As Richard said in this question > Possible to continuously update text in the notification area? , I have the same issue here .
I want to update text in the notification area .
This question is answered and accepted , but the answer doesn't help me .
It's about to create text as bitmap dynamically and set it to Small Icon of notification .
But as Richard commented , the images for setSmallIcon must be predefined in the package.There is no ability to edit them on the fly.
Please kindly show me the right way to do this stuff .
It's quite simple to update your Notification.
First of all when you create it you must create it with an id.
NotificationManager mNotificationManager =(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// Sets an ID for the notification, so it can be updated
int notifyID = 1;
mNotifyBuilder = new NotificationCompat.Builder(this)
.setContentTitle("Title")
.setContentText("Text")
.setSmallIcon(R.drawable.ic_notify);
mNotificationManager.notify(
notifyID,
mNotifyBuilder.build());
Now to update your Notification you only need to notify the new notification with the old id and this will be updated (you don't need to reset the paraneter you don't want to update).
mNotifyBuilder.setContentText(setContentText("New Text")
mNotifyBuilder.setSmallIcon(R.drawable.ic_new_notify);
//this update your notification
mNotificationManager.notify(
notifyID,
mNotifyBuilder.build());
I tried to make one but it has one disadvantage is that, it has one empty notification in notification drawer,
public static int when = 0;
private void generateNotification(Context context) {
Log.i(TAG, "generateNotification");
Intent notificationIntent = new Intent(context, MainActivity.class);
PendingIntent pIntent = PendingIntent.getActivity(
this.getApplicationContext(), 0, notificationIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder mNotification = new NotificationCompat.Builder(
this)
// .setContentTitle("AppName")
// .setContentText("Message For Notification Drawer")
// .setSound(soundUri)
// .setDefaults(Notification.DEFAULT_SOUND)
// .setVibrate(new long[] { 1000, 1000 })
// .addAction(R.drawable.ic_launcher, "View", pIntent)
// .addAction(0, "Remind", pIntent)
// .setNumber(fCount)
// .setWhen(when)
.setSmallIcon(R.drawable.ic_launcher)
.setTicker(Integer.toString(when)) // Set String you want
.setAutoCancel(true)
.setContentIntent(pIntent);
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
// notificationManager.notify(when, mNotification.build());
notificationManager.notify(1, mNotification.build());
when++;
}