I would like my app to show a notification daily at a specific time. To do this I have put the following code in the onCreate method of my MainActivity class (which is opened by default when the app is started).
Mainactivity.class
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setNotification(Mainactivity.this);
}
public void setNotification(Context context) {
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY,12);
calendar.set(Calendar.MINUTE,1);
calendar.set(Calendar.SECOND,0);
long c = System.currentTimeMillis();
long t = calendar.getTimeInMillis();
if (t <= c) {
calendar.add(Calendar.DATE, 1);
}
Intent intent = new Intent(this, NotificationReciever.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(MainActivity.this, 100, intent,
PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE); alarmManager.set(AlarmManager,RTC_WAKEUP,calendar.getTimeInMillis(),pendingIntent);
boolean alarmUp = (PendingIntent.getBroadcast(MainActivity.this, 100,
new Intent(this, NotificationReciever.class),
PendingIntent.FLAG_NO_CREATE) != null);
if (alarmUp) {
Toast.makeText(MainActivity.this,"Aan",Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(MainActivity.this,"Niet aan",Toast.LENGTH_SHORT).show();
}
}
Then in the class NotificationReceiver I simply construct and initiate the notification. It does this at the correct time, but also always when the app is started. I do not know how to stop the notification from stopping to initiate whenever the app is started, can someone help me with this?
One thing that I have tried is to check if the current time is larger than or equal to the time where the notification should be initiated. I did that by implementing the following code in the NotificationReceiver class:
NotificationReceiver.class
public class NotificationReciever extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
NotificationManager notificationManager = (NotificationManager) context.getSystemService(NOTIFICATION_SERVICE);
Intent new_intent = new Intent(context, MainActivity.class);
new_intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 100, new_intent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder notification = new NotificationCompat.Builder(context)
.setContentIntent(pendingIntent)
.setSmallIcon(R.drawable.ic_stat_name)
.setTicker("ticker_title")
.setContentTitle("content_title")
.setContentText("description")
.setAutoCancel(true);
notificationManager.notify(0, notification.build());
MainActivity mainActivity = new MainActivity();
mainActivity.setNotification(context);
}
}
However, this way the notification does not get initiated when the app is started before, in this case, 12:01, but it still does after this time.. (btw, the following code is added into my manifest)
<receiver android:name=".NotificationReciever"/>