I have the code for showing the notification in the status bar. I know the updating can be done by calling setLatestEventInfo() again, but I want the updating to be done in every half an hour.
How do I keep track of the time?
Does somebody know any function for that?
I even thought of using counter which keeps getting incremented every half an hour but again retrieving the time is a problem.
String ns = Context.NOTIFICATION_SERVICE;
NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);
int icon = R.drawable.index1;
CharSequence tickerText = "Hello";
long when = System.currentTimeMillis();
Notification notification = new Notification(icon, tickerText, when);
Context context = getApplicationContext();
CharSequence notificationTitle = "My notification";
CharSequence notificationText = "Hello World!";
Intent notificationIntent = new Intent(this, NotificationAppActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
notification.setLatestEventInfo(context, notificationTitle, notificationText, contentIntent);
final int HELLO_ID = 1;
mNotificationManager.notify(HELLO_ID, notification);
notification.defaults |= Notification.DEFAULT_SOUND;
notification.defaults |= Notification.DEFAULT_VIBRATE;
notification.defaults |= Notification.DEFAULT_LIGHTS;
}
You have to use AlarmManager and schedule recurring event every 30 minutes. Then you need to handle this event and in broadcast receiver's onReceive() update your notification, but usually sending Intent to your service to do the job.
Example code:
Intent intent = new Intent(this, MyAlarmBroadcastReceiver.class);
PendingIntent sender = PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager am = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE);
long recurring = (30 * 60000); // in milliseconds
am.setRepeating(AlarmManager.RTC, Calendar.getInstance().getTimeInMillis(), recurring, sender);
and your MyAlarmBroadcastReceiver is regular BroadcastReceiver with code in onReceive(). I prefer to use one broadcast receiver so I also add some additional data to the intent so my broadcast receiver knows what it should do, but you can have it separated if you like.
public class MyBroadcastReceiver extends BroadcastReceiver {
#Override
public void onReceive( Context context, Intent intent ) {
// ... do what you need to do here...
}
}
Use alarmManager as in example bellow:
AlarmManager mgr=(AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
Intent i=new Intent(context, OnAlarmReceiver.class);
PendingIntent pi=PendingIntent.getBroadcast(context, 0, i, 0);
mgr.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime(), PERIOD, pi);
Related
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 using alarm manager to display multiple local notification. The notification works fine, but the sequence of notification is happens only after i clear it from notification bar. The sequence is not happened.
code to pending intent
Intent intent = new Intent(this, TimeAlarm.class);
for(int i=0;i<milliSec.size();i++){
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0,intent, PendingIntent.FLAG_ONE_SHOT);
am.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(),(milliSec.get(i)), pendingIntent);
System.out.println("Calling Alaram...");
Code to display notification
public void onReceive(Context context, Intent intent) {
nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
CharSequence from = "Sample Notification";
CharSequence message = "Notification different milliseconds ...";
PendingIntent contentIntent = PendingIntent.getActivity(context, 0, new Intent(), 0);
Notification notif = new Notification(R.drawable.ic_launcher, "Notification Test...", System.currentTimeMillis());
notif.setLatestEventInfo(context, from, message, contentIntent);
notif.flags= Notification.FLAG_AUTO_CANCEL;
nm.notify(1, notif);
}
How to do multiple notification sequence without clearing the existing message from notification. Thanks in advance
use this line of code
nm.notify( System.currentTimeMillis(), notif);
You have set it to 1 so every time it overrites notification
You always passing same requestcode in intent. so just need to change request code.
PendingIntent contentIntent = PendingIntent.getActivity(context, request_code, new Intent(), 0);
also need to change notify id.
nm.notify(change_notify_id, notif);
I have sqlite database table which contacts friend and his contacts with date of birth.I want to ring Alarm when any date match from database with current date.Please Anyone help me How I do this task.
Actually I want to Match date 2 time a day daily please suggest me.
I am using this code
private void setAlarm(String name,String date)
{
String[] arrdob =date.split("/");
Calendar cal = Calendar.getInstance(); //for using this you need to import java.util.Calendar;
// add minutes to the calendar object
cal.set(Calendar.MONTH,Integer.parseInt(arrdob[1]));
cal.set(Calendar.YEAR, Integer.parseInt(arrdob[2]));
cal.set(Calendar.DAY_OF_MONTH,Integer.parseInt(arrdob[0]));
cal.set(Calendar.HOUR_OF_DAY,14);
cal.set(Calendar.MINUTE,35);
System.out.println("1");
Intent alarmintent = new Intent(getApplicationContext(), AlarmReceiver.class);
alarmintent.putExtra("title","B'Day Alarm");
alarmintent.putExtra("subject","Hi!Todays "+name+" B'Day");
PendingIntent sender = PendingIntent.getBroadcast(getApplicationContext(), HELLO_ID,
alarmintent,PendingIntent.FLAG_UPDATE_CURRENT | Intent.FILL_IN_DATA);
//VERY IMPORTANT TO SET FLAG_UPDATE_CURRENT... this will send correct extra's informations to
//AlarmReceiver Class
// Get the AlarmManager service
AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
am.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), sender);
}
call this method at when insert value into database.
AlarmReceiver class is as:
private static int NOTIFICATION_ID = 1;
#Override
public void onReceive(Context context, Intent intent) //
{
// NotificationManager mNotificationManager =
System.out.println("2");
//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, AlarmReceiver.class), 0);
Bundle extras=intent.getExtras();
String title=extras.getString("title");
//here we get the title and description of our Notification
String note=extras.getString("subject");
notification.setLatestEventInfo(context, note, title, contentIntent);
notification.flags = Notification.FLAG_INSISTENT;
notification.defaults |= Notification.DEFAULT_SOUND;
//here we set the default sound for our
//notification
// The PendingIntent to launch our activity if the user selects this notification
manger.notify(NOTIFICATION_ID++, notification);
}
I would like to ask which service and how to use to make reminder in android... Let's say: after 10 minutes from now show me notification in notification bar...
Thanks for your answer
Obviously you should use AlarmManager in order to setup something to be executed in given PERIOD.
AlarmManager mgr = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Intent i = new Intent(context, OnAlarmReceiver.class);
PendingIntent pi = PendingIntent.getBroadcast(context, 0, i, 0);
mgr.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,
SystemClock.elapsedRealtime(), PERIOD, pi);
where the PERIOD is your time to something that should be executed in OnAlarmReceiver.
And then, just implement method in
#Override
public void onReceive(Context context, Intent intent) {
NotificationManager nm = (NotificationManager);
context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification();
notification.tickerText = "10 Minutes past";
nm.notify(0, notification);
}
Enjoy.
You should use AlarmManager. With it you can schedule an intent to be delivered. Create a BroadcastReceiver to get it and show the notification.
Somethink like this i guess, you start an runnable after 10min and open an notification in the runnable's code.
Runnable reminder = new Runnable()
{
public void run()
{
int NOTIFICATION_ID = 1324;
NotificationManager notifManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Notification note = new Notification(R.drawable.icon, "message", System.currentTimeMillis());
// Add and AUTO_CANCEL flag to the notification,
// this automatically removes the notification when the user presses it
note.flags |= Notification.FLAG_AUTO_CANCEL;
PendingIntent i = PendingIntent.getActivity(this, 0, new Intent(this, ActivityToOpen.class), 0);
note.setLatestEventInfo(this, "message", title, i);
notifManager.notify(NOTIFICATION_ID, note);
}
};
Handler handler = new Handler();
handler.postDelayed(reminder, 600000);
I currently have an app which, when a button is pressed, and after a certain period of time, a statusbar notification is set.
Everything works fine apart from the fact if the user does not have the application open, when the notification appears, the app also reopens. This is not what i would like to happen. I would like the notification to appear on it's own (wherever the user is).
On my button press i use:
// get a Calendar object with current time
Calendar cal = Calendar.getInstance();
// add minutes to the calendar object
cal.add(Calendar.SECOND, time);
Intent alarmintent = new Intent(getApplicationContext(), AlarmReceiver.class);
alarmintent.putExtras(bundle);
// In reality, you would want to have a static variable for the request code instead of 192837
PendingIntent sender = PendingIntent.getBroadcast(getApplicationContext(), HELLO_ID, alarmintent, 0);
// Get the AlarmManager service
AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
am.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), sender);
This calls my AlarmReceiver.class, which uses this code to call my notification class:
Intent myIntent = new Intent(context, Note.class);
myIntent.putExtras(bundle2);
myIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_CLEAR_TOP);
context.startActivity(myIntent);
notification.class:
public class Note extends Activity {
/** Called when the activity is first created. */
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String ns = Context.NOTIFICATION_SERVICE;
final NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);
int icon = R.drawable.launcher;
CharSequence tickerText = "Remind Me!";
long when = System.currentTimeMillis();
final Notification notification = new Notification(icon, tickerText, when);
notification.flags |= Notification.FLAG_AUTO_CANCEL | Notification.FLAG_NO_CLEAR;
final Context context = getApplicationContext();
CharSequence contentTitle = "Remind Me!";
CharSequence contentText = param1;
Intent notificationIntent = new Intent(context, Completed.class);
PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
mNotificationManager.notify(HELLO_ID, notification);
HELLO_ID++;
}
}
This is the expected result, as the Intent you create in your AlarmReceiver class explicitly launches your Note Activity.
Why not simply create the Notification in your AlarmReceiver? (rather than launching your activity)