Here is my code.
From where i am not able to generate alarm programmatically..
Calendar cal = Calendar.getInstance();
int id = (int) cal.getTimeInMillis();
Intent myIntent = new Intent(this,MyScheduledReceiver.class);
myIntent.putExtra("taskTitle", taskTitle.getText().toString());
myIntent.putExtra("taskDetails", taskDetails.getText().toString());
cal.setTimeInMillis(System.currentTimeMillis());
cal.set(year, mon, day,tp.getCurrentHour(),tp.getCurrentMinute());
PendingIntent sender = PendingIntent.getBroadcast(getApplicationContext(), id,
myIntent,PendingIntent.FLAG_UPDATE_CURRENT| Intent.FILL_IN_DATA);
AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
am.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), sender);
I declared in the manifest file
<receiver android:name=".MyScheduledReceiver"></receiver>
An in the broadcast reciever.
NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
NotificationManager manger = (NotificationManager)
context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(R.drawable.ic_launcher, "Combi Note",
System.currentTimeMillis());
PendingIntent contentIntent = PendingIntent.getActivity(context,
NOTIFICATION_ID,
new Intent(context, MyScheduledReceiver.class), 0);
Bundle extras=intent.getExtras();
String title=extras.getString("taskTitle");
String note=extras.getString("taskDetails");
notification.setLatestEventInfo(context, note, title, contentIntent);
notification.flags = Notification.FLAG_INSISTENT;
notification.defaults |= Notification.DEFAULT_SOUND;
manger.notify(NOTIFICATION_ID++, notification);
I have something similar and it works.
First hi declare a Pending:
Intent intent = new Intent(Global.a, EventAlarmReceiver.class);
intent.putExtra("title", ""+cd.title);
intent.putExtra("desc", ""+cd.description);
PendingIntent pendingIntent = PendingIntent.getBroadcast(
Global.a.getApplicationContext(), (int) (cd.alarmTime), intent, PendingIntent.FLAG_CANCEL_CURRENT);
AlarmManager alarmManager = (AlarmManager) Global.a.getSystemService(Activity.ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC, cd.alarmTime, pendingIntent);
and in EventAlarmReceiver class i have:
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Activity.NOTIFICATION_SERVICE);
String text = String.valueOf(intent1.getCharSequenceExtra("title"));
Notification notification = new Notification(R.id.icon,
text, System.getTimeInMillis());
notification.vibrate = new long[]{100,250,300,330,390,420,500};
// Hide the notification after its selected
notification.flags |= Notification.FLAG_AUTO_CANCEL;
Intent intent = new Intent(context, ShowEventActivity.class);
intent.putExtra("title", String.valueOf(intent1.getCharSequenceExtra("title")));
intent.putExtra("desc", String.valueOf(intent1.getCharSequenceExtra("desc")));
String text1 = String.valueOf(intent1.getCharSequenceExtra("desc"));
PendingIntent activity = PendingIntent.getActivity(context, (int) System.getTimeInMillis() , intent, PendingIntent.FLAG_CANCEL_CURRENT);
notification.setLatestEventInfo(context, text, text1, activity);
notificationManager.notify((int)System.getTimeInMillis(), notification);
make sure that in the Manifest you declare declare the package where the Receiver is: for example in my case the EventAlarmReceiver class is the package com.app.name.notifications so in the manifest i have:
<receiver android:name=".notifications.EventAlarmReceiver"></receiver>
Related
In my application if there are 3 notification, On tapping of any notification I am getting data of last notification. I am setting number with each notification but If there are multiple notification I get the data of last notification.
Here is my code when I set notification
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Calendar calendar = userSetMeetingDate;
calendar.set(Calendar.HOUR_OF_DAY, conferenceHour);
calendar.set(Calendar.MINUTE, conferenceMinute);
Bundle bundle = new Bundle();
bundle.putInt(KEY_CONFERENCE_ID, entity.getId());
Intent alarmIntent = new Intent(this, OnConferenceAlarmReceiver.class);
alarmIntent.putExtra(KEY_CONFERENCE_ENTITIY_BUNDLE, bundle);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, entity.getId(), alarmIntent, PendingIntent.FLAG_UPDATE_CURRENT);
alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
and
Notification notification = new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle(entity.getName())
.setContentText(entity.getConfrenceNumber())
.setContentIntent(pendingIntent)
.setVibrate(vibrate)
.setDefaults(Notification.DEFAULT_SOUND)
.setAutoCancel(true)
.addAction(android.R.drawable.ic_menu_call, "Call", pendingIntent)
//.addAction(android.R.drawable.ic_menu_edit, "Update", pendingIntent)
.build();
notification.flags |= Notification.FLAG_AUTO_CANCEL;
int id = (int) ((long) entity.getId());
notificationManager.notify(id, notification);
Thanks in Advance
I have used broadcast receiver and alarm manager. Added the notification in particular date and time. The notification is showing fine. But when the user is touching the notification i want to launch myApplication.
Notification.Builder notification= new Notification.Builder(this);
notification.setContentTitle("MY Title");
notification.setContentText("Today you have scheduled for...");
notification.setSmallIcon(R.drawable.ic_app_launcher);
notification.setAutoCancel(true);
notification.build();
Intent notificationIntent = new Intent(this, NotificationPublisher.class);
notificationIntent.putExtra(NotificationPublisher.NOTIFICATION_ID, 1);
notificationIntent.putExtra(NotificationPublisher.NOTIFICATION, notification);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
long futureInMillis = dateSpecified.getTime(); //Some future date like 20 feb 2015
AlarmManager alarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, futureInMillis, pendingIntent);
NotificationPublisher class -BroadcastReciver
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);
int id = intent.getIntExtra(NOTIFICATION_ID, 0);
notificationManager.notify(id, notification);
}
}
I referred this link.
I hope your understand what i'm trying to say.
Please anybody help me. Thanks lot.
But when the user is touching the notification i want to launch
myApplication.
Because NotificationPublisher BroadcastReceiver fire when notification is clicked so start Application from onReceive method of Receiver:
public void onReceive(Context context, Intent intent) {
// start application here
NotificationManager notificationManager = (NotificationManager)
context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = intent.getParcelableExtra(NOTIFICATION);
int id = intent.getIntExtra(NOTIFICATION_ID, 0);
Intent notificationIntent = new Intent(context, HomeActivity.class);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
| Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent intentn = PendingIntent.getActivity(context, 0,
notificationIntent, 0);
notification.setLatestEventInfo(context, title, message, intentn);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notificationManager.notify(id, notification);
}
I am trying to do scheduled notification. All works except: When application is active and minimized. Notification auto starts activity without waiting for user to click on it.
On reveive:
public void onReceive(Context context, Intent paramIntent) {
Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationManager notificationManager = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.cancelAll();
Notification notification = new Notification(R.drawable.logo_f, context.getResources().getString(R.string.notification_text), System.currentTimeMillis());
Intent notificationIntent = new Intent(context, TimeLeftActivity.class);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
| Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent intent = PendingIntent.getActivity(context, 0,
notificationIntent, 0);
notification.setLatestEventInfo(context, context.getResources().getString(R.string.notification_text), "", intent);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notification.sound=alarmSound;
// Fire the notification
notificationManager.notify(1, notification);
}
My notification start method:
private void createScheduledNotification(int sec)
{
// Get new calendar object and set the date to now
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
// Add defined amount of days to the date
calendar.add(Calendar.SECOND, sec);
// Retrieve alarm manager from the system
AlarmManager alarmManager = (AlarmManager) getApplicationContext().getSystemService(getBaseContext().ALARM_SERVICE);
// Every scheduled intent needs a different ID, else it is just executed once
int id = 1;
// Prepare the intent which should be launched at the date
Intent intent = new Intent(this, TimeAlarm.class);
// Prepare the pending intent
PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), id, intent, PendingIntent.FLAG_UPDATE_CURRENT);
alarmManager.cancel(pendingIntent);
// Register the alert in the system. You have the option to define if the device has to wake up on the alert or not
alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
}
EDIT after Kirill answer. Error still persist. Notification auto starts pending intent and does not wait for click.
#Override
public void onReceive(Context context, Intent paramIntent) {
Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationManager notificationManager = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.cancelAll();
Intent notificationIntent = new Intent(context, TimeLeftActivity.class);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
| Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent intent = PendingIntent.getActivity(context, 0,
notificationIntent, 0);
Notification notification = new NotificationCompat.Builder(context)
.setContentTitle(context.getResources().getString(R.string.notification_text))
.setContentIntent(intent)
.setSound(alarmSound)
.build();
notification.flags |= Notification.FLAG_AUTO_CANCEL;
// Fire the notification
notificationManager.notify(1, notification);
}
It is hard to find error, because you use deprecated API in your code, you should to use Notication.Builder
Notification noti = new Notification.Builder(mContext)
.setContentTitle("New mail from " + sender.toString())
.setContentText(subject)
.setSmallIcon(R.drawable.new_mail)
.setLargeIcon(aBitmap)
.build();
If you need to support old versions you can use NotificationCompat
UPDATE
This is sample from my app, it throws a notification, which open activity by click, I marked method to add intent.
String message = context.getString(R.string.notif_message);
Intent notificationIntent = new Intent(AddBpRecordActivity.ADD_ACTION);
NotificationCompat.Builder nb = new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.ic_notif_logo)
.setContentTitle(message)
.setContentText(billet.comment)
.setDefaults(Notification.DEFAULT_ALL)
.setAutoCancel(true)
>>> .setContentIntent(PendingIntent.getActivity(context, (int) billet.id, notificationIntent, PendingIntent.FLAG_CANCEL_CURRENT))
.setWhen(System.currentTimeMillis());
Notification notification = nb.build();
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify((int) billet.id, notification);
I am scheduling events and creating the notification for the same on weekly basis or daily basis.The notification is being displayed at correct time.
Problem
I want to delete the notification once the scheduled event is deleted. But I get the notification even if event is deleted. I tried deleting the Pending Intent by passing the same unique_id(eventId) that I have used for creating that intent, but its not deleting. Please find my code below:
creating notification
Intent intent = new Intent(this, MyReceiver.class);
HABIT_NAME = habitName.getText().toString();
intent.putExtra("HABIT_NAME", habitName.getText().toString());
PendingIntent pendingIntent = PendingIntent.getBroadcast(this.getApplicationContext(), eventId, intent, 0);
AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
if(daysNo.size() == 7){ //alarm for every day
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,reminderLifeTS.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);
}else{ // specific days of week
for(int i=0;i<daysNo.size();i++){
int day = getWeek(daysNo.get(i));
Calendar c = Calendar.getInstance();
c.set(Calendar.DAY_OF_WEEK, day);
c.set(Calendar.HOUR_OF_DAY, reminderLifeTS.get(Calendar.HOUR_OF_DAY));
c.set(Calendar.MINUTE, reminderLifeTS.get(Calendar.MINUTE));
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,
c.getTimeInMillis(),7*24*60*60*1000, pendingIntent);
}
}
Service class
mManager = (NotificationManager) this.getApplicationContext().
getSystemService(this.getApplicationContext().NOTIFICATION_SERVICE);
Intent intent1 = new Intent(this.getApplicationContext(),MainActivity.class);
intent1.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP| Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingNotificationIntent = PendingIntent.getActivity(
this.getApplicationContext(),0, intent1,PendingIntent.FLAG_ONE_SHOT);
// megha
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.app_icon)
.setContentTitle("DharmaLife")
.setContentText(AddScheduleEventActivity.HABIT_NAME)
.setContentIntent(pendingNotificationIntent);
Notification note = mBuilder.build();
note.defaults |= Notification.DEFAULT_VIBRATE;
note.defaults |= Notification.DEFAULT_LIGHTS;
note.flags|= Notification.FLAG_AUTO_CANCEL;
PowerManager pm = (PowerManager) getApplicationContext().getSystemService(Context.POWER_SERVICE);
WakeLock wakeLock = pm.newWakeLock((PowerManager.SCREEN_BRIGHT_WAKE_LOCK |
PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP), "TAG");
wakeLock.acquire();
Deleting Notification
Intent intent = new Intent();
PendingIntent pendingIntent = PendingIntent.getBroadcast(this.getApplicationContext(), eventId, intent, PendingIntent.FLAG_UPDATE_CURRENT);
pendingIntent.cancel();
AlarmManager am = (AlarmManager) getApplicationContext().getSystemService(Context.ALARM_SERVICE);
am.cancel(pendingIntent);
EDIT
to keep the list of all the pending intents created, I am saving it in arrayList along with its unique id:
notificationData.setAlarmManager(alarmManager);
notificationData.setPendingIntent(pendingIntent);
notificationData.setEventId(eventId);
LifeLiteral.NOTIFICATION_STACK.add(notificationData);
to delete the notification:
for(NotificationData i : LifeLiteral.NOTIFICATION_STACK){
if(i.getEventId() == eventId){
pendingIntent = i.getPendingIntent();
alarmManager = i.getAlarmManager();
alarmManager.cancel(pendingIntent);
pendingIntent.cancel();
}
}
but still notifications are not getting deleted. Please guide.
When attempting to delete the PendingIntent you aren't passing an Intent that matches the original Intent (because the Intent you pass to getBroadcast() is empty). Change your delete code to this:
Intent intent = new Intent(this, MyReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this.getApplicationContext(),
eventId, intent, 0);
AlarmManager am = (AlarmManager) getApplicationContext().getSystemService(Context.ALARM_SERVICE);
am.cancel(pendingIntent);
// Cancel the `PendingIntent` after you've canceled the alarm
pendingIntent.cancel();
I am using AlarmManager and NotificationManager with BroadcastReceiver.
When I set an alarm with a specific date, it is working the first time. However, when I modify the date and click the confirm button, the alarm is working any date
immediately. I want to set the alarm interval day after expired date with fixed time.
What is the problem with it? I don't understand at the moment.
confirmButton.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v) {
//set alarm with expiration date
am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
setOneTimeAlarm();
Toast.makeText(fridgeDetails.this, "Alarm automatic set",
Toast.LENGTH_SHORT).show();
setResult(RESULT_OK);
finish();
}
public void setOneTimeAlarm() {
c.set(Calendar.HOUR_OF_DAY, 14);
c.set(Calendar.MINUTE, 49);
c.set(expiredYear, expiredMonth, expiredDay);
Intent myIntent = new Intent(fridgeDetails.this, AlarmService.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(
fridgeDetails.this, 0, myIntent, PendingIntent.FLAG_ONE_SHOT);
am.setRepeating(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(),
AlarmManager.INTERVAL_DAY, pendingIntent);
}
});
AlarmService.java
public class AlarmService extends BroadcastReceiver{
NotificationManager nm;
#Override
public void onReceive(Context context, Intent intent) {
nm = (NotificationManager) context.getSystemService(
Context.NOTIFICATION_SERVICE);
CharSequence from = "Check your fridge";
CharSequence message = "It's time to eat!";
PendingIntent contentIntent = PendingIntent.getActivity(context, 0,
new Intent(), 0);
Notification notif = new Notification(R.drawable.ic_launcher,
"Keep Fridge", System.currentTimeMillis());
notif.setLatestEventInfo(context, from, message, contentIntent);
notif.defaults |= Notification.DEFAULT_SOUND;
notif.flags |= Notification.FLAG_AUTO_CANCEL;
nm.notify(1, notif);
}
}
You need to set the property instead of FLAG_ONE_SHOT. This is for single alarm event not for the repeat. try this
PendingIntent pendingIntent = PendingIntent.getBroadcast(
fridgeDetails.this, 0, myIntent, PendingIntent.FLAG_UPDATE_CURRENT);
see more detail about from here
Edit:
As you do for notification with PendingIntent
PendingIntent contentIntent = PendingIntent.getActivity(context, 0,new Intent(), 0);
In this you pass the empty object of Intent you need to pass the class name for that when you click on notification which Activity will be launch like in Alarm set time you do
Intent myIntent = new Intent(fridgeDetails.this, AlarmService.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(
fridgeDetails.this, 0, myIntent, PendingIntent.FLAG_UPDATE_CURRENT);
now just pass the Acitivity name in the intent like suppose you want to launch your home activity and activity name like "homeactivity"
PendingIntent contentIntent = PendingIntent.getActivity(context, 0,new Intent(context,HomeActivity.class), 0);