1

I created a notification which is shown at specific time, but the problem is that everytime I close the app (after the prefixed time) the noification is shown. How can I fix this issue?

This is my code: Home.class (is a fragment)

Calendar calend = Calendar.getInstance();
        calend.setTimeInMillis(System.currentTimeMillis());
        calend.set(Calendar.HOUR_OF_DAY, 9);
        calend.set(Calendar.MINUTE, 27);
        calend.set(Calendar.SECOND, 0);
        Intent myIntent = new Intent(getActivity(), MyReceiver.class);
        pendingIntent = PendingIntent.getBroadcast(getActivity(), 0, myIntent,0);
        AlarmManager alarmManager = (AlarmManager)getActivity().getSystemService(Context.ALARM_SERVICE);
        alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calend.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);

MyReceiver.class

public class MyReceiver extends BroadcastReceiver
{

    @Override
    public void onReceive(Context context, Intent intent)
    {


       Intent service1 = new Intent(context, MyAlarmService.class);
       context.startService(service1);

    }   
}

MyAlarmService.class

public class MyAlarmService extends Service 
{

   private NotificationManager mManager;

    @Override
    public IBinder onBind(Intent arg0)
    {
       // TODO Auto-generated method stub
        return null;
    }

    @Override
    public void onCreate() 
    {
       // TODO Auto-generated method stub  
       super.onCreate();
    }

   @SuppressWarnings({ "static-access", "deprecation" })
   @Override
   public void onStart(Intent intent, int startId)
   {
       super.onStart(intent, startId);

       mManager = (NotificationManager) this.getApplicationContext().getSystemService(this.getApplicationContext().NOTIFICATION_SERVICE);
       Intent intent1 = new Intent(this.getApplicationContext(),MainActivity.class);

       Notification notification = new Notification(R.drawable.ic_launcher,"This is a test message!", System.currentTimeMillis());
       intent1.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP| Intent.FLAG_ACTIVITY_CLEAR_TOP);

       PendingIntent pendingNotificationIntent = PendingIntent.getActivity( this.getApplicationContext(),0, intent1,PendingIntent.FLAG_UPDATE_CURRENT);
       notification.flags |= Notification.FLAG_AUTO_CANCEL;
       notification.setLatestEventInfo(this.getApplicationContext(), "AlarmManagerDemo", "This is a test message!", pendingNotificationIntent);

       mManager.notify(0, notification);
    }

    @Override
    public void onDestroy() 
    {
        // TODO Auto-generated method stub
        super.onDestroy();
    }

}
Slaiv206
  • 309
  • 2
  • 14
  • Please supply where you are placing the above code. – Kevin Crain Mar 16 '15 at 08:42
  • You actually created an `Alarm` on `alarmManager.setRepeating` which will repeat itslef. Cancel it when you close the app as the Alarm runs on `Service` – hrskrs Mar 16 '15 at 08:43
  • **"How can I fix this issue?"** : Cancel the alarm when you leave the app. – Squonk Mar 16 '15 at 08:44
  • If I cancel the alarm the notification will not shown ?For example if I set the alarm at 10 am, and I cancel alarm when I leave the app, if the user doesn't open the app the day after the notification will show at 10am ? – Slaiv206 Mar 16 '15 at 08:50
  • @Slaiv206 : Sorry but you're not explaining your problem very well. Please explain further explaining what you app does, what the notification is for, what you expect to happen and what is actually happening. All you've done is say your notification is showing when it shouldn't and posted some standard code for setting an alarm which doesn't tell us anything. – Squonk Mar 16 '15 at 08:55
  • The code in `MyReceiver` should be execute only at specified time every day. If a notification is shown when you close the app, this can't come from the alarm manager. Can you provide the code from the Receiver (The notification I suppose ;) ) – AxelH Mar 16 '15 at 08:56
  • @AxelH I post the complete code :) – Slaiv206 Mar 16 '15 at 09:00
  • @Squonk I posted the code. The problem is that everytime in the moment I exit the app(when in the device I press the central button and cancel the application in background so not when I press back button) the notification is show. I want just to show a notification in a specific hour and even if the application is close the notification will be show, but not everytime I close the app in background – Slaiv206 Mar 16 '15 at 09:01

2 Answers2

1

I have an answer for this.

don't use service for the same. you just need to write the below code in your MyReceiver class and it will not give you notification when you kill the app from task manager.

public class AlarmReceiver extends BroadcastReceiver {
        private NotificationManager mManager;

        private static final int MY_NOTIFICATION_ID = 1;
        private static final String TAG = "AlarmNotificationReceiver";

        // Notification Text Elements
        private final CharSequence tickerText = "its ok?";
        private final CharSequence contentTitle = " Reminder1";
        private final CharSequence contentText = "reminder content";

        // Notification Action Elements
        private Intent mNotificationIntent;
        private PendingIntent mContentIntent;

         @Override
         public void onReceive(Context context, Intent intent) {
         // When our Alaram time is triggered , this method will be excuted (onReceive)



             mNotificationIntent = new Intent(context, MyView.class);

             // The PendingIntent that wraps the underlying Intent
             mContentIntent = PendingIntent.getActivity(context, 0,mNotificationIntent,PendingIntent.FLAG_UPDATE_CURRENT);

             // Build the Notification
             Notification.Builder notificationBuilder = new Notification.Builder(
                     context).setTicker(tickerText)
                     .setSmallIcon(android.R.drawable.stat_sys_warning)
                     .setAutoCancel(true).setContentTitle(contentTitle)
                     .setContentText(contentText).setContentIntent(mContentIntent);

             // Get the NotificationManager
             NotificationManager mNotificationManager = (NotificationManager)context
                     .getSystemService(Context.NOTIFICATION_SERVICE);

             // Pass the Notification to the NotificationManager:
             mNotificationManager.notify(MY_NOTIFICATION_ID,
                     notificationBuilder.build());

        }

    } 
Baqir
  • 717
  • 1
  • 7
  • 20
0

Has you can read in this post : Will AlarmManager work if my application is not running? The AlarmManager keep you Alarm until the device is reboot OR if you kill the app. Remove it from the recent list will kill the app.

This did not explain why the notification is shown but I guess the system execute your request during the killing. It should just remove it but maybe (no proof here) it prefere to send the broadcast insteed

Community
  • 1
  • 1
AxelH
  • 14,325
  • 2
  • 25
  • 55
  • ok, but I try to kill the app a minute before the time set for notification and when it was the right time the notification was shown, even if I kill the app. And I see that the notification is show also after I kill the app both in the precise moment I close the app and some time later. – Slaiv206 Mar 16 '15 at 09:18
  • Can you edit you question with all the test you have done. Cause this isn't clear right now. – AxelH Mar 16 '15 at 09:24