Notification is triggered again and again - android

I am creating a notification on a activity called Mainmenu using alarm here is code
Intent myIntent = new Intent(MainMenu.this, SyncService.class);
AlarmManager alarmManager = (AlarmManager)context.getSystemService(ALARM_SERVICE);
pendingIntent = PendingIntent.getService(MainMenu.this, 1, myIntent,0);
Calendar calforAlram = Calendar.getInstance();
calforAlram.set(Calendar.HOUR_OF_DAY, 20);
calforAlram.set(Calendar.MINUTE, 46);
calforAlram.set(Calendar.SECOND, 0);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calforAlram.getTimeInMillis(), 24 * 60 * 60 * 1000, pendingIntent);
alarmManager.cancel(pendingIntent);
On notification received , on click of the notification i have to open the same Mainmenu activity.
Problem: again one more notification is generated , again click on notification again Mainmenu activity will open and continues
Here is Notification code
NotificationManager mManager = (NotificationManager) getApplicationContext().getSystemService(this.getApplicationContext().NOTIFICATION_SERVICE);
Intent intent1 = new Intent(this.getApplicationContext(),MAFLogonActivity.class);
NotificationCompat.Builder notification = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.icon)
.setContentTitle("XXXXXXXX")
.setContentText("Please sync data.").setAutoCancel(true);
intent1.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pendingNotificationIntent = PendingIntent.getActivity(this.getApplicationContext(), 1, intent1, 0);
notification.setContentIntent(pendingNotificationIntent);
Log.d("On service", "Alarms set for everyday 2 pm.");
mManager.notify(0, notification.build());
return Service.START_NOT_STICKY;

You should check the application has started from push notification or normal.
How to do this.
Notification generate code
Intent intent1 = new Intent(this.getApplicationContext(),MAFLogonActivity.class);
intent1.putExtra("isFromNotification", true);
onCreate() of MAFLogonActivity.java
boolean isFromNotification = getIntent().getBooleanExtra("isFromNotification", false);
if(!isFromNotification){
Intent myIntent = new Intent(MainMenu.this, SyncService.class);
AlarmManager alarmManager = (AlarmManager) context.getSystemService(ALARM_SERVICE);
pendingIntent = PendingIntent.getService(MainMenu.this, 1, myIntent, 0);
Calendar calforAlram = Calendar.getInstance();
calforAlram.set(Calendar.HOUR_OF_DAY, 20);
calforAlram.set(Calendar.MINUTE, 46);
calforAlram.set(Calendar.SECOND, 0);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calforAlram.getTimeInMillis(), 24 * 60 * 60 * 1000, pendingIntent);
alarmManager.cancel(pendingIntent);
}
Hope this would help you.

Related

Alarm manager for notification is not working properly

I have used alarm manager in Android for showing some notifications, but it is not working properly.
My code in activity is:
Intent i = new Intent(this, AlarmBroadcastReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, i, 0);
AlarmManager alarmManager = (AlarmManager)this.getSystemService(Context.ALARM_SERVICE);
alarmManager.cancel(pendingIntent);
alarmManager.setExact(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + (int) (10*60 * 1000),
pendingIntent);
And the code for receiving a notification after 15 minutes is:
AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(context, AlarmBroadcastReceiver.class);
PendingIntent pi = PendingIntent.getService(context, 0, intent, 0);
am.cancel(pi);
am.setExact(AlarmManager.ELAPSED_REALTIME_WAKEUP,
SystemClock.elapsedRealtime() +
(int) ( 5*60 * 1000), pi);
use this type of AlarmManager when you use service than you need to change getService insted of getBroadCast. sometime receiver not call and also creat issue in some devices when app in background
Intent intent = new Intent(myContext, SetAlarmService.class);
intent.putExtra("salah",i);
intent.putExtra("repeat",HowManyTimeRepeatAlarm);
PendingIntent pendingIntent = PendingIntent.getService(
myContext, i, intent, 0);
AlarmManager alarmManager = (AlarmManager) myContext.getSystemService(ALARM_SERVICE);
// alarmManager.cancel(pendingIntent);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, reset_cal.getTimeInMillis(),60000 * 2 ,
pendingIntent);

Notification comes again when app starts

As a beginner, I wrote a code for notification. I put that code in MainActivity.
Now the problem is, my app shows notification at sharp 8 AM. But During other time, if i restart my app, it shows notification again(at any other time). Basically I am not cancelling it notification as it appears. Though I have put AutoCancel true. Am i missing something?
Code in MainActivity, onCreateMethod:
calendar = Calendar.getInstance();
PendingIntent pendingIntent ;
calendar.set(Calendar.HOUR_OF_DAY, 8);
calendar.set(Calendar.MINUTE,00);
calendar.set(Calendar.SECOND, 00);
Intent intent = new Intent(MainActivity.this, NotificationBroadcasrReceiver.class);
intent.putExtra("NotificationType","GoodMorning");
pendingIntent = PendingIntent.getBroadcast(
getBaseContext(), (int)System.currentTimeMillis(), intent,
PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager am = (AlarmManager) getBaseContext()
.getSystemService(getBaseContext().ALARM_SERVICE);
am.setInexactRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
AlarmManager.INTERVAL_DAY, pendingIntent);
After this it comes to BroadCast Receiver :
the code inside onReceive is
Intent intentNew = new Intent(context, CustomIntentService.class);
intentNew.putExtra("NotificationType",intent.getStringExtra("NotificationType"));
context.startService(intentNew);
Now custom intent servcice class has following code..
Intent notificationIntent = new Intent(this,CustomActivity.class);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,
notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this);
mBuilder.setSmallIcon(R.drawable.happiness_icon);
mBuilder.setAutoCancel(true);
mBuilder.setContentIntent(pendingIntent);
mBuilder.setContentTitle("Positive Minds");
mBuilder.setCustomBigContentView(remoteViews);
mBuilder.setVibrate(new long[]{1000, 1000, 1000, 1000, 1000});
NotificationManager mNotificationManager = (NotificationManager)this
.getSystemService(this.NOTIFICATION_SERVICE);
mNotificationManager.notify((int)System.currentTimeMillis(), mBuilder.build());
I used below mentioned code to fix this.
if(calendar1.getTime().compareTo(new Date()) < 0)
calendar1.add(Calendar.DAY_OF_MONTH, 1);

How to add alarm sound or ringtone to apk?

Can I add alarm sound or ringtone to apk ?
I need to add to alarm sound to the application we developing.
Thanks!
Steps you need to do:
Add song that you want to add into res->raw folder.
Then in your activity write this:
Intent AlarmIntent = new Intent(getApplicationContext(), AlarmReceiver.class);
AlarmIntent.putExtra("Ringtone",
Uri.parse("getResources().getResourceName(R.raw.shankh_final_mid)"));
PendingIntent Sender = PendingIntent.getBroadcast(this, 0, AlarmIntent, 0);
AlarmManager AlmMgr = (AlarmManager)getSystemService(ALARM_SERVICE);
AlmMgr.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() +
(60 * 1000), (24 * 60 * 60 * 1000), Sender);
After that in your reciever class add this line of codes:
public void onReceive(Context context, Intent intent) {
Intent in = new Intent(context, SnoozeEvent.class);
in.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent Sender = PendingIntent.getActivity(context, 0, in, PendingIntent.FLAG_UPDATE_CURRENT);
manager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
notification = new Notification(R.drawable.icon, "Wake up alarm", System.currentTimeMillis());
notification.setLatestEventInfo(context, "Hanuman Chalisa", "Wake Up...", Sender);
notification.flags = Notification.FLAG_INSISTENT;
notification.sound = Uri.parse("android.resource://my.package.name/raw/notification");
manager.notify(1, notification); }

Notification disappears then reappears when clicked on?

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.

How to run a background service to give alerts in android?

I am working on an android application but the problem is that I have to alert user through a message on notification bar which will occur after ten(10) days ,like in candy crush game it gives alert of full lives after some hours. I am a beginner in android so I am confused about the implementation of this scenario. Please guide me of its implementation with code.
You can do so with the AlarmManager and the BroadcastReceiver
AlarmManager
Intent myIntent = new Intent(this, TimeAlarm.class);
pendingIntent = PendingIntent.getService(AndroidAlarmService.this, 0, myIntent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.add(Calendar.DAY_OF_MONTH, 10);
alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
BroadcastReceiver
public class TimeAlarm extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent paramIntent) {
// Request the notification manager
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
// Create a new intent which will be fired if you click on the notification
Intent intent = new Intent("android.intent.action.VIEW");
// Attach the intent to a pending intent
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
// Create the notification
Notification notification = new Notification(R.drawable.icon, "Something new!"), System.currentTimeMillis());
notification.setLatestEventInfo(context, "Something new!", "Description",pendingIntent);
// Fire the notification
notificationManager.notify(1, notification);
}
}

Categories

Resources