Salmononius2
Salmononius2

Reputation: 295

Android: Fire a Notification.Builder at a certain time

I have a Notification.Builder that sends a message to the notification bar as soon as I click a button. Is there a way to have the notification appear at a selected time? I looked through the Android Documentation, but didn't see anything that seemed like it would work.

Here's the code snippet I have:

        Notification n = new Notification.Builder(this)
            .setContentTitle("Random title")
            .setContentText("Random text")
            .setSmallIcon(R.drawable.abc_ic_go_search_api_mtrl_alpha)
            .setContentIntent(pIntent).build();

    NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

    notificationManager.notify(0, n);

Thanks!

Upvotes: 3

Views: 6435

Answers (2)

tachyonflux
tachyonflux

Reputation: 20211

1) Create a broadcast receiver with your notification code.

public class AlarmReceiver extends BroadcastReceiver{
    @Override
    public void onReceive(Context context, Intent intent) {
        NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

        Notification notification = new NotificationCompat.Builder(context)
                .setContentTitle("Random title")
                .setContentText("Random text")
                .setSmallIcon(R.drawable.abc_ic_go_search_api_mtrl_alpha)
                .setContentIntent(PendingIntent.getActivity(context, 0, new Intent(context, MyActivity.class), 0))
                .build();

        notificationManager.notify(0, notification);
    }
}

2) Schedule an alarm using AlarmManager to broadcast the intent for your broadcast receiver.

AlarmManager alarmMgr = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(this, AlarmReceiver.class);
PendingIntent alarmIntent = PendingIntent.getBroadcast(this, 0, intent, 0);
// set for 30 seconds later
alarmMgr.set(AlarmManager.RTC, Calendar.getInstance().getTimeInMillis() + 30000, alarmIntent);

Upvotes: 4

Rohit5k2
Rohit5k2

Reputation: 18112

Use this where you want to setup the notification

//Create an offset from the current time in which the alarm will go off.
Calendar cal = Calendar.getInstance();
cal.add(Calendar.SECOND, 15);

//Create a new PendingIntent and add it to the AlarmManager
Intent intent = new Intent(this, MyAlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this,
        100, intent, PendingIntent.FLAG_CANCEL_CURRENT);
AlarmManager am = (AlarmManager)getSystemService(Activity.ALARM_SERVICE);
am.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), pendingIntent);

And show your notification here

public class MyAlarmService extends Service 
{

    @Override
    public IBinder onBind(Intent arg0)
    {
        return null;
    }

    @Override
    public void onCreate() 
    { 
        super.onCreate();
    }

    @Override
    public void onStart(Intent intent, int startId)
    {
        super.onStart(intent, startId);
       // put notification code here
   }

   @Override
   public void onDestroy() 
   {
       super.onDestroy();
   }
}

Upvotes: 1

Related Questions