I want to send notification at specific time using broadcast receiver. many tutorials videos and also answers has been read in this regard and all of them was clear. but I c
The problem was detected, as I searched again in this regard I found that when Calendar has been set for 10 Second calendar.set(Calendar.SECOND, 10);
it means BroadcastReceiver() must trigger Notification after 10 Second. it doesn't care the time of day.
question still exist, how we can tell broadcastreceiver() to Show Notification based on the time of day that we defined in the CODE exactly? such as 16:33:24 (hh:mm:ss)
You can send notification at specific time daily by following method
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, 12);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
Intent notifyIntent = new Intent(getApplicationContext(),showNotification.class);
notifyIntent.setFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
PendingIntent pendingIntent = PendingIntent.getBroadcast(etApplicationContext(),0,notifyIntent,PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),1000 * 60 * 60 * 24, pendingIntent);
Then in showNotification class
public class showNotification extends BroadcastReceiver {
public showNotification() {
}
@Override
public void onReceive(Context context, Intent intent) {
sendNotification(context);
}
private void sendNotification(Context context) {
Uri defaultSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
Intent intent = new Intent(context, SecondActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent,PendingIntent.FLAG_UPDATE_CURRENT);
NotificationManager notificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
String NOTIFICATION_CHANNEL_ID = "101";
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
@SuppressLint("WrongConstant") NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, "Notification", NotificationManager.IMPORTANCE_MAX);
//Configure Notification Channel
notificationChannel.setDescription("Game Notifications");
notificationChannel.enableLights(true);
notificationChannel.setVibrationPattern(new long[]{200});
notificationChannel.enableVibration(false);
notificationManager.createNotificationChannel(notificationChannel);
}
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(context, NOTIFICATION_CHANNEL_ID)
.setSmallIcon(R.mipmap.ic_launcher_round)
.setContentTitle(title.get("Title of notification"))
.setContentText(subText.get("Sub text of notification"))
.setAutoCancel(true)
.setSound(defaultSound)
.setContentIntent(pendingIntent)
.setWhen(System.currentTimeMillis())
.setPriority(Notification.PRIORITY_MAX);
notificationManager.notify(1, notificationBuilder.build());
}
}
If you wish to have a timer expire at an exact time, then you can configure the timer as follows.
Calendar alarmFor = Calendar.getInstance();
alarmFor.set(Calendar.HOUR_OF_DAY, 7);
alarmFor.set(Calendar.MINUTE, 45);
alarmFor.set(Calendar.SECOND, 0);
Intent MyIntent = new Intent(getApplicationContext(), BroadcastNotification.class);
PendingIntent MyPendIntent = PendingIntent.getBroadcast(getApplicationContext(), 0, MyIntent, PendingIntent.FLAG_CANCEL_CURRENT);
AlarmManager MyAlarm = (AlarmManager) getSystemService(ALARM_SERVICE);
MyAlarm.setAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, alarmFor.getTimeInMillis(), MyPendIntent);
In this example, onReceive() is as follows:
@Override
public void onReceive(Context context, Intent intent) {
Log.d("MyAPP", "onReceive() called");
}
When I run this, the following is logged at the the exact time requested:
11-22 07:45:00.730 5833-5833/com.sap.myapplication D/MyAPP: onReceive() called