M.V.
M.V.

Reputation: 1672

Android notification time

I have a public class to make notifications...

public class notifyHelper  {
public void sendNotification(Activity caller, Class<?> activityToLaunch, String title, String msg, int numberOfEvents, boolean flashLed, boolean vibrate, int ID) {
    NotificationManager notifier = (NotificationManager) caller.getSystemService(Context.NOTIFICATION_SERVICE);

    // Create this outside the button so we can increment the number drawn over the notification icon.
    // This indicates the number of alerts for this event.
    long whenTo = System.currentTimeMillis() + (1000 * 60 * 15);
    final Notification notify = new Notification(R.drawable.icon, "", whenTo);

    notify.icon = R.drawable.icon;
    notify.tickerText = "TV Spored ++";
    notify.when = whenTo;
    notify.number = numberOfEvents;
    notify.flags |= Notification.FLAG_AUTO_CANCEL;

    if (flashLed) {
    // add lights
        notify.flags |= Notification.FLAG_SHOW_LIGHTS;
        notify.ledARGB = Color.CYAN;
        notify.ledOnMS = 500;
        notify.ledOffMS = 500;
        notify.defaults |= Notification.DEFAULT_SOUND;
    }

    if (vibrate) {

        notify.vibrate = new long[] {100, 200, 200, 200, 200, 200, 1000, 200, 200, 200, 1000, 200};
    }

    Intent toLaunch = new Intent(caller, activityToLaunch);
    PendingIntent intentBack = PendingIntent.getActivity(caller, 0, toLaunch, 0);

    notify.setLatestEventInfo(caller, title, msg, intentBack);
    notifier.notify(ID, notify);
}

public static void clear(Activity caller) {
    NotificationManager notifier = (NotificationManager) caller.getSystemService(Context.NOTIFICATION_SERVICE);
    notifier.cancelAll();
}
}

No matter what I do (look whenTo) this notification is always shown when called... How can I set notification time?

Thanks for the answer!

Upvotes: 2

Views: 8792

Answers (3)

jibysthomas
jibysthomas

Reputation: 1621

You have to use AlarmManager to schedule the notification.

Upvotes: 0

surinder singh
surinder singh

Reputation: 1565

may be this will help you

private void createNotification(String contentTitle, String contentText, String tickerText, long millisec){     
        notificationManager = (NotificationManager) this.ctx.getSystemService(Context.NOTIFICATION_SERVICE);        
        note = new Notification(android.R.drawable.btn_star_big_on, tickerText, millisec );
        note.when = millisec;        
        Intent notificationIntent = new Intent(this.ctx, CenteringActivity.class);
        notificationIntent.setAction(Intent.ACTION_MAIN);
        notificationIntent = notificationIntent.setFlags(Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
        contentIntent = PendingIntent.getActivity(this.ctx, 0, notificationIntent, 0);
        note.setLatestEventInfo(this.ctx, contentTitle, contentText, contentIntent);
        note.number = 1;//Just created notification so number=1. Remove this line if you don't want numbers       
        notificationManager.notify(notif_ID, note);
    }    

 private void createStatusBarNotification(final String contentTitle, final String contentText, final String tickerText, final long millisec)
    { 
        //Date date = new Date(System.currentTimeMillis() + (1000 * 60 * 2));
        //long f = System.currentTimeMillis() + (1000 * 60 * 2);
        //super.webView.loadUrl("javascript: alert('::"+millisec+","+f+"')");
        Date date = new Date(millisec);
        Timer timer = new Timer();
        TimerTask timerTask = new TimerTask(){
            @Override
            public void run(){
                createNotification( contentTitle,  contentText,  tickerText,  millisec);
            }
        };
        timer.schedule(timerTask, date, 1000*60);//(1000*60)will repeate the same notification in 1 minute   
    }

Upvotes: 1

mah
mah

Reputation: 39847

What makes you think there's a way to create a notification that is not immediate?

From http://developer.android.com/reference/android/app/Notification.html you can see a few important things. First, the constructor you're using is deprecated -- you should look to using http://developer.android.com/reference/android/app/Notification.Builder.html instead. More importantly though, the 3rd parameter is not "when to show the notification", its the time to display in the time field of the notification itself. To visualize this... call your phone and don't answer (in order to cause a missed call notification). Then pull down your notification drawer and notice the time that gets displayed in the lower right.

Upvotes: 3

Related Questions