I need to the Android app to send notification to remind users at 6am , 7pm , 12am
How can I do that and thank you
I use Android Studio
I need to the Android app to send notification to remind users at 6am , 7pm , 12am
How can I do that and thank you
I use Android Studio
You can use AlarmManager
for remider.
You can refer code from here
https://developer.android.com/training/scheduling/alarms.html
Main Activity.java
private PendingIntent pendingIntent;
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, 2);
calendar.set(Calendar.MINUTE, 20);
Intent intent = new Intent(MainActivity.this, AlertReceiver.class);
pendingIntent = PendingIntent.getBroadcast(MainActivity.this, 0, intent , 0);
AlarmManager alarm = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarm.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),AlarmManager.INTERVAL_DAY, pendingIntent);
AlertReceiver.java
public class AlertReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
createNotification(context, "Hi", "titl", "Alarm");
}
public void createNotification(Context context, String msg , String msgText , String msgAlert){
PendingIntent notificIntent = PendingIntent.getActivities(context , 0 ,
new Intent[]{new Intent(context, MainActivity.class)},0);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.icon)
.setContentTitle(msg)
.setTicker(msgAlert)
.setContentText(msgText);
mBuilder.setContentIntent(notificIntent);
mBuilder.setDefaults(NotificationCompat.DEFAULT_SOUND);
mBuilder.setAutoCancel(true);
NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(context.NOTIFICATION_SERVICE);
mNotificationManager.notify(1,mBuilder.build());
}
}