Search code examples
androidnotificationsalarmmanager

Android - notification repeats after opening activity


I'm trying to build application which will send notification daily at specific time.. I'm using .setOngoing(true) and .setAutoCancel(true) because I want to stick notification unless user open application by tapping on notification. but when application opens notification disappears and show again after few seconds.. how to stop showing notification when open application but only at specific time?

MainActivity code is:

    public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.HOUR_OF_DAY, 16);
        calendar.set(Calendar.MINUTE, 6);
        calendar.set(Calendar.SECOND, 0);
        Intent intent = new Intent(MainActivity.this, AlarmReceiver.class);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(MainActivity.this, 0,intent, PendingIntent.FLAG_UPDATE_CURRENT);
        AlarmManager am = (AlarmManager) MainActivity.this.getSystemService(MainActivity.this.ALARM_SERVICE);
        am.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);

    }

}

AlarmReceiver Class is :

    public class AlarmReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
    NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

    Intent intent1 = new Intent(context, MainActivity.class);
    intent1.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

    PendingIntent pendingIntent = PendingIntent.getActivity(context, 100, intent1, PendingIntent.FLAG_UPDATE_CURRENT);

    NotificationCompat.Builder builder = (NotificationCompat.Builder) new NotificationCompat.Builder(context)
            .setSmallIcon(R.drawable.ic_stat_name)
            .setContentIntent(pendingIntent)
            .setContentText("notification text")
            .setContentTitle("notification title")
            .setAutoCancel(true)
            .setOngoing(true);
    notificationManager.notify(100, builder.build());

}

}


Solution

  • In your mainactivity put this code:-

    Calendar calendar = Calendar.getInstance();
    calendar.set(Calendar.HOUR_OF_DAY, 1);
    calendar.set(Calendar.MINUTE, 49);
    calendar.set(Calendar.SECOND, 0);
    calendar.set(Calendar.AM_PM,Calendar.PM);
    
    if(calendar.getTimeInMillis()>System.currentTimeMillis()){
    
         Intent intent = new Intent(MainActivity.this, AlarmReceiver.class);
         pendingIntent = PendingIntent.getBroadcast(MainActivity.this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
         AlarmManager am = (AlarmManager) MainActivity.this.getSystemService(MainActivity.this.ALARM_SERVICE);
         am.setInexactRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 24*60*60*1000, pendingIntent); //Repeat every 24 h
    
    
     }
    else{
    
        Toast.makeText(getApplicationContext(),"Always set value with greater than current time",Toast.LENGTH_SHORT).show();
    }
    

    and change the code on reciever look like this:-

    NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    
            intent = new Intent(context, MainActivity.class);
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
            intent.putExtra("STARTED_BY_RECEIVER", true);
            context.startActivity(intent);
    
            PendingIntent pendingIntent = PendingIntent.getActivity(context, 100, intent, PendingIntent.FLAG_UPDATE_CURRENT);
            NotificationCompat.Builder builder = (NotificationCompat.Builder) new NotificationCompat.Builder(context)
                    .setSmallIcon(R.mipmap.ic_launcher)
                    .setContentIntent(pendingIntent)
                    .setContentText("notification text")
                    .setContentTitle("notification title")
                    .setAutoCancel(true)
                    .setOngoing(true);
            notificationManager.notify(100, builder.build());
    

    Try this!!