Reputation: 275
Everything works perfectly fine, Notification runs properly. But I've set the Time in MainActivity when the Notification has to show. but I don't think it's working properly. I get the notification at 12 AM in the night :( what am i doing wrong. Please help.
My Notification Helper Code,
public class NotificationHelper {
private Context mContext;
private static final String NOTIFICATION_CHANNEL_ID = "10001";
NotificationHelper(Context context) {
mContext = context;
}
void createNotification()
{
Bitmap bigImage = BitmapFactory.decodeResource(mContext.getResources(), R.drawable.verse_img_2);
Bitmap thumbnail = BitmapFactory.decodeResource(mContext.getResources(), R.mipmap.ic_launcher);
Intent intent = new Intent(mContext , MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent resultPendingIntent = PendingIntent.getActivity(mContext,
100 /* Request code */, intent,
PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(mContext);
mBuilder.setSmallIcon(R.drawable.ic_prayer_request);
mBuilder.setContentTitle("Verse of the Day!")
.setContentText("Click on the 'Notification' to read the verse!")
.setAutoCancel(false)
.setLargeIcon(thumbnail)
.setAutoCancel(true)
.setSound(null)
// .setStyle(new NotificationCompat.BigPictureStyle().bigPicture(bigImage))
.setPriority(Notification.PRIORITY_DEFAULT)
.setContentIntent(resultPendingIntent);
NotificationManager mNotificationManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O)
{
int importance = NotificationManager.IMPORTANCE_LOW;
NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, "Verse of the day!", importance);
notificationChannel.enableLights(true);
notificationChannel.setLightColor(Color.RED);
notificationChannel.enableVibration(true);
notificationChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
assert mNotificationManager != null;
mBuilder.setChannelId(NOTIFICATION_CHANNEL_ID);
mNotificationManager.createNotificationChannel(notificationChannel);
}
assert mNotificationManager != null;
mNotificationManager.notify(100 /* Request Code */, mBuilder.build());
}
}
Main Activity Code
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Methods Call
dailyVerseNotification();
}
private void dailyVerseNotification() {
Calendar time = Calendar.getInstance();
time.setTimeZone(TimeZone.getTimeZone("GMT"));
time.set(Calendar.HOUR_OF_DAY, 6);
time.set(Calendar.MINUTE, 59);
time.set(Calendar.AM_PM, Calendar.AM);
time.set(Calendar.SECOND, 10);
Intent intent = new Intent(getApplicationContext(), NotificationReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 100, intent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
assert alarmManager != null;
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, time.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);
}
}
Upvotes: 1
Views: 93
Reputation: 275
Fixed by checking the Present day.
Calendar calendar = Calendar.getInstance();
//Setting time of the day (7 AM here) Notification will be sent everyday.
Calendar currentDate = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, 7);
calendar.set(Calendar.MINUTE, 00);
calendar.set(Calendar.SECOND, 0);
if(currentDate.after(calendar)){
Log.w("Added a day", "1");
calendar.add(Calendar.DATE, 1);
}
Upvotes: 1