user1797190
user1797190

Reputation: 227

notification at set day and time

I want to create a notification that fires at a set day and time I have created the following function:

public void notify(View v){
    Context context = AllClasses.this;
    Calendar calendar = Calendar.getInstance();
    calendar.set(Calendar.DAY_OF_WEEK, 4);
    calendar.set(Calendar.HOUR_OF_DAY, 17);
    calendar.set(Calendar.MINUTE, 31);
    calendar.set(Calendar.SECOND, 0);
    AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    PendingIntent pi = PendingIntent.getService(context, 0, new Intent(context, Notify.class), PendingIntent.FLAG_UPDATE_CURRENT);
    am.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pi);
}

and the corresponding class:

public class Notify extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent arg1) {
    NotificationManager nm;
    nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);        
    CharSequence from = "VIPUL";
    CharSequence message = "Crazy About Android...";
    PendingIntent contentIntent = PendingIntent.getActivity(context, 0,
      new Intent(), 0);
    Notification notif = new Notification(R.drawable.android_logo,
      "Crazy About Android...", System.currentTimeMillis());
    notif.setLatestEventInfo(context, from, message, contentIntent);
    nm.notify(1, notif);

}
}

I can't figure out what's missing. Do I need certain permissions or something. Any help greatly appreciated!

Upvotes: 0

Views: 97

Answers (1)

MKJParekh
MKJParekh

Reputation: 34301

I guess you need to use PendingIntent.getBroadcast(context, requestCode, intent, flags) instead getService method.

Upvotes: 1

Related Questions