I have a kind of weird problem. I know that I am missing something but I don't know what.
I am adding approximatly 5 notifications per day that I create each two to three hours.
If we have the scenario below:
Notification 1 is created at 8:00 and should be fired at 12:00.
When Notification 1 is received, it is processed and Notification 2 is created and needs to be fired at 14:00.
When Notification 2 is received, it is processed and Notification 3 is created and should be fired at 17:00
When Notification 3 is received, it is processed and Notification 4 is created and should be fired at 8:00 the next Day.
Notification 4 will not be received until I manually unlock/unsleep the phone. The time of the notification is 8:00 even if I unlocked the phone at 9:00.
So my problem is when the phone goes to sleep for a long time, the notifications are not processed until I manually pressed the wake button on my Samsung S7. Also, I am playing a media each time the notification is received thru a Service. For Notification 4 the media does not play until I manually wake the phone. Also, I have some style applied to the content, It displays correctly for Notification 1,2 and 3 but not on Notification 4.
I thought that maybe when I am creating Notifications for the next day, there is a problem registering them so I tried to play with my date manually and everything works ok.
This is how I create my Notification:
public Notification getNotification(String notificationName, long notificationTime) {
Intent intent = new Intent( context, NotificationPlayerService.class );
intent.setAction( NotificationPlayerService.ACTION_STOP );
PendingIntent pendingIntent = PendingIntent.getService(context, 1, intent, 0);
NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
builder.setContentTitle("Notification");
builder.setContentText(notificationName);
builder.setSmallIcon(R.drawable.icon);
builder.setColor(ContextCompat.getColor(context, R.color.teal_500));
builder.setLights(Color.GREEN, 3000, 3000);
builder.setVibrate(new long[]{0,750});
builder.setWhen(notificationTime);
builder.setShowWhen(true);
Uri sound= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
builder.setSound(sound);
builder.setDeleteIntent(pendingIntent);
return builder.build();
}
And this is how I schedule them:
#SuppressLint("NewApi")
public void scheduleNotification(Notification notification, long notificationTime, String action, int id) {
Intent notificationIntent = new Intent(context, NotificationPublisher.class);
notificationIntent.setAction(action);
if(id!=-1){
notificationIntent.putExtra(NotificationPublisher.NOTIFICATION, notification);
notificationIntent.putExtra(NotificationPublisher.NOTIFICATION_ID, id);
}
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
if (Build.VERSION.SDK_INT > 19) {
alarmManager.setExact(AlarmManager.RTC_WAKEUP, notificationTime, pendingIntent);
}
}
This is how I receive them:
public void onReceive(Context context, Intent intent) {
this.context = context;
NotificationManager notificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
String action = intent.getAction();
int id = intent.getIntExtra(NOTIFICATION_ID, -1);
Log.e("NOTIFICATION_RECEIVER","Notification received " + action +"-"+ id);
if(id != -1){
Notification notification = intent.getParcelableExtra(NOTIFICATION);
notificationManager.notify(id, notification);
}
Intent serviceIntent = NotificationService.startNotificationService(context);
if (serviceIntent != null) {
startWakefulService(context, serviceIntent);
}
}
And Finally I am using an IntenService to create my notifications:
#Override
protected void onHandleIntent(Intent intent) {
try{
startNotifications();
}
finally {
WakefulBroadcastReceiver.completeWakefulIntent(intent);
}
}
startNotification uses methods getNotification and scheduelNotification shown above. Basically it gets some settings from SharedPreferences and calculates when the notifications should be fired.
Thank you
Related
When my application is launched, it performs an API call and then schedules notifications based on the results. This amounts to around ~10 notifications being scheduled. There seems to be an issue with the timestamp displayed on the actual notification being incorrect.
Since I am creating these notifications and then scheduling an alarm with an AlarmManager, the default time present on the notification will be the time at which the notification is created (System.currentTimeMillis()).
I've tried to use the .setWhen() method on my Notification.Builder to set it to the time I am using to schedule the previously mentioned alarm. This is a little better, however, because notifications are not guaranteed to be delivered at the exact time specified, I often get notifications a few minutes in the past.
Additionally, I tried to manually override the when field on the notification in my BroadcastReceiver, right before .notify() is actually called:
public class NotificationPublisher extends BroadcastReceiver {
public static String NOTIFICATION_ID = "notification_id";
public static String NOTIFICATION = "notification";
public void onReceive(Context context, Intent intent) {
NotificationManager notificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = intent.getParcelableExtra(NOTIFICATION);
notification.when = System.currentTimeMillis();
int id = intent.getIntExtra(NOTIFICATION_ID, 0);
notificationManager.notify(id, notification);
}
}
However, in the above scenario, it seems that .when is ignored.
Frankly, I am simply looking for a way to have the timestamp displayed on the notification be the time at which it is actually displayed.
I would suggest passing in your notification's information as extras then building the notification inside of the BroadcastReceiver. This will build the notification just before it is issued, so it will have the same time your AlarmManager triggers the BroadcastReceiver.
From wherever you're scheduling the notification:
private void scheduleNotification(){
// Create an intent to the broadcast receiver you will send the notification from
Intent notificationIntent = new Intent(this, SendNotification.class);
// Pass your extra information in
notificationIntent.putExtra("notification_extra", "any extra information to pass in");
int requestCode = 1;
// Create a pending intent to handle the broadcast intent
PendingIntent alarmIntent = PendingIntent
.getBroadcast(this, requestCode, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
// Set your notification's trigger time
Calendar alarmStart = Calendar.getInstance();
alarmStart.setTimeInMillis(System.currentTimeMillis());
alarmStart.set(Calendar.HOUR_OF_DAY, 6); // This example is set to approximately 6am
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
// Set the alarm with the pending intent
// be sure to use set, setExact, setRepeating, & setInexactRepeating
// as well as RTC_WAKEUP, ELAPSED_REALTIME_WAKEUP, etc.
// where appropriate
alarmManager.set(AlarmManager.RTC_WAKEUP, alarmStart.getTimeInMillis(), alarmIntent);
}
Then, inside your BroadcastReceiver's onReceive:
String notificationExtra = null;
// Retrieve your extra data
if(intent.hasExtra("notification_extra")){
notificationExtra = intent.getStringExtra("notification_extra");
}
//Build the notification
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context);
mBuilder.setSmallIcon(notificationIcon)
.setContentTitle(notificationTitle)
.setContentText(notificationMessage)
.setAutoCancel(true); // Use AutoCancel true to dismiss the notification when selected
// Check if notificationExtra has a value
if(notificationExtra != null){
// Use the value to build onto the notification
}
//Define the notification's action
Intent resultIntent = new Intent(context, MainActivity.class); // This example opens MainActivity when clicked
int requestCode = 0;
PendingIntent resultPendingIntent =
PendingIntent.getActivity(
context,
requestCode,
resultIntent,
PendingIntent.FLAG_UPDATE_CURRENT
);
//Set notification's click behavior
mBuilder.setContentIntent(resultPendingIntent);
// Sets an ID for the notification
int mNotificationId = 1;
// Gets an instance of the NotificationManager service
NotificationManager mNotifyMgr =
(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
// Builds the notification and issues it.
mNotifyMgr.notify(mNotificationId, mBuilder.build());
I have also been struggling with this for a bit, but your question actually brought me to the best answer. I checked out setWhen() and it seems like now this just works fine (checked with API lvl 30 & 31). As this post is a few years old, maybe this issue was fixed in the meantime. So here's how I did it in Kotlin:
class NotificationPublisher : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
val notificationManager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
val notification = intent.getParcelableExtra<Notification>(NOTIFICATION)
notification?.`when` = System.currentTimeMillis() // overwriting the creation time to show the current trigger time when the notification is shown
val postId = intent.getIntExtra(NOTIFICATION_ID, 0)
notificationManager.notify(postId, notification)
}
Your NotificationPublisher's onReceive() method will be invoked only when scheduled alarm triggers as specified time. When you crate a notification from onReceive() method, it will definitely show the current time. No need to require to use .when or .setWhen() method.
Try this one:
public class NotificationPublisher extends BroadcastReceiver {
public static String NOTIFICATION_ID = "notification_id";
public static String NOTIFICATION = "notification";
public void onReceive(Context context, Intent intent) {
int id = intent.getIntExtra(NOTIFICATION_ID, 0);
// Notification
Notification notification = new Notification.Builder(context)
.setContentTitle("This is notification title")
.setContentText("This is notification text")
.setSmallIcon(R.mipmap.ic_launcher).build();
notification.flags |= Notification.FLAG_AUTO_CANCEL;
// Notification Manager
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager .notify(id, notification);
}
}
If you want to redirect to an activity when click on Notification, then you can use PendingIntent and set it to your Notification.
public class NotificationPublisher extends BroadcastReceiver {
public static String NOTIFICATION_ID = "notification_id";
public static String NOTIFICATION = "notification";
public void onReceive(Context context, Intent intent) {
int id = intent.getIntExtra(NOTIFICATION_ID, 0);
Intent intent = new Intent(context, YourTargetActivity.class);
intent.putExtra("KEY_ID", id); // Pass extra values if needed
PendingIntent pI = PendingIntent.getActivity(context, id, intent, PendingIntent.FLAG_UPDATE_CURRENT);
// Notification
Notification notification = new Notification.Builder(context)
.setContentTitle("This is notification title")
.setContentText("This is notification text")
.setSmallIcon(R.mipmap.ic_launcher)
.setContentIntent(pI).build();
notification.flags |= Notification.FLAG_AUTO_CANCEL;
// Notification Manager
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager .notify(id, notification);
}
}
Hope this will help~
I've been programming in Android for over a year but have never used notifications before, which I need for one of my apps.
In the app, there are events that can be set at different times. Obviously, the user can choose to change these times or add/remove events.
I plan to use notifications to notify the user 5 minutes before their event starts.
I've read through the Android developer docs for Building a Notification and Services (which I am also new to). I have decided to use Services because I figured that the notification would need to be shown even when the app not running. To help me, I have been referring to one particular SO answer as guidance.
I begun by experimenting a little with the notification builder classes in my NotificationService class like so:
public class NotificationService extends IntentService {
private static final int NOTIFICATION_ID = 1;
public NotificationService() {
super(NotificationService.class.getSimpleName());
}
#Override
protected void onHandleIntent(Intent intent) {
showNotification();
}
private void showNotification() {
Intent intent = new Intent(this, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(
this, NOTIFICATION_ID, intent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
.setContentTitle("Test notification")
.setSmallIcon(R.drawable.ic_event_black_24dp)
.setContentText("Test description")
.setContentIntent(pendingIntent);
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
manager.notify(NOTIFICATION_ID, builder.build());
}
}
I also use the following to start this Service in the splash screen of my app:
Intent serviceIntent = new Intent(this, NotificationService.class);
startService(serviceIntent);
To display notifications at specific times, I've read about AlarmManager, mainly through SO questions like this one. I also know that to display multiple notifications, I would need different notification ids (like my constant NOTIFICATION_ID).
However, what I am unsure of is dynamically updating the times that notifications would be shown each time an event is added, removed, or its times changed.
Could someone provide me some guidance on how I could achieve this?
You implement notification part. For notify user at Specific time you should set AlarmManager. I paste whole code you need then explain each part:
public class AlarmReceiver extends WakefulBroadcastReceiver {
AlarmManager mAlarmManager;
PendingIntent mPendingIntent;
#Override
public void onReceive(Context context, Intent intent) {
int mReceivedID = Integer.parseInt(intent.getStringExtra(AddReminderActivity.EXTRA_REMINDER_ID));
// Get notification title from Reminder Database
ReminderDatabase rb = new ReminderDatabase(context);
ApiReminderModel reminder = rb.getReminder(mReceivedID);
String mTitle = reminder.getName();
// Create intent to open ReminderEditActivity on notification click
// handling when you click on Notification what should happen
Intent editIntent = YourActivity.createActivity(context, reminder.getChannelId(), reminder.getStartTime());
PendingIntent mClick = PendingIntent.getActivity(context, mReceivedID, editIntent, PendingIntent.FLAG_UPDATE_CURRENT);
// Create Notification
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context)
.setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.drawable.ic_logo))
.setSmallIcon(R.drawable.ic_logo)
.setContentTitle(context.getResources().getString(R.string.app_name))
.setTicker(mTitle)
.setContentText(mTitle)
.setContentIntent(mClick)
.setSound(ringtoneUri)
.setAutoCancel(true)
.setOnlyAlertOnce(true);
NotificationManager nManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
nManager.notify(mReceivedID, mBuilder.build());
}
public void setAlarm(Context context, Calendar calendar, int ID) {
mAlarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
// Put Reminder ID in Intent Extra
Intent intent = new Intent(context, AlarmReceiver.class);
intent.putExtra(AddReminderActivity.EXTRA_REMINDER_ID, Integer.toString(ID));
mPendingIntent = PendingIntent.getBroadcast(context, ID, intent, PendingIntent.FLAG_CANCEL_CURRENT);
// Calculate notification time
Calendar c = Calendar.getInstance();
long currentTime = c.getTimeInMillis();
long diffTime = calendar.getTimeInMillis() - currentTime;
// Start alarm using notification time
mAlarmManager.set(AlarmManager.ELAPSED_REALTIME, SystemClock.elapsedRealtime() + diffTime, mPendingIntent);
// Restart alarm if device is rebooted
ComponentName receiver = new ComponentName(context, BootReceiver.class);
PackageManager pm = context.getPackageManager();
pm.setComponentEnabledSetting(receiver,
PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
PackageManager.DONT_KILL_APP);
}
public void cancelAlarm(Context context, int ID) {
mAlarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
// Cancel Alarm using Reminder ID
mPendingIntent = PendingIntent.getBroadcast(context, ID, new Intent(context, AlarmReceiver.class), 0);
mAlarmManager.cancel(mPendingIntent);
// Disable alarm
ComponentName receiver = new ComponentName(context, BootReceiver.class);
PackageManager pm = context.getPackageManager();
pm.setComponentEnabledSetting(receiver,
PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
PackageManager.DONT_KILL_APP);
}
}
As you see we have setAlarm which notify users at specific time. I also pass calendar instance which is initiated before ( assign a time which user should be notify).
Calendar mCalendar = Calendar.getInstance();
mCalendar.add(Calendar.DAY_OF_YEAR, 7);// assume you want send notification 7 days later
new AlarmReceiver().setAlarm(getApplicationContext(), mCalendar, id);
We have another method cancelAlarm. If you want to delete a Alaram just pass unique ID of Alarm (Which already used for creation of Alarm).
Also don't forget to add this Service to your AndroidManifest.xml:
<receiver android:name=".service.AlarmReceiver" />
There is only on thing remain When you reboot your device you should set AlarmsManager again so you need a BootRecivier service.
I'm building an app that shows a notification with two options "Dim" and "Full" when a BroadcastReceiver is called. Each of this buttons broadcasts an action.
So far everything is possible to do, right?
The problem is that the buttons shown on the notification do not respond to the tap but the whole notification does (if I click on the icon or text instead of the button).
I have this function to build the notification:
private Notification buildReleaseNotification(NotificationManager nManager, Context context) {
Notification.Builder builder = new Builder(context);
builder.addAction(R.drawable.rate_star_big_half_holo_dark, "Dim", buildPendingIntent(context, DIM));
builder.addAction(R.drawable.rate_star_big_on_holo_dark, "Full", buildPendingIntent(context, FULL));
builder.setContentTitle("Car notification");
builder.setContentText("Freed");
builder.setSmallIcon(R.drawable.ic_launcher);
builder.setOngoing(true);
Notification notification = builder.build();
return notification;
}
and it is called when receiving a broadcast:
public void onReceive(Context context, Intent intent) {
NotificationManager nManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification noty = null;
noty = buildReleaseNotification(context);
startService(context, RELEASE);
if (noty != null) {
nManager.notify(ChangeLockService.GLOBAL_TAG, ID, noty);
}
}
------ edit
Just noticed that the following function returns null... So, how can I build a pending intent to perform a broadcast?
private PendingIntent buildPendingIntent(Context context, String action) {
Intent i = new Intent(action);
PendingIntent pi = PendingIntent.getBroadcast(context, ID, i, Intent.FLAG_RECEIVER_REPLACE_PENDING);
return pi;
}
When creating the intent through
PendingIntent pi = PendingIntent.getBroadcast(context, ID, i, Intent.FLAG_RECEIVER_REPLACE_PENDING);
caused it to return null.
According to the documentation, I understood that the flag Intent.FLAG_RECEIVER_REPLACE_PENDING would replace any pending intent that already existed for that button. If I send no flags then everything works fine:
PendingIntent pi = PendingIntent.getBroadcast(context, ID, i, 0);
I've got an application that needs to show a notification of certain events in a day, depending on the event.
I know how to do it, if I need a service to stay running in the background, or it will run in the background getting these events on certain days.
private void Notificar(String descricaoEvento){
NotificationManager notifier = (NotificationManager)this.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(R.drawable.ic_launcher, "Novo evento no The Point Club", System.currentTimeMillis());
notification.flags |= Notification.DEFAULT_SOUND;
PendingIntent pIntent = PendingIntent.getActivity(this, 0, null, 0);
notification.setLatestEventInfo(this, "Title", descricaoEvento.toString(), pIntent);
notifier.notify(0x007, notification);
}
using this method to call when the right date!
Thanks for listening, I'll try to explain what I'm doing right: I'm making an application for a club party, and the application will show the next event of the celebrations when I open the application, but what I wish is that a notification is created in android notification bar, on which there is a party, serving as a reminder.
I use this code when I open the activity to list events:
Calendar c = Calendar.getInstance();
c.setTimeInMillis(System.currentTimeMillis());
int anoAtual= c.get(Calendar.YEAR);
long time = c.getTimeInMillis();
Intent it = new Intent("EXECUTAR_ALARME")
p = PendingIntent.getBroadcast(Eventos.this, 0, it, 0);
AlarmManager alarme = (AlarmManager) getSystemService(ALARM_SERVICE);
alarme.set(AlarmManager.RTC_WAKEUP, time, p);
the line
Intent it = new Intent("EXECUTAR_ALARME")
will call the class:
public class Alarm extends BroadcastReceiver{ #Override
public void onReceive(Context contexto, Intent intent) {
NotificationManager notifier =(NotificationManager) contexto.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(R.drawable.ic_launcher, "New event in Club", System.currentTimeMillis());
notification.flags |= Notification.DEFAULT_SOUND;
//Intent i = new Intent(this, Eventos.class);
PendingIntent pIntent = PendingIntent.getActivity(contexto, 0, null, 0);
notification.setLatestEventInfo(contexto, "Club", "Teste", pIntent);
notifier.notify(0x007, notification);
}
}
Now I want to know two things:
1 - How do I leave it running in the background without being open with the application.
2 This code only creates only an alarm, and I need to create multiple.
Thanks for the help.
Use AlarmManager to run a PendingIntent at your specific time. At that time, the intent that it stored in the PendingIntent should likely be a service if you're planning on creating a Notification to display to the user. If you could form this as a question, I may be able to help more, but it's unclear what you're trying to accomplish, and what you've managed to accomplish.
Additional Details
In your code, it looks like you're scheduling the alarm for right now, instead of at some time in the future. I'd suggest the following to create the alarm:
Calendar c = Calendar.getInstance();
// don't do this:
// c.setTimeInMillis(System.currentTimeMillis()); // This just sets the alarm time to "now"
// set the calendar to 8:00 pm:
c.set(Calendar.HOUR_OF_DAY, 20);
c.set(Calendar.MINUTE, 0);
long alarmTime = c.getTimeInMillis();
Intent it = new Intent(ServiceClass.getClass());
// Even though the docs say that the request code doesn't matter making it unique tells the alarmManager that it's a different PendingIntent
PendingIntent pendingIntent = PendingIntent.getService(this, 0, it, 0);
AlarmManager alarmMgr = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmMgr.set(AlarmManager.RTC_WAKEUP, alarmTime, pendingIntent); // set the first alarm for 8:00
c.set(Calendar.MINUTE, 30);
Intent it2 = new Intnt(ServiceClass.getClass());
PendingIntent pi2 = PendingIntent.getService(this, 0, it2, 0);
alarmMgr.set(AlarmManager.RTC_WAKEUP, alarmTime, pi2); // set the second alarm for 8:30
the line
Intent it = new Intent(ServiceClass.getClass());
will start the Class ServiceClass:
class ServiceClass extends Service {
#Override
public IBinder onBind(Intent intent) {
// TODO TRAVIS Auto-generated method stub
return null;
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) { // onStartCommand is run when the service is started by the AlarmManager
// build and set the notification here
NotificationManager notifMgr = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
// build notification
Notification notification = new Notification.Builder(getApplicationContext())
.setContentTitle("New mail from " + sender.toString())
.setContentText(subject)
.setSmallIcon(R.drawable.new_mail)
.setLargeIcon(aBitmap)
.build();
int ID = 0;
notifMgr.notify(ID++, notification); // use a unique id every time if you want multiple notifications (sounds like what you want, but not a best practice)
notification = new Notification.Builder(getApplicationContext())
.setContentTitle("New event from " + sender.toString())
.setContentText(subject)
.setSmallIcon(R.drawable.new_mail)
.setLargeIcon(aBitmap)
.build();
notifMgr.notify(ID++, notification); // add the second notification to the notification bar
return super.onStartCommand(intent, flags, startId);
}
}
Answers to your questions:
1 - By setting the alarm for sometime in the future, you're asking the OS to start your PendingIntent (in your case, send a Broadcast; in my exmaple above, a Service) at the time specified (that's why you need to set the time with the calendar to sometime in the future!)
2 - Creating multiple alarms is a matter of calling the alarm manager with different PendingIntent s scheduled at the appropriate time (for when you want them to fire). The same is true for Notification s, create multiple ones (with unique ids).
Is there any way in Android to detect when a user swipes a notification to the left and deletes it? I'm using an alarmmanager to set a repeating alert and I need my repeating alert to stop when the notification is cancelled by the user. Here's my code:
Setting the repeating alert:
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), repeatFrequency, displayIntent);
My notification code:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//Get the notification ID.
int notifID = getIntent().getExtras().getInt("Reminder_Primary_Key");
//Get the name of the reminder.
String reminderName = getIntent().getExtras().getString("Reminder_Name");
//PendingIntent stores the Activity that should be launched when the user taps the notification.
Intent i = new Intent(this, ViewLocalRemindersDetail.class);
i.putExtra("NotifID", notifID);
i.putExtra("notification_tap", true);
//Add FLAG_ACTIVITY_NEW_TASK to stop the intent from being launched when the notification is triggered.
PendingIntent displayIntent = PendingIntent.getActivity(this, notifID, i, Intent.FLAG_ACTIVITY_NEW_TASK);
NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Notification notif = new Notification(R.drawable.flag_red_large, reminderName, System.currentTimeMillis());
CharSequence from = "Here's your reminder:";
CharSequence message = reminderName;
notif.setLatestEventInfo(this, from, message, displayIntent);
//Pause for 100ms, vibrate for 250ms, pause for 100ms, and vibrate for 500ms.
notif.defaults |= Notification.DEFAULT_SOUND;
notif.vibrate = new long[] { 100, 250, 100, 500 };
nm.notify(notifID, notif);
//Destroy the activity/notification.
finish();
}
I know I need to call alarmManager.cancel(displayIntent) in order to cancel my repeating alarm. However, I don't understand where to put this code. I need to cancel the repeating alert ONLY when the user has tapped on the notification or dismissed it. Thanks for your help!
I believe that Notification.deleteIntent is what you are looking for. The doc says:
The intent to execute when the notification is explicitly dismissed by the user, either with the "Clear All" button or by swiping it away individually. This probably shouldn't be launching an activity since several of those will be sent at the same time.
To all those future people out there -- you can register a broadcast receiver to listen for notification delete inents.
Create a new broadcast receiver:
public class NotificationBroadcastReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (action == null || !action.equals(Config.NotificationDeleteAction)) {
return;
}
// Do some sweet stuff
int x = 1;
}
}
Register the broadcast receiver within your application class:
"If your app targets API level 26 or higher, you cannot use the manifest to declare a receiver for most implicit broadcasts (broadcasts that do not target your app specifically)."
Android Documentation.
registerReceiver(
new NotificationBroadcastReceiver(),
new IntentFilter(Config.NotificationDeleteAction)
);
You probably noticed the static variable Config.NotificationDeleteAction. This is a unique string identifier for your notification. It typically follows the following {namespace}{actionName} convention:
you.application.namespace.NOTIFICATION_DELETE
Set the delete intent on your notification builder:
notificationBuilder
...
.setDeleteIntent(createDeleteIntent())
...
Where, createDeleteIntent is the following method:
private PendingIntent createDeleteIntent() {
Intent intent = new Intent();
intent.setAction(Config.NotificationDeleteAction);
return PendingIntent.getBroadcast(
context,
0,
intent,
PendingIntent.FLAG_ONE_SHOT
);
}
Your registered broadcast receiver should receive the intent when your notification is dismissed.
You can also use an Activity PendingIntent, which may be simpler to implement if you have an Activity that can handle the dismissal, because you don't have to create and configure a broadcast receiver.
public static final String DELETE_TAG = "DELETE_TAG";
private PendingIntent createDeleteIntent(Context context) {
Intent intent = new Intent(context, MyActivity.class);
intent.putExtra(DELETE_TAG, true);
return PendingIntent.getActivity(
context,
0,
intent,
PendingIntent.FLAG_ONE_SHOT
);
}
MyActivity would receive the intent in its onCreate(), and in this example, could look for the DELETE_TAG extra to recognize it.