I wrote the below method for showing FCM notification on my android 5.1 device. When I run the code inside a FirebaseMessagingService it is just giving single line notification , where if I run the same code inside my Activity its giving expandable notifications.
I basically need my long FCM text notifications to get expanded on notifications rather than showing partial notification text.
Any leads?
private void showNotif(String messageBody){
Intent intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);
// Constructs the Builder object.
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(getApplicationContext())
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle(getString(R.string.app_name))
.setContentText(messageBody)
.setDefaults(Notification.DEFAULT_ALL) // requires VIBRATE permission
.setAutoCancel(true)
.setContentIntent(pendingIntent)
/*
* Sets the big view "big text" style and supplies the
* text (the user's reminder message) that will be displayed
* in the detail area of the expanded notification.
* These calls are ignored by the support library for
* pre-4.1 devices.
*/
.setStyle(new NotificationCompat.BigTextStyle()
.bigText(messageBody));
// android.support.v4.app.NotificationManagerCompat mNotifManager = (NotificationManagerCompat) getSystemService(Context.NOTIFICATION_SERVICE);
NotificationManager mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// mId allows you to update the notification later on.
mNotificationManager.notify(0, mBuilder.build());
}
This should work .
Big Text notifcations are shown in normal mode when there are other notifications on top . Try to expand it while testing.
You need to creare notification using this method https://fcm.googleapis.com/fcm/send and not using firebase console and all would be ok
Related
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.
Short version:
I want to create a notification that is uninterruptible by all other notifications, SMS messages, etc until the user clears it.
Long version:
I'm using Firebase Cloud Messaging to send alerts to my phone. Messages are handled based on the topic, and I need to make the "alarm" messages repeat continuously until the notification is cleared by the user. This is currently accomplished by setting the notification with FLAG_INSISTENT, which loops the sound.
The problem is the insistent "alarm" notification is permanently stopped when a different notification or SMS comes through.
I would like to ignore all new notifications, or restart the alarm after the new notification plays. I've searched for a couple hours and tried several flags and settings, but can't figure out how to do it.
Here is the code I'm using to handle FCM messages and set the notifications:
public class MyFirebaseMessagingService extends FirebaseMessagingService {
#Override
public void onMessageReceived(RemoteMessage remoteMessage) {
if (remoteMessage.getData() != null){
String messageTopic= remoteMessage.getFrom();
String messageTitle = remoteMessage.getData().get("title");
String messageBody = remoteMessage.getData().get("message");
if (messageTopic.equals("/topics/news")) {
Intent intent = new Intent(this, MainScreenActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
Uri soundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_stat_news)
.setContentTitle(messageTitle)
.setContentText(messageBody)
.setAutoCancel(true)
.setSound(soundUri)
.setContentIntent(pendingIntent)
.setPriority(Notification.PRIORITY_MAX);
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(30548, notificationBuilder.build());
}
else if (messageTopic.equals("/topics/alarm")) {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse("http://www.example.com/alarm.php"));
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
Uri soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_stat_alarm)
.setContentTitle(messageTitle)
.setContentText(messageBody)
.setAutoCancel(true)
.setSound(soundUri)
.setContentIntent(pendingIntent)
.setPriority(Notification.PRIORITY_MAX);
Notification mNotification = notificationBuilder.build();
mNotification.flags |= Notification.FLAG_INSISTENT;
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(30549, mNotification);
}
}
}
I may be able to prevent my own notifications from interrupting the alarm by ignoring them when the alarm notification ID (30549) is active (or create the new notification and then create the alarm again). However, SMS and notifications from other programs will still interrupt, so that wouldn't be a perfect solution.
I don't think you'll have luck with this, unfortunately. Android will not let you have a notification that takes full priority from my understanding.
However, in the Firebase console when you go to the notifications section and click advanced you can set the priority of a notification to High and set a sound.
Since it looks like you're writing the notification programmatically and setting priority to Max this will achieve the same affect.
Setting priority to high, from Google's documentation:
When a high-priority notification arrives (see right), it is presented to users for a short period of time with an expanded layout exposing possible actions.
After this period of time, the notification retreats to the notification shade. If a notification's priority is flagged as High, Max, or full-screen, it gets a heads-up notification
Also check out this SO for someone who's notification wouldn't go away when they wanted it to
here
On receiving a message from GCM, I'm generating a notification
/**
* Create and show a simple notification containing the received GCM message.
*
* #param message GCM message received.
*/
private void sendNotification(String message) {
Intent intent = new Intent(this, MyActivity.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);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_action_chat)
.setContentTitle("GCM Message")
.setContentText(message)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
}
The notification is generated, the sound gets played, but it does not get shown at top of screen. The notification is displayed in the quick settings/notifications drop down menu, but at the time when I generate it, it is not displayed at the top of screen, only sound is played.
I want to display it at top of screen when I generate it. How can I do it?
it's a headsup notification works from android lollipop here you check how to show notification you can see here http://developer.android.com/guide/topics/ui/notifiers/notifications.html and for headsup you have to set notification priority as below
.setPriority(Notification.PRIORITY_HIGH)
EDIT Nov-17
Notification.PRIORITY_HIGH was deprecated in API level 26. use IMPORTANCE_HIGH instead.
In my case, I had to do
.setSmallIcon(R.drawable.my_icon)
I am creating an application with a notification at different times. First notification display fine in the notification bar with an icon, title and text. But when the second notification comes the title and text of the first notification gets cleared and the second one displays. For the third notification again the second one's title and text gets cleared and so on.
The code i am using is
Uri uri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
long mNotificationId = System.currentTimeMillis();
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher)
.setSound(uri);
NotificationCompat.BigTextStyle bigTextStyle = new NotificationCompat.BigTextStyle();
bigTextStyle.setBigContentTitle("Time to Sleep!!!");
bigTextStyle.bigText("A good laugh and a long sleep are the best cures in the doctor's book.");
mBuilder.setStyle(bigTextStyle);
mBuilder.getNotification().flags |= Notification.FLAG_AUTO_CANCEL;
// Gets an instance of the NotificationManager service
NotificationManager mNotifyMgr = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
// Builds the notification and issues it.
mNotifyMgr.notify((int)mNotificationId, mBuilder.build());
Any help would be appreciated. Thanks in advance.
I think you are only setting text for BigTextStyle
Also set Values for small or default settings
for eg
// Small - default style
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(context)
.setDefaults(Notification.DEFAULT_ALL)
.setSmallIcon(context.getApplicationInfo().icon)
.setWhen(System.currentTimeMillis())
.setContentTitle(extras.getString("title"))
.setTicker(extras.getString("title"))
.setContentIntent(contentIntent);
Just check the mNotificationId value if its same
If same you can use the following to generate the random number
Random random = new Random();
int m = random.nextInt(9999 - 1000) + 1000;
and then use this m
mNotifyMgr.notify(m, mBuilder.build());
Try using
int notification_id = (int) date.getTime();
to ensure that every time a unique id is generated. Notifications with unique ids do not get over written and you can see multiple notifications in the notification panel. Hope this helps.
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);