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)
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 have been searching for this since morning and referred to most of the android alarm problems on stackoverflow.
I am trying to set multiple alarms with different intents. On receiving the the alarm, I want the alarm to be cancelled and the activity to come in front, in case its already running, or start again if it was killed, but this time the alarm shouldnt be set again. I dont want the other alarms to be effected.
Currently, the problem is that clicking on the notification starts the activity again and resets the alarm. If I try to cancel it using alarmmanager.cancel, it doesnt notify the user at all.
Here is my code, please help
My MainActivity thats sets multiple alarms
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Calendar cal = Calendar.getInstance(); //for using this you need to import java.util.Calendar;
// add minutes to the calendar object
cal.set(Calendar.DAY_OF_WEEK,Calendar.MONDAY);
cal.set(Calendar.HOUR_OF_DAY, 22);
cal.set(Calendar.MINUTE, 8);
// cal.add(Calendar.MINUTE, 1);
AlarmManager mgrAlarm = (AlarmManager) this.getSystemService(ALARM_SERVICE);
ArrayList<PendingIntent> intentArray = new ArrayList<PendingIntent>();
for(int i = 0; i < 10; ++i)
{
Intent intent = new Intent(this, AlarmReceiver.class);
intent.putExtra("title", "notification no."+String.valueOf(i));
intent.putExtra("NOTIFICATION_ID", String.valueOf(i));
// Loop counter `i` is used as a `requestCode`
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, i, intent, 0);
// Single alarms in 1, 2, ..., 10 minutes (in `i` minutes)
mgrAlarm.set(AlarmManager.ELAPSED_REALTIME_WAKEUP,
SystemClock.elapsedRealtime() + 60000 * i,
pendingIntent);
intentArray.add(pendingIntent);
}
}
My AlarmReceiver Class
public class AlarmReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
NotificationManager manger = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(R.drawable.ic_launcher, "Alarm App", System.currentTimeMillis());
Bundle extras=intent.getExtras();
String title=extras.getString("title");
int notif_id=Integer.parseInt(extras.getString("NOTIFICATION_ID"));
//here we get the title and description of our Notification
Class myclass = MainActivity.class;
PendingIntent contentIntent = PendingIntent.getActivity(context, notif_id,
new Intent(context, MainActivity.class), 0);
String note=extras.getString("note");
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(notif_id, notification);
}
};
In your MainActivity, you can differentiate a launch from the Notification with an additional parameter in the intent. You would anyway need the notification id to cancel the particular notification. So, you can try the following in your MainActivity
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent appIntent = this.getIntent();
int notif_id = appIntent.getIntExtra( "my_notification_id", -1 ) ;
if( notif_id != -1 )
{
Log.d ( "LOG_TAG", "Launched from Notification ");
NotificationManager nm = (NotificationManager) getSystemService( NOTIFICATION_SERVICE );
nm.cancel( notif_id );
/* Do the separate processing here */
.....
}
else
{
/* Your previous onCreate code goes here */
In the file AlarmReceiver.java, you need to make the following changes
//PendingIntent contentIntent = PendingIntent.getActivity(context, notif_id, new Intent(context, MainActivity.class), 0);
Intent appIntent = new Intent( context, MainActivity.class );
appIntent.putExtra( "my_notification_id", notif_id );
PendingIntent contentIntent = PendingIntent.getActivity(context, notif_id, appIntent, 0);
Hope this helps.
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);
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);
}
hi ia m new to android..
i have developed a programme to set notification onspecific time
but the problem is it shows notification only of date before october 2011
if i enter any date of november its not showing any notification..
seems strange but really stuck here....
main activity..
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Calendar cal = Calendar.getInstance(); // for using this you need to
cal.set(Calendar.MONTH, 11);
cal.set(Calendar.YEAR, 2011);
cal.set(Calendar.DAY_OF_MONTH, 25);
cal.set(Calendar.HOUR_OF_DAY, 18);
cal.set(Calendar.MINUTE, 28);
Intent alarmintent = new Intent(getApplicationContext(),
AlarmReceiver.class);
alarmintent.putExtra("title", "Title of our Notification");
alarmintent.putExtra("note", "Description of our Notification");
int HELLO_ID = 1;
PendingIntent sender = PendingIntent.getBroadcast(
getApplicationContext(), HELLO_ID, alarmintent,
PendingIntent.FLAG_UPDATE_CURRENT |
Intent.FILL_IN_DATA);
AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
am.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), sender);
}
and the alarm reciever activity...
public class AlarmReceiver extends BroadcastReceiver {
private static final String NotificationManager = null;
private static int NOTIFICATION_ID = 0;
#Override
public void onReceive(Context context, Intent intent) {
(NotificationManager)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");
String note = extras.getString("note");
notification.setLatestEventInfo(context, note, title, contentIntent);
notification.flags = Notification.FLAG_INSISTENT;
notification.defaults |= Notification.DEFAULT_SOUND;
manger.notify(NOTIFICATION_ID, notification);
}
};
Calendar.set(Calendar.MONTH, month)
Expects a 0-based value. 11 is december.
You are setting an alarm for 5/5/2011, this date has passed so it is executed immediately.
When you receive a instance of Calendar, I think that the system automatically fills in the current date & time. So change only what you need to change (I.E if you want to send a notification in 5 days, change the day only in the calendar instance)
By the way, when clicking the notification it will, again, call your BroadcastReceiver and this time the intent won't have the extras you have put in it while in the activity. When clicking the notification, you should launch an activity - not a broadcast receiver.