Let us assume you want to set up alarms at scheduled time which should trigger notifications. Here is how you can achieve it.
You can trigger an alarm at a scheduled time and then use an IntentService to capture it once alarm goes off.
In the main activity (lets assume the name is MainActivity, you can create an alarm like this.
AlarmManager mgr = (AlarmManager) MainActivity.getSystemService(Context.ALARM_SERVICE); Intent intent = new Intent(MainActivity, NotifService.class); PendingIntent pi = PendingIntent.getService(MainActivity, 0, intent, 0); mgr.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime() + PERIOD, pi); //where the PERIOD defines after how much milliseconds you want the alarm to go off.
To configure the AlarmManager differently, you can read more about it.
NotifService is what captures the alarm when it goes off. Create a class called NotifService it extends IntentService
public class NotifService extends IntentService { public NotifService() { super("My service"); } @Override protected void onHandleIntent(Intent intent) { //write your code for sending Notification Intent intent = new Intent(); intent.setAction("com.xxx.yyy.youractivitytobecalled"); PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 1, intent, 0); NotificationCompat.Builder builder = new NotificationCompat.Builder (getApplicationContext()); builder.setContentTitle("Your Application Title"); builder.setContentText("Notification Content"); builder.setContentIntent(pendingIntent); builder.setTicker("Your content"); builder.setSmallIcon(R.drawable.ic_launcher); builder.setOngoing(false); builder.setPriority(0); Notification notification = builder.build(); NotificationManager notificationManger = (NotificationManager) getSystemService (Context.NOTIFICATION_SERVICE); notificationManger.notify(1, notification); } }
Register NotifService in your AndroidManifest.xml
<application fill content here /> <service android:name=".NotifService" > </service> </application>