i want show notification at 8:00 am everyday
alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmIntent = new Intent(context of current file, AlarmReceiver1.class);
pendingIntent = PendingIntent.getBroadcast(Menu.this, 0, alarmIntent,PendingIntent.FLAG_UPDATE_CURRENT);
alarmIntent.setData((Uri.parse("custom://"+System.currentTimeMillis())));
alarmManager.cancel(pendingIntent);
Calendar alarmStartTime = Calendar.getInstance();
Calendar now = Calendar.getInstance();
alarmStartTime.set(Calendar.HOUR_OF_DAY, 8);
alarmStartTime.set(Calendar.MINUTE, 00);
alarmStartTime.set(Calendar.SECOND, 0);
if (now.after(alarmStartTime)) {
Log.d("Hey","Added a day");
alarmStartTime.add(Calendar.DATE, 1);
}
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, alarmStartTime.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);
Log.d("Alarm","Alarms set for everyday 8 am.");
Coming to the broadcast receiver class. You need to register your broadcast receiver in the manifest. This will cause you to receive clock events.
Override the onReceive method of this broadcast receiver and make a notification there itself or make a seperate notification building service and build and display your notification there.
The manifest code snippet:
<receiver android:name="AlarmReceiver1" android:enabled="true">
The broadcast receiver code snippet:
public class AlarmReceiver1 extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Intent service1 = new Intent(context, NotificationService1.class);
service1.setData((Uri.parse("custom://"+System.currentTimeMillis())));
context.startService(service1);
}
Notification building service code snippet:
public class NotificationService1 extends IntentService{
private NotificationManager notificationManager;
private PendingIntent pendingIntent;
private static int NOTIFICATION_ID = 1;
Notification notification;
@Override
protected void onHandleIntent(Intent intent) {
ontext context = this.getApplicationContext();
notificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
Intent mIntent = new Intent(this, Activity to be opened after clicking on the notif);
Bundle bundle = new Bundle();
bundle.putString("test", "test");
mIntent.putExtras(bundle);
pendingIntent = PendingIntent.getActivity(context, 0, mIntent, PendingIntent.FLAG_UPDATE_CURRENT);
Resources res = this.getResources();
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
Uri soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
notification = new NotificationCompat.Builder(this)
.setContentIntent(pendingIntent)
.setSmallIcon(R.drawable.ic_launcher)
.setLargeIcon(BitmapFactory.decodeResource(res, R.drawable.ic_launcher))
.setTicker("ticker value")
.setAutoCancel(true)
.setPriority(8)
.setSound(soundUri)
.setContentTitle("Notif title")
.setContentText("Text").build();
notification.flags |= Notification.FLAG_AUTO_CANCEL | Notification.FLAG_SHOW_LIGHTS;
notification.defaults |= Notification.DEFAULT_SOUND | Notification.DEFAULT_VIBRATE;
notification.ledARGB = 0xFFFFA500;
notification.ledOnMS = 800;
notification.ledOffMS = 1000;
notificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
notificationManager.notify(NOTIFICATION_ID, notification);
Log.i("notif","Notifications sent.");
}
}
I select random days for notification but notification is shown everyday
Setting the Calendar
this way is unreliable. You should never call set()
using DAY_OF_WEEK
.
If you want the alarm to trigger on the next Wednesday, you should figure out the date (month, day, year) of the next Wednesday and set that on the Calendar
.
See Setting DAY_OF_WEEK returns unexpected result
See also https://bugs.java.com/bugdatabase/view_bug.do?bug_id=4655637
Example of how to create a Calendar
instance for the current time for the following WEDNESDAY:
Calendar calendar = Calendar.getInstance();
int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);
// dayOfWeek is 1..7 (SUNDAY to SATURDAY)
int daysToAdd = Calendar.WEDNESDAY - dayOfWeek;
// Subtract current day of week from the day of week you want
if (daysToAdd < 0) {
// Current day of the week is after WEDNESDAY, so we go to next week
daysToAdd += 7;
}
// If daysToAdd is zero, today is already WEDNESDAY!
if (daysToAdd != 0) {
calendar.add(Calendar.DATE, daysToAdd);
}
Fire notification at every 24 hours and at exact time of 8 AM
Do as chintan suggested. To get a clear picture, the exact solution
might look something similar to the below:
Intent myIntent = new Intent(Splash.this, AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(Splash.this, 0, myIntent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
Calendar firingCal= Calendar.getInstance();
Calendar currentCal = Calendar.getInstance();
firingCal.set(Calendar.HOUR, 8); // At the hour you wanna fire
firingCal.set(Calendar.MINUTE, 0); // Particular minute
firingCal.set(Calendar.SECOND, 0); // particular second
long intendedTime = firingCal.getTimeInMillis();
long currentTime = currentCal.getTimeInMillis();
if(intendedTime >= currentTime){
// you can add buffer time too here to ignore some small differences in milliseconds
// set from today
alarmManager.setRepeating(AlarmManager.RTC, intendedTime, AlarmManager.INTERVAL_DAY, pendingIntent);
} else{
// set from next day
// you might consider using calendar.add() for adding one day to the current day
firingCal.add(Calendar.DAY_OF_MONTH, 1);
intendedTime = firingCal.getTimeInMillis();
alarmManager.setRepeating(AlarmManager.RTC, intendedTime, AlarmManager.INTERVAL_DAY, pendingIntent);
}
notification when user don't use app 24 hours
It helped me
alarmManager.setRepeating(AlarmManager.RTC, calendar.getTimeInMillis() + AlarmManager.INTERVAL_DAY, AlarmManager.INTERVAL_DAY, pendingIntent);
How to send a localNotification at a specific time everyday, even if that time has passed?
Updated @Paulw11 's answer to Swift 3.0 and wrapped up in a function:
/// Set up the local notification for everyday
/// - parameter hour: The hour in 24 of the day to trigger the notification
class func setUpLocalNotification(hour: Int, minute: Int) {
// have to use NSCalendar for the components
let calendar = NSCalendar(identifier: .gregorian)!;
var dateFire = Date()
// if today's date is passed, use tomorrow
var fireComponents = calendar.components( [NSCalendar.Unit.day, NSCalendar.Unit.month, NSCalendar.Unit.year, NSCalendar.Unit.hour, NSCalendar.Unit.minute], from:dateFire)
if (fireComponents.hour! > hour
|| (fireComponents.hour == hour && fireComponents.minute! >= minute) ) {
dateFire = dateFire.addingTimeInterval(86400) // Use tomorrow's date
fireComponents = calendar.components( [NSCalendar.Unit.day, NSCalendar.Unit.month, NSCalendar.Unit.year, NSCalendar.Unit.hour, NSCalendar.Unit.minute], from:dateFire);
}
// set up the time
fireComponents.hour = hour
fireComponents.minute = minute
// schedule local notification
dateFire = calendar.date(from: fireComponents)!
let localNotification = UILocalNotification()
localNotification.fireDate = dateFire
localNotification.alertBody = "Record Today Numerily. Be completely honest: how is your day so far?"
localNotification.repeatInterval = NSCalendar.Unit.day
localNotification.soundName = UILocalNotificationDefaultSoundName;
UIApplication.shared.scheduleLocalNotification(localNotification);
}
Related Topics
What Is More Efficient? Static, Data Passing, Shared Preferences, Database...
Error Inflating Class Com.Google.Android.Maps.Mapview
Arrayindexoutofboundsexception with Custom Android Adapter for Multiple Views in Listview
How Do Popular Apps Authenticate User Requests from Their Mobile App to Their Server
Integrating Video File in Android App as App Background
Is Fcm (Firebase Cloud Messaging) Token for One Device or for One Account
Android Different Edittext Backgrounds for Different States Using Shapes, Selector or List
How to Show a Mediacontroller While Playing Audio in Android
How to Take Emulator Screenshots Using Eclipse
How to Access Adb in Os X Through Terminal, "Command Not Found"
Cordova Cannot Add Android Failed with Exit Code Enoent
What Happens to Older Clients When You Migrate an App Server from Gcm to Fcm
Execution Failed for Task: ':App:Mergedebugresources'. Android Studio
Android in Navigation Drawer When I Click Anywhere Drawer Closes
Difference Between <Supports-Screens> and <Compatible-Screens> on Android
What Does Fragmentmanager and Fragmenttransaction Exactly Do