开发者

Implementing Snooze in Android Notifications

What's the best way to implement snooze functionality in an Android notification. (i.e. I notify user of X [for arguments sake lets use the example of a text message], and he doesn't want to be bothered by it for now, yet at the same time he wants to make sure he doesn't forget. So he does want it to play the noise again, but at e.g. 5 minutes from now)

I saw the way the android alarm clock does it, but to me it seems messy (and usually not good) to popup a floating intent while the user might be doing something i开发者_StackOverflow中文版mportant.

On the other hand, it doesn't seem possible to put buttons inside the notification area. Or am I wrong?

What would you suggest?


  1. Add a snooze button:

    Button snooze = (Button) findViewById(R.id.snooze);
        snooze.setOnTouchListener(new OnTouchListener() {
            public boolean onTouch(View arg0, MotionEvent arg1) {
                 mMediaPlayer.stop();
                 finish();
                 return true;
            }
    });       
    
  2. Then before where you call the alarm, update the time

    Intent intent = new Intent(this, this.getClass());
    PendingIntent pendingIntent =
        PendingIntent.getService(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
    
    long currentTimeMillis = System.currentTimeMillis();
    long nextUpdateTimeMillis = currentTimeMillis + 5 * DateUtils.MINUTE_IN_MILLIS;
    Time nextUpdateTime = new Time();
    nextUpdateTime.set(nextUpdateTimeMillis);
    
    AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
    alarmManager.set(AlarmManager.RTC, nextUpdateTimeMillis, pendingIntent);
    
  3. Call the alarm now.


A simple strategy may be as follows:

  • The notification text can be "1 new message. Click here to notify again in 5 mins".
  • If the user clears the notifications, nothing happens and notification is cleared.
  • If the user clicks on the notification, then clear the notification and set up a timer and post a new notification after 5 min.

Alternatively, clicking the notification can bring an activity to the foreground that will set up a timer if there's no other input from the user (e.g., clicking on a "dismiss" button).

Another, opposite approach, would be to set up a timer when the notification is sent, and after the snooze time, remove it and add a new one, with the corresponding noise and probably a text added saying how much time passed since the original notification. That can be done recursively until the user clears the notification or clicks on it.

The best strategy will depend on what the application does and what is the level of customization the user is able to do. I would not implement any snozze behavior that cannot be easily prevented by the user (can be very annoying if not).


I've done this exactly same way how i created first alarm-notification

  private void startAlarm(Calendar calendar) {

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

    Intent intent = new Intent(this, AlertReceiver.class);

    PendingIntent pendingIntent = PendingIntent.getBroadcast(this, Config.NOTIFICATION_REQUEST_CODE, intent, 0);

    //Repeat every 24 hours
    alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 24*60*60*1000, pendingIntent);
}

So for snoozing notification or alarm i just created another alarm but that's not repeating alarm. that will trigger for only once

    private void snoozeAlarm() {

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

    Intent intent = new Intent(this, AlertReceiver.class);

    PendingIntent pendingIntent = PendingIntent.getBroadcast(this, Config.NOTIFICATION_REQUEST_CODE, intent, 0);

    alarmManager.setExact(AlarmManager.RTC_WAKEUP,
            Calendar.getInstance().getTimeInMillis() + 5 * 60000, //...start alarm again after 5 minutes
            pendingIntent);

    finish();
    System.exit(0); //...exit the activity which displayed
}

if anyone need to take a look at AlertReceiver.class

 public class AlertReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {


        intent = new Intent();
        intent.setClass(context, DisplayedOnScreen.class); 
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(intent);

        NotificationHelper notificationHelper = new NotificationHelper(context);
        NotificationCompat.Builder notificationBuilder = notificationHelper.getNotification();
        notificationHelper.getManager().notify(1, notificationBuilder.build());

    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜