krtkush
krtkush

Reputation: 1528

How set up a notification which will show when a certain event is near.

I'm making an app which is some what like a to-do list. Now, I want my app to show a notification when a certain set 'to-do' event is one day away. I want the notification to pop up even if app is not running. How can I achieve this? Reading the official developer document about notification does not really clear it up for me.

Upvotes: 0

Views: 1341

Answers (2)

Lalith B
Lalith B

Reputation: 12269

You can create a Android Service and use the wakeful intent service to schedule alarms and when you catch your alarm you can show an alertbox.

for you find the link below for code:

  1. Wakeful Intent
  2. Android Service Documentation

Upvotes: 1

Blundell
Blundell

Reputation: 76536

Maybe this can clear it up a bit: http://blog.blundell-apps.com/notification-for-a-user-chosen-time/

If you want the notification the day before the todo data, you'll just minus 1 day from the date.

1) Set an Alarm

 getSystemService(Context.ALARM_SERVICE).set(AlarmManager.RTC, date.getTimeInMillis(), pendingIntent);

2) Catch Alarm

3) Show notification

        // This is the 'title' of the notification
        CharSequence title = "Alarm!!";
        // This is the icon to use on the notification
        int icon = R.drawable.ic_dialog_alert;
        // This is the scrolling text of the notification
        CharSequence text = "Your notification time is upon us.";       
        // What time to show on the notification
        long time = System.currentTimeMillis();

        Notification notification = new Notification(icon, text, time);

        // The PendingIntent to launch our activity if the user selects this notification
        PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new Intent(this, SecondActivity.class), 0);

        // Set the info for the views that show in the notification panel.
        notification.setLatestEventInfo(this, title, text, contentIntent);

        // Clear the notification when it is pressed
        notification.flags |= Notification.FLAG_AUTO_CANCEL;

        // Send the notification to the system.
        mNM.notify(NOTIFICATION, notification);

Full Source Code

Upvotes: 1

Related Questions