Mike
Mike

Reputation: 435

Notification not firing at correct time

I am creating a To Do app for school. When a person enters the date and time of the item, the app should create a notification that goes off at the specified time. However, when the app is run, the notification goes off at the creation of the item, rather than the time specified. Attached is my notification method.

static final int uniqueid = 139868;
@SuppressLint("NewApi")
public void sendNotification(String title, String body){


    try{
        Log.i("LIST", "inside sendNotification");

        NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        Intent intent = new Intent(this,ItemEditorActivity.class);

        PendingIntent pi = PendingIntent.getActivity(this, 0, intent, 0);
        //set notification for date set
        Notification n = new Notification.Builder(this)
                .setContentTitle(title)
                .setSmallIcon(R.drawable.ic_launcher)
                .setWhen(dateAndTime.getTimeInMillis())
                .setTicker(body)
                .addAction(R.drawable.ic_launcher, title, pi)
                .setDefaults(Notification.DEFAULT_ALL)
                .setAutoCancel(true)
                .build();
        Log.i("LIST", dateAndTime.toString());  
        Log.i("LIST", "notify");

        nm.notify(uniqueid, n);
    } catch (Exception e){
        e.printStackTrace();
    }
}

where private Calendar dateAndTime=Calendar.getInstance();

Upvotes: 0

Views: 276

Answers (3)

David Wasser
David Wasser

Reputation: 95636

Notifications do not "go off at a specified time". When you post a Notification (ie: call NotificationManager.notify()) it appears immediately. From one of your comments it seems that you are confused about the when parameter of Notification. This is just a timestamp that will be displayed to the user. It is intended to be used for you to communicate some relevant timestamp to the user (for example, if the notification is for a calendar event, it could be the time of the calendar event). This is not the time when the notification will go off.

To schedule a notification to go off at a specified time you need to use AlarmManager.

Upvotes: 1

ayon
ayon

Reputation: 2180

I am editing my answer, Probably I have got your problem exactly. Look , you are using an unique ID , probably this is your request code for the pending Intent. So, as you are using same request code for every to do pending intents, so when you set a new one then your previous alarm/notfication request is replaced.That's why your recent notification works but previous one doesn't. You have to use different request codes. This is the reason.

Now , Try this way to show alarm and notification...Of course change contexts and other things according to your needs in the following code. It should work fine

String Noti_title = intent.getExtras().getString("title");
        String Noti_message = intent.getExtras().getString("notes");
        int Noti_Code = intent.getExtras().getInt("code");

        mManager = (NotificationManager) this.getApplicationContext().getSystemService(this.getApplicationContext().NOTIFICATION_SERVICE);

        Intent intent1 = new Intent(this.getApplicationContext(), MainActivity.class);

        Notification notification = new Notification(R.drawable.ic_launcher , Noti_title , System.currentTimeMillis());
        intent1.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);

        PendingIntent pendingNotificationIntent = PendingIntent.getActivity(this.getApplicationContext(), 0, intent1, PendingIntent.FLAG_UPDATE_CURRENT);
        notification.flags |= Notification.FLAG_AUTO_CANCEL;

        notification.setLatestEventInfo(this.getApplicationContext(),Noti_title , Noti_message , pendingNotificationIntent);
        notification.vibrate = new long[] { 100L, 100L, 200L, 500L };
        mManager.notify(Noti_Code , notification);
        try {
            Uri notification_uri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
            Ringtone r = RingtoneManager.getRingtone(getApplicationContext(), notification_uri);
            r.play();
        } catch (Exception e) {}

Upvotes: 0

Maulik Sheth
Maulik Sheth

Reputation: 574

use alarm manager

AlarmManager alarmManager = (AlarmManager) getApplicationContext()
                    .getSystemService(Context.ALARM_SERVICE);

alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY,pendingIntent);

INTERVAL_DAY will repeat the alarm every day at same time

EDIT: this is how i did, in my activity i used alarm manager to set pending intent at users time, then at the time a broadcast receiver will start a service and that service will start a notification

Upvotes: 0

Related Questions