21

I have referred many sites but still I am not able to create the notification(reminder or alarm) I don't know exactly how to create and work with it. Its to notify/remind user about task and also provide daily tips to the user.. I will be glad to have your help in doing so and how to code it too...

Regards:) Thanxs for your help in advance.

Rushabh
  • 385
  • 2
  • 5
  • 14
  • you want a [Notification](http://developer.android.com/guide/topics/ui/notifiers/notifications.html) or an [Alarm](http://developer.android.com/reference/android/app/AlarmManager.html)? Be specific – iTurki Aug 31 '12 at 02:11
  • 1
    My [answer](http://stackoverflow.com/a/12189105/996493) may help you in your requirement. – Lucifer Aug 31 '12 at 02:13
  • @iturki : I want coding for the both.. – Rushabh Aug 31 '12 at 02:13
  • @Rushabh What do you mean both? How you want to use them? Please explain your requirement so that we can help. – iTurki Aug 31 '12 at 02:16
  • @iturki actually i am right now working with an blood donation application. So I have provide an reminder about the tips and useful things to the user on daily / weekly basis so I want to use notification sort of things. Let me know if u can help me. – Rushabh Aug 31 '12 at 02:19

2 Answers2

46

You need two things:

  • AlarmManager: to schedule your notification at a regular bases (daily, weekly,..).
  • Service: to launch your notification when the AlarmManager goes off.

Here is a basic example:

In your Activity:

Intent myIntent = new Intent(this , NotifyService.class);     
AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
PendingIntent pendingIntent = PendingIntent.getService(this, 0, myIntent, 0);

Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.HOUR, 0);
calendar.set(Calendar.AM_PM, Calendar.AM);
calendar.add(Calendar.DAY_OF_MONTH, 1);

alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 1000*60*60*24 , pendingIntent);

This will trigger Alarm each day at midnight (12 am). You can change that if you want.

Now, create a Service NotifyService and put this code in its onCreate():

@Override
public void onCreate() {
    NotificationManager mNM = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
    Notification notification = new Notification(R.drawable.notification_icon, "Notify Alarm strart", System.currentTimeMillis());
    Intent myIntent = new Intent(this , MyActivity.class);     
    PendingIntent contentIntent = PendingIntent.getActivity(this, 0, intent, 0);
    notification.setLatestEventInfo(this, "Notify label", "Notify text", contentIntent);
    mNM.notify(NOTIFICATION, notification);
}

And this code will show the notification when the Alarm is received.

Good Luck!

Alex Zaraos
  • 6,443
  • 2
  • 26
  • 21
iTurki
  • 16,292
  • 20
  • 87
  • 132
5

here is a little YouTube Video Tutorial about daily notifications. You can find the source code in the description.

This video is not made by myself. But I think its a quick help. Although i recommend some changes because the Notification.Builder is deprecated:

1.

import android.support.v4.app.NotificationCompat;

2.

// Change: Notification mNotify = new Notification.Builder(this) to
Notification mNotify = new NotificationCompat.Builder(this)

Have Fun!

Wicked161089
  • 481
  • 4
  • 18