Notification disappears then reappears when clicked on? - android

Hi I have a notifcation set to go off on a specific day and time. Here is my code -
private void createPushTimer() {
// TODO Auto-generated method stub
Calendar calendar = Calendar.getInstance();
Intent intent = new Intent(MainActivity.this, Drawing.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(MainActivity.this, 2, intent, 0);
AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE);
int day = calendar.get(Calendar.DAY_OF_WEEK);
if (day == 4) { //5 is thurs
calendar.set(Calendar.HOUR_OF_DAY, 9);
calendar.set(Calendar.MINUTE, 48);
calendar.set(Calendar.SECOND, 0);
am.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY*7, pendingIntent);
}
//am.setRepeating(AlarmManager.RTC_WAKEUP, 10000, AlarmManager.INTERVAL_DAY, pendingIntent);
}
Here is the receiving class for the notification -
public void showNotification(Context context) {
Intent intent = new Intent(context, MainActivity.class);
PendingIntent pi = PendingIntent.getActivity(context, 0, intent, 1);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("Drawing!")
.setContentText("The new drawing has been updated! Check the new numbers!")
.setAutoCancel(true);
mBuilder.setContentIntent(pi);
mBuilder.setDefaults(Notification.DEFAULT_SOUND);
//mBuilder.setAutoCancel(true);
NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(0, mBuilder.build());
}
It goes off at the appropriate time (9:48 on wednesday for testing purposes) but when notification is clicked on, it appears again at the start of the app. It does disappear for a second but then reemerges. Even though it is the time when clicked on. Is there a better way of doing this or am I missing something? Thanks for any help.

Related

Changing a variable from Notification Method

I added a notification feature to my app, when the user clicks on the notification it sends him to a certain activity in my app, but I also want it to change an integer variable number, I tried defining the variable static and do:
ActivityName.Variable = ActivityName.Variable + 1 or ActivityName.Variable++;
And it actually worked! but only when I change the date on my phone manually, so when the user sees the notification after waiting 1 day (the notification is daily) and clicks on it...he get to the activity but with no increase in the variable.
here's the notification codes:
public void setNotificationOn() { //This method sets the notification...so after 24h it will be triggered
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, 24);
Intent intent = new Intent(getApplicationContext(), NotificationReceiverActivity.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(),
100, intent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);
}
package com.example.wordspuzzlejsontest;
public class NotificationReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
LevelActivity.stars++; //This is the variable which I'm trying to increase
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
Intent repeating_intent = new Intent(context, LevelsListActivity.class);
repeating_intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity
(context, 100, repeating_intent, PendingIntent.FLAG_CANCEL_CURRENT);
NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
.setContentIntent(pendingIntent)
.setSmallIcon(android.R.drawable.star_big_off)
.setContentTitle("Collect your daily star")
.setContentText("Click to collect your daily star")
.setAutoCancel(true)
.setColor(Color.BLUE);
notificationManager.notify(100, builder.build());
}
}

Android notification at exact time

For the life of me, I can't figure out why I can't set an exact time for an android notification. Been trying to figure this out for a few days, and still don't understand why.
Sometimes, it works on the exact same time from the time picker. But, sometimes it's 20 seconds, 34 seconds or even a full minute late. If someone could help me out, It'd be much appreciated because I don't know what else to do. I'm setting the time to the alarm manager from date milliseconds.
Broadcast Receiver:
public class AlertReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
int code = intent.getIntExtra("code", 0);
Notification notification = new NotificationCompat.Builder(context)
.setContentTitle("Task reminder!")
.setTicker(intent.getStringExtra("taskText"))
.setContentText(intent.getStringExtra("taskText"))
.setSmallIcon(R.drawable.ic_stat_name)
.setContentIntent(PendingIntent.getActivity(context, code, new Intent(context, MainActivity.class), 0))
.setAutoCancel(true)
.setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
.build();
Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
notification.defaults = Notification.DEFAULT_SOUND;
notificationManager.notify(code, notification);
}
}
MainActivity:
int code = new Random().nextInt(1200) +
AlarmManager alarmMgr = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(getApplicationContext(), AlertReceiver.class);
intent.putExtra("taskText", taskInput.getText().toString());
intent.putExtra("code", code);
PendingIntent alarmIntent = PendingIntent.getBroadcast(getApplicationContext(), code, intent, 0);
alarmMgr.set(AlarmManager.RTC, reminder, alarmIntent);
Intent i = new Intent(getApplicationContext(), MainActivity.class);
startActivity(i);
Getting my time from this:
Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(System.currentTimeMillis());
cal.set(Calendar.YEAR, datePicker.getYear());
cal.set(Calendar.MONTH, datePicker.getMonth());
cal.set(Calendar.DAY_OF_MONTH, datePicker.getDayOfMonth());
cal.set(Calendar.HOUR_OF_DAY, timePicker.getCurrentHour());
cal.set(Calendar.MINUTE, timePicker.getCurrentMinute());
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0);
Fixed it! Replaced set() with setExact().
Thanks, Mike!

How to update broadcast receiver. (multiple notification)

Variables in BroadcastReceiver are not updating every time i set a notification/alarm manager.
"receiver (recycler)" is from a fragment.
receiver" is from a BroadcastReceiver class.
onCreateView
intentAlarmManager = new Intent(context, NotificationReceiver.class);
pendingIntent = PendingIntent.getBroadcast(context, 0, intentAlarmManager, PendingIntent.FLAG_UPDATE_CURRENT);
Notification Method
private void setNotification(int hour, int min, int interval, int uniqueID) {
//get instance of the calendar
calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, hour);
calendar.set(Calendar.MINUTE, min);
//create delayed intent
pendingIntent = PendingIntent.getBroadcast(context, uniqueID, intentAlarmManager, PendingIntent.FLAG_UPDATE_CURRENT);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 1000 * (interval * 30), pendingIntent);
}
Recycler its attached to a switch listener
setNotification(Integer.parseInt(model.getHour()), Integer.parseInt(model.getMinute()), Integer.parseInt(model.getInterval()), Integer.parseInt(model.getTime()));
Receiver
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
Intent intentToStartWhenAlarmSets = new Intent(context, LoginActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(context, (int) System.currentTimeMillis(), intentToStartWhenAlarmSets, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
.setContentIntent(pendingIntent)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("Content Title")
.setContentText("Notify " + HomeFragment.notifMedName)
.setSound(notifSound)
.setVibrate(pattern)
//swipable
.setAutoCancel(true);
Log.d(ContentValues.TAG, "receiver " + HomeFragment.notifMedName);
notificationManager.notify((int) System.currentTimeMillis(), builder.build());
I figured it out, i think. Although I'm not sure if this is the best/correct way (i'ts working though).
So in in my receiver class i added
intent.getStringExtra("string);
and in my fragment i added
intentAlarmManager.putExtra("string", notifMedName);
getActivity().sendBroadcast(intentAlarmManager);
and also i changed all pending intent from context to getActivity().
Feel free to answer if you guys have a better solution.
More info here.

Issue with AlarmReceiver Android

Introduction:
Through a ToggleButton I make my user to enable or disable the alarm reminder. The alarm remember my user to record their activity one hour after the last time he has entered the app also do not want the alarm to sound at night. This all works fine.
public void each_hour_alarm() {
Calendar cal = Calendar.getInstance();
cal.setTime(new Date());
int hour = cal.get(Calendar.HOUR_OF_DAY);
if(hour <= 20 && hour >= 7) {
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Intent notificationIntent = new Intent("android.media.action.DISPLAY_NOTIFICATION");
notificationIntent.addCategory("android.intent.category.DEFAULT");
PendingIntent broadcast = getBroadcast(getApplicationContext(), 100, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.HOUR, 1);
alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), broadcast);
stop_alarm_9am();
}else {
stop_each_hour_alarm();
alarm_9am();
}
}
The problem begins when I call a new method: alarm_9am();
The new method raises an alarm at 9 am if the user has not entered before the app. This is where I have the problem. The alarm sounds at 9am, but also, and this is my problem, it also sounds every time I log activity between 21h and 23: 59h.
I do not understand why this happens. Any ideas??
This is de code:
public void alarm_9am() {
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Intent notificationIntent = new Intent("android.media.buenosdias.action.DISPLAY_NOTIFICATION");
notificationIntent.addCategory("android.intent.buenosdias.category.DEFAULT");
PendingIntent broadcast = getBroadcast(getApplicationContext(), 100, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, 9);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 1);
alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), broadcast);}
Code to stop alarm_9am();
public void stop_alarm_9am() {
Intent intentlocal = new Intent("android.media.buenosdias.action.DISPLAY_NOTIFICATION");
intentlocal.addCategory("android.intent.buenosdias.category.DEFAULT");
PendingIntent pilocal = getBroadcast(getApplicationContext(), 100, intentlocal, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.cancel(pilocal);
pilocal.cancel();
}
And my AlarmReceiver;
public class AlarmReceiver_buenos_dias extends BroadcastReceiver{
#Override
public void onReceive(Context context, Intent intent) {
Intent notificationIntent = new Intent(context, v_agua.class);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
stackBuilder.addParentStack(v_agua.class);
stackBuilder.addNextIntent(notificationIntent);
PendingIntent pendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
Notification notification = builder.setContentTitle("Good Morning")
.setContentText("Begin New Day")
.setTicker("Go to app")
.setSmallIcon(R.drawable.logo_launcher_a)
.setSound(Settings.System.DEFAULT_NOTIFICATION_URI)
.setStyle(new NotificationCompat.BigTextStyle().bigText(""))
.setAutoCancel(true)
.setContentIntent(pendingIntent).build();
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, notification);
}}
My issue is that the alarm sounds between 21h to 23:59 every time I enter in activity and i dont know why. I need not sound the alarm every time I log in activity between 21h and 23:59, I just want alarm_9am(); sounds at 9am
Thanks!

Notification sent to user at a specific time Android

I'm trying to send a notification to a user at a specific time using an Alarm Manager. Basically, nothing happens at all and to me the code looks fine. My code for the Alarm Manager is below:
/** Notify the user when they have a task */
public void notifyAtTime() {
Intent myIntent = new Intent(PlanActivity.this , Notification.class);
AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
PendingIntent pendingIntent = PendingIntent.getService(PlanActivity.this, 0, myIntent, 0);
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, 12);
calendar.set(Calendar.MINUTE, 17);
calendar.set(Calendar.SECOND, 00);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 24*60*60*1000 , pendingIntent);
}
The code for the notification, which is in the "Notification" class is below:
public class Notification extends Service {
#Override
public void onCreate() {
Toast.makeText(this, "Notification", Toast.LENGTH_LONG).show();
Intent intent = new Intent(this,PlanActivity.class);
PendingIntent pending = PendingIntent.getActivity(this, 0, intent, 0);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
this).setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("Football")
.setContentText("Don't forget that you have Football planned!")
.setContentIntent(pending);
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
manager.notify(0, mBuilder.build());
}
#Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
}
The Toast which is set in the Notification class also doesn't come up. I don't know if it's something really silly that I'm missing but any help would be greatly appreciated! :)
This is your receiver class :
public class OnetimeAlarmReceiver extends BroadcastReceiver {
public void onReceive(Context context, Intent intent) {
//Toast.makeText(context, "Repeating Alarm worked.", Toast.LENGTH_LONG).show();
// try here
// prepare intent which is triggered if the
// notification is selected
Intent intent = new Intent(this, NotificationReceiver.class);
PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent, 0);
// build notification
// the addAction re-use the same intent to keep the example short
Notification n = new Notification.Builder(this)
.setContentTitle("New mail from " + "test#gmail.com")
.setContentText("Subject")
.setSmallIcon(R.drawable.icon)
.setContentIntent(pIntent)
.setAutoCancel(true)
.addAction(R.drawable.icon, "Call", pIntent)
.addAction(R.drawable.icon, "More", pIntent)
.addAction(R.drawable.icon, "And more", pIntent).build();
NotificationManager notificationManager =
(NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationManager.notify(0, n);
}
}
In Second Activity to set Alerm :
private void setAlarm(){
Intent intent = new Intent(this, OnetimeAlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, REQUEST_CODE, intent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + alarm_time , pendingIntent);
System.out.println("Time Total ----- "+(System.currentTimeMillis()+total_mili));
}
I would like to recommend this tutorial How to send notification
Try to put your notification code in the onStartCommand function instead of onCreate.
Writing the alarmmanager code in the onCreate method of the LAUNCHER activity of the app solved my problem

Categories

Resources