Handling clicks on notification button and the notification part itself in android - android

I've a notification which says "Now Playing : Radio " and it has a stop button below this. When the user clicks on stop button, the audio stops and now what I'm trying to achieve is when the user clicks on the notification part above the stop button I want to open a fragment. I'm showing the notification like this.
Intent notIntent = new Intent();
notIntent.setAction(ACTION_STOP);
PendingIntent pendInt = PendingIntent.getService(this, (int) System.currentTimeMillis(),
notIntent, PendingIntent.FLAG_UPDATE_CURRENT);
Notification n = builder.setContentIntent(pendInt)
.setSmallIcon(R.drawable.music_nepal_not)
.setColor(ContextCompat.getColor(BackgroundAudioService.this, R.color.colorPrimary))
.setTicker("Playing Live Radio")
.setContentTitle("Now Playing : Live Radio ")
.addAction(R.layout.toggle_notification, "", pendInt)
.build();
notificationManager =
(NotificationManager) getSystemService(NOTIFICATION_SERVICE);
n.flags |= Notification.FLAG_NO_CLEAR;
notificationManager.notify(0, n);

You can do something like:
n.addContentIntent(pendingIntent);
This would get triggered when the user clicks on the notification body. pendingIntent would be a PendingIntent to your Activity hosting the fragment since, those bastards cannot exist without activities. Similarly, you can add that pendingIntent to your addAction().

Related

How to send the notification bar back up programatically?

I researched and the only answers I could find either told:
How to disable the notification bar from being pulled down.
How to cancel a notification using:
NotificationManager notificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.cancel(0);
Both of these aren't what I require as the notification gets cancelled fine. I've set two actions in my notification: 'Dismiss' and 'Open Activity'. As the name implies, upon clicking both, the above code executes and clears the notification but doesn't cause the notification bar from going back up. This is needed, specially when the second Notification Action causes an Activity launch.
I tried on Android Lollipop and Nougat and the notification bar didn't go back up in either. So if someone could kindly tell me if it is even possible and how.
Thanks.
The code for building the notification:
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
builder
.setSmallIcon(icon).setTicker(message).setWhen(when)
.setAutoCancel(true).setContentTitle("Kindly record your Voice")
.setColor(Color.RED);
notificationIntent = new Intent(this,ReminderReceiver.class);
notificationIntent.setAction("Record");
PendingIntent pendIntent1 = PendingIntent.getBroadcast(getApplicationContext(), 1, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
builder.addAction(R.drawable.common_google_signin_btn_icon_dark, "Record", pendIntent1);
notificationIntent2 = new Intent(this, ReminderReceiver.class);
notificationIntent2.setAction("Dismissed");
PendingIntent pendIntent2 = PendingIntent.getBroadcast(getApplicationContext(), 1, notificationIntent2, PendingIntent.FLAG_UPDATE_CURRENT);
builder.addAction(R.drawable.starticon, "Dismiss", pendIntent2);
notificationIntent3 = new Intent(this, CancelReceiver.class);
PendingIntent pendIntent3 = PendingIntent.getBroadcast(getApplicationContext(), 1, notificationIntent3, PendingIntent.FLAG_UPDATE_CURRENT);
builder.setDeleteIntent(pendIntent3);
notification = builder.build();
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notificationManager.notify(1, notification);
Might seem weird, but the notification bar doesn't go up when there are other notifications present. If yours is the only notification, the bar slides up automatically upon clicking.

notification disappear when home button clicked

MY application displays notification when it is running. But when I click the home button (the application is still running in the background) the notification disappears.
What should I do to keep it as is until the user clicks on it.
Thanks
private void showNotification() {
// The PendingIntent to launch our activity if the user selects this
// notification
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
serviceIntent, 0);
Notification notification = new Notification.Builder(this)
.setTicker("Accelormeter app").setContentTitle("Acc")
.setContentText("Notification content.")
.setSmallIcon(R.drawable.noti_icon).setContentIntent(contentIntent)
.getNotification();
// Send the notification.
mNM.notify(0, notification);
}
Don't use getNotification(); instead use build(); in the Notification.Builder

Delete notification notification.builder on content click

I have a notification that have 2 actions. One intent open an Activity, the other open browser web.
my code:
Intent intent1 = new Intent(context, NotificationClickActivity.class);
PendingIntent pIntent1 = PendingIntent.getActivity(context, 0, intent1, PendingIntent.FLAG_CANCEL_CURRENT);
String url = "http://www.google.com";
Intent browserIntent = new Intent(Intent.ACTION_VIEW);
browserIntent.setData(Uri.parse(url));
PendingIntent pBrowserIntent = PendingIntent.getActivity(context, 1, browserIntent, PendingIntent.FLAG_CANCEL_CURRENT);
// Build notification
Notification noti = new Notification.Builder(context)
.setContentTitle(title)
.setContentText(shortDescription)
.setStyle(new Notification.BigTextStyle().bigText(longDescription))
.setSmallIcon(R.drawable.small_defaulticon)
.setAutoCancel(true)
.setContentIntent(pIntent1)
.addAction(R.drawable.playstore_icon_32, "Dettagli", pIntent1)
.setContentIntent(pBrowserIntent)
.addAction(R.drawable.small_defaulticon, "Vai al sito", pBrowserIntent).build();
NotificationManager notificationManager =
(NotificationManager) context.getSystemService(NOTIFICATION_SERVICE);
// Hide the notification after its selected
noti.flags |= Notification.FLAG_AUTO_CANCEL;
notificationManager.notify(0, noti);
If i click on the notification it goes away, but if I click on one of the 2 Actions it open the right intent but notification doesn't cleared.
I try also .setDeleteIntent(myPendingIntent) but nothing... I wonder where I am going wrong...
A screen of my notification...
Thank you!
A notification is never dismissed when an action button is pressed. It just sends you associated intent. If you want it to dismiss, you need to call NoficationManager.cancel(int id) to cancel it yourself. You will normally do it in the same method, which handles action button intent.
Notification.FLAG_AUTO_CANCEL flag you tried to use is only applied to notification body and not to action buttons.
use cancel() in the activity that started by your action to dismiss the notification
try this
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context)
.setDefaults(Notification.DEFAULT_ALL);
and try to send uniqueId in notificationManager.notify(uniqueId, noti);

NotificationManager — How to make a notification do log "no" if not pressed within two hours?

I've set up a notification that appears in the status bar at a certain time. When you press it, it will log "yes" into a database. However I want it to log "no" if it is not pressed after 2 hours. I understand how to do the logging part, but I have no clues on how to invoke that scenario if not pressed within two hours. I know I can create two calendars to compare them, but again where would I put these? Any suggestions please? I've added some of my code below. Thanks!
Setting alarm code:
Intent intent_noti = new Intent(this,Nofi.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 12345, intent_noti,
PendingIntent.FLAG_CANCEL_CURRENT);
AlarmManager am = (AlarmManager)getSystemService(Activity.ALARM_SERVICE);
am.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), setTime,
pendingIntent);
Notification code:
Intent intent = new Intent(this, ScanActivity.class);
PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent, 0);
Notification noti = new Notification.Builder(this)
.setContentTitle("MedScan")
.setContentText("2. You should take pills now")
.setSmallIcon(R.drawable.original)
.setContentIntent(pIntent)
.build();
NotificationManager notificationManager = (NotificationManager) getSystemService(
NOTIFICATION_SERVICE);
noti.defaults |= Notification.DEFAULT_ALL;
// Hide the notification after its selected
noti.flags |= Notification.FLAG_AUTO_CANCEL;
notificationManager.notify(ID, noti);
You can pass deleteIntent with your notification object that will be called if the user explicitly Clear your Notification
So to achieve your goal you can do the following:
When you display the notification, start alarm that will fire after two hours
Handle the two cases in which the use clicks your Notification or hit Clear and cancel the Alarm
If the alarm fires means your notification is still shown log no in the database

Displaying a notification in android

I am using the following code to show a notification in my android application, but using this, with the notification an activity also shows up (i.e. a black blank screen) I dont want this screen, but just a simple notification and when the user clicks the notification then I want to launch the activity. What should be done to this code?
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
Notification notify = new Notification(R.drawable.mcube_logo, "Message Sent", Calendar.getInstance().getTimeInMillis());
Intent intent = new Intent(RecieveAlarm.this, otherActivity.class);
PendingIntent pendingIntent =PendingIntent.getActivity(RecieveAlarm.this, 1, null, PendingIntent.FLAG_CANCEL_CURRENT);
notify.setLatestEventInfo(RecieveAlarm.this, "Title", "Details", pendingIntent);
notificationManager.notify(0, notify);
I suggest you to use:
Notification not = new Notification(idIcon, text, System.currentTimeMillis());
PendingIntent pInt = PendingIntent.getActivity(ctx, 0, new Intent(ctx, the_class_to_call), 0);
not.setLatestEventInfo(ctx, app_name, text, pInt);
and then not.notify....
use AlertDialog, then launch your desired activity on positive button click

Categories

Resources