Whenever I receive a push notification on Android it replaces any existing notification in the drawer.
any idea to fix that?
Below is my code:
NotificationManager notificationManager = (NotificationManager)
getSystemService(NOTIFICATION_SERVICE);
Intent intent = new Intent(DbInsertService.this, MessageActivity.class);
intent.putExtra("Service",serviceType);
PendingIntent pIntent = PendingIntent.getActivity(DbInsertService.this, (int)
System.currentTimeMillis(), intent,PendingIntent.FLAG_UPDATE_CURRENT);
Notification n = new Notification.Builder(DbInsertService.this)
.setContentTitle("Message from " +serviceType.getSer_name())
.setContentText(messageType.getHis_title())
.setSmallIcon(R.drawable.ic_email_variant_grey600_24dp)
.setContentIntent(pIntent)
.setAutoCancel(true).build();
notificationManager.notify(0, n);
thanks
Set different Id for different notification.
notificationManager.notify(0, n); // here set different Id.
You're sending them all with the same id. Send different ids and they won't overwrite.
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.
I am developing apps that can perform sending fake sms and receiving fake text message. It's better If I can use built-in SMS notification but I don't know the way. So I use my below code to generate Notification and when I click on that Notification nothing happens. Please can anyone solve my problem? I need to open Message Inbox when I click that notification. Any help would be appreciated!
Uri sms_uri = Uri.parse("smsto:+92xxxxxxxx");
Intent sms_intent = new Intent(Intent.ACTION_SENDTO, sms_uri);
sms_intent.putExtra("sms_body", "Good Morning ! how r U ?");
PendingIntent contentIntent = PendingIntent.getService(context, 0, sms_intent, 0);
Resources res = context.getResources();
Notification noti = new Notification.Builder(context)
.setContentTitle(res.getString(R.string.app_name))
//.setContentText(res.getString(R.string.cancelText))
.setSmallIcon(android.R.drawable.ic_dialog_email)
.setTicker(res.getString(R.string.app_name))
// .setAutoCancel(true)
.setWhen(System.currentTimeMillis())
.setContentIntent(contentIntent)
.build();
NotificationManager notificationManager = (NotificationManager) getActivity().getSystemService(NOTIFICATION_SERVICE);
noti.flags |= Notification.FLAG_AUTO_CANCEL;
notificationManager.notify(0, noti);
Use
PendingIntent.getActivity(context, 0 , sms_intent, 0);
that may help!!!
I am using the following code for displaying notification . Upon displaying the notification , I go to the activity if I click in the notifications .
void Notify(String notificationTitle, String notificationMessage){
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(R.drawable.noti, notificationTitle,System.currentTimeMillis());
Intent notificationIntent = new Intent(this, SmsActivity.class);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
| Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent intent = PendingIntent.getActivity(this, 0,
notificationIntent, 0);
notification.setLatestEventInfo(this, notificationTitle,notificationMessage, intent);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notificationManager.notify(0, notification);
}
Now I want to update the text of notifications . How can I do that ?
You can try this
//First time
NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
.setContentText(context.getString(R.string.notif_text))
.setContentTitle(title)
.setSmallIcon(R.drawable.ic_action_alarm_2)
.setAutoCancel(false)
.setOngoing(running)
.setOnlyAlertOnce(true)
.setContentIntent(
PendingIntent.getActivity(context, 10,
new Intent(context, YourActivity.class)
.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP),
0)
)
notificationManager.notify(id, builder.build());
//Second time
builder.setContentTitle(title);
notificationManager.notify(id, builder.build());
Send notification with same id:
https://developer.android.com/training/notify-user/managing.html
You can update the text of notification by sending different text in Notify(String notificationTitle, String notificationMessage) method.
The problem in approach suggested here is that if user already responded to this notification he could see it again, so you'd better to show it once instead.
So I wouldn't recommend to use this API at all in order to update your notifications.
If you are using it for that purpose you probably just exchange one bug for another.
Instead of passing 0 as notification ID, send some other number and trigger another notification with the same ID.
The existing one will be updated.
This is the code that gives notification on start of service
NotificationCompat.Builder mbuild = new NotificationCompat.Builder(getApplicationContext());
Intent in = new Intent(getApplicationContext(), MainActivity.class);
PendingIntent resultIN = PendingIntent.getActivity(getApplicationContext(),code,in,NOTIFICATION_COUNT); mbuild.setSmallIcon(R.drawable.images1);
mbuild.setContentText(NOTIFICATION_COUNT +" New Message");
mbuild.setContentIntent(resultIN); //mbuild.addAction(R.drawable.notifications,NOTIFICATION_COUNT +"New Messages",resultIN);
NotificationManager nmagr = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
nmagr.notify(1, mbuild.build());
everyting is working correct ..the code opens the target activity but the notification still stays there in the notification bar.
i have tried useing mbuil.setautocancel(true); but its doing nothing
try this
NotificationManager nmagr = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Notification notification=mbuild.build();
notification.flags = Notification.FLAG_AUTO_CANCEL;
nmagr.notify(1,notification);
You didn't set setAutoCancel(true)
Just set this
mbuild.setAutoCancel(true)
or
mbuild.getNotification().flags |= Notification.FLAG_AUTO_CANCEL
Updated
You can also try below code.
NotificationManager mgr = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
mgr.cancel(1); // here is "1" is your notification id which you set at "nmagr.notify(1, mbuild.build());"
write above code in your onCreate() method of MainActivity.class.
NotificationManager notification_manager = (NotificationManager) getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);
notification_manager.cancel(NOTIFICATION_ID);
Just an update to anyone doing this with NotificationCompat and / or using the Notificaitoncompat.Builder.
This is how I did mine :
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
builder.setContentTitle(title);
/* your own code here */
builder.setAutoCancel(true);
Notification notification = builder.build();
NotificationManagerCompat.from(this).notify(0,notification);
It is important to note that if you use a pending intent to redirect the user to a specific intent in your app, this will also call the pending intent.
As per the documentation :
Setting this flag will make it so the notification is automatically canceled when the user clicks it in the panel. The PendingIntent set with setDeleteIntent will be broadcast when the notification is canceled.
just a simple question: How to make the single-line notification that will disappear after a short time.
Just like Whatsapp's:
My current notification code is still very basic:
Intent intent = new Intent("com.example.MyChat");
PendingIntent pIntent = PendingIntent.getActivity(context, 0, intent, 0);
Notification notification = new Notification.Builder(context)
.setContentTitle("New message from " + name)
.setContentText(message)
.setSmallIcon(R.drawable.ic_launcher).setContentIntent(pIntent)
.getNotification();
notification.defaults |= Notification.DEFAULT_SOUND;
NotificationManager notificationManager = (NotificationManager) context
.getSystemService(context.NOTIFICATION_SERVICE);
notificationManager.notify(0, notification);
Thanks
This is called the "ticker" and it is shown briefly by the status bar if you set the tickerText property of your Notification.
See setTickerText.
You can use AlarmManager to invoke after a short time, say 5 seconds.
Also you can use a service, which gets launched, when first time the notification appears and it could cancel the notification after some time.
here you can cancel Notification using,
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.cancel(NOTFICATION_ID);
Have a look at this post, it might help you as well.