I'm trying to create a simple app to give notification daily at specific time but somehow it doesn't work and it return no error at all the code:
The button where the notification should start when it pressed:
setRenider.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY,18);
calendar.set(Calendar.MINUTE,14);
calendar.set(Calendar.SECOND,0);
Intent intent = new Intent(getApplicationContext(),Notification_reciever.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(),100,intent,PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,calendar.getTimeInMillis(),AlarmManager.INTERVAL_DAY,pendingIntent);
}
});
then this is the notification class:
public class Notification_reciever extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
NotificationManager notificationManager = (NotificationManager) context.getSystemService(context.NOTIFICATION_SERVICE);
Intent repeating_intent = new Intent(context,ReaptingActivity.class);
repeating_intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(context,100,repeating_intent,PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder builder = new NotificationCompat.Builder(context).setContentIntent(pendingIntent).setSmallIcon(R.drawable.ic_free_breakfast_black_24dp)
.setContentTitle("Water Time").setContentText("Drink Water").setAutoCancel(true);
notificationManager.notify(100,builder.build());
}
}
and I create a simple empty activity named it ReaptingActivity and I type those code in the manifests:
<uses-permission android:name="com.android.alarm.permission.SET_ALARM"/>
<application
<receiver android:name=".Notification_reciever"/>
</application>
Based on a tutorial I followed it should work but it havent. Note, the time I type is correct but it show no notification !!!