2

I've managed to have my notifications pop up at the press of a button, but I now want to further develop it so that the notification doesn't pop until X minutes (or Y hours) have passed since the button was pressed.

The relevant code:

private void sendNotif() {
    notification.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.avatar));
    notification.setTicker("Kayla has returned");
    notification.setWhen(System.currentTimeMillis());
    notification.setContentTitle("Kayla has returned");
    notification.setContentText("See what news she has to share");

    Intent notifIntent = new Intent(this, MainActivity.class);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notifIntent, PendingIntent.FLAG_CANCEL_CURRENT);
    notification.setContentIntent(pendingIntent);

    NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    nm.notify(notifId, notification.build());
}

private void setNotif() {
    Button btn = (Button) findViewById(R.id.notifBtn);
    btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            sendNotif();
        }
    });
}

I've figured I need to use AlarmManager in some way, but I can't seem to incorporate it into the notification...

PQuix
  • 83
  • 2
  • 10

1 Answers1

0

you want to set it for a specific time in the future. For example if it is currently 12:00PM and you want it in 10 minutes, set the alarm for 12:10PM. SO just always get current time + how long you want to wait until the alarm goes off. Here is an example. I also included if you want to repeat that alarm every 20 minutes.

private AlarmManager alarmMgr;
private PendingIntent alarmIntent;

private void setAlert(int hour, int minute) {
  alarmMgr = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
  Intent intent = new Intent(context, AlarmReceiver.class);
  alarmIntent = PendingIntent.getBroadcast(context, 0, intent, 0);

  Calendar calendar = Calendar.getInstance();
  calendar.setTimeInMillis(System.currentTimeMillis());
  calendar.set(Calendar.HOUR_OF_DAY, hour);
  calendar.set(Calendar.MINUTE, minute);

  alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
    1000 * 60 * 20, alarmIntent);
}

Send notification:

public void showNotification(Context context) {

  NotificationCompat.Builder mBuilder =
   new NotificationCompat.Builder(context)
   .setSmallIcon(R.drawable.notification_icon)
   .setContentTitle("Notification  Title")
   .setContentText("Notification Text");

  NotificationManager mNotificationManager =
  (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

  NotificationManager.notify().

  mNotificationManager.notify(001, mBuilder.build());
}

Call sendNotification where you are currently calling your toast

make sure to get context from your receiver

@Override
public void onReceive(Context context, Intent arg1) {
    showNotification(context);
}
TomD
  • 604
  • 1
  • 7
  • 22
  • Thank you very much! I manage to get it working with just a Toast in the AlarmReceiver class, but when I try to implement the code for my Notification it doesn't seem to want to work. Would you mind suggesting how I may implement that part? – PQuix Nov 07 '17 at 18:47
  • Sorry for the stupid question, but does the sendNotification method go in my MainActivity and then call on it from the MyAlarmReceiver class, or do I create the method in MyAlarmReceiver? Because I can't get either of them to work... If I put the method in MainActivity then it won't let me call on it in the onReceive method in MyAlarmReceiver, and if I try to create the method in MyAlarmReceiver then I get "Cannot resolve method 'getSystemService(java.lang.String)' – PQuix Nov 07 '17 at 19:11
  • the code needs to be in the MyAlarmReceiver class. You need to pass context to your notification. Code updated again. getSystemService doesn't work because it needs the context. (Also updated the function name to be more accurate) . should be good now. – TomD Nov 07 '17 at 19:19
  • Thank you so much for your help, This has been giving me a headache all day! – PQuix Nov 07 '17 at 19:31