Peleg Oren
Peleg Oren

Reputation: 17

Android notification set to 1 hour

I'm trying to display a push notification to remind the user to check something they choose (do dishes, go to work, etc). I'd like to set it to current time + 1 hour for starters.

However I try the following code and it doesn't change anything (notification pops up immediately regardless of value I set)

 pushNotifAd.when = System.currentTimeMillis()+10000000;

Here's the complete code, please guide me how I can call it 1 hour after it's originally triggered:

//shows a notif on user tray, just pass the title and text!
public void pushNotif(String title,String text ){
    Context context = MainActivity.this
            .getApplicationContext();
    notificationManager = (NotificationManager) context
            .getSystemService(NOTIFICATION_SERVICE);

    pushNotifAd = new Notification();
    pushNotifAd.icon = R.drawable.icon;
    pushNotifAd.tickerText = "time to go to work!";
    pushNotifAd.when = System.currentTimeMillis()+3600000;//current time + 1 // hr in millisecs


    Uri uri = Uri.parse("work.com");
    Intent intent = new Intent(Intent.ACTION_VIEW, uri);

    notificationIntent = new Intent(Intent.ACTION_VIEW, uri);
    contentIntent = PendingIntent.getActivity(context, 0,
            notificationIntent, 0);


    pushNotifAd.setLatestEventInfo(context, "Go to work!",
            "Time to get stuff done!", contentIntent);

    notificationManager
            .notify(LIST_UPDATE_NOTIFICATION, pushNotifAd);

Upvotes: 0

Views: 1362

Answers (1)

Giru Bhai
Giru Bhai

Reputation: 14398

Initially set pushNotifAd.when=System.currentTimeMillis(); and then use getMinutesDifference method as

if (getMinutesDifference(pushNotifAd.when, System.currentTimeMillis()) > 60) {
    // use notification method here
}

private long getMinutesDifference(long timeStart, long timeStop) {
    long diff = timeStop - timeStart;
    long diffMinutes = diff / (60 * 1000);
    return diffMinutes;
}

Upvotes: 1

Related Questions