0

I'm looking to create a function for an Android app in which I get a notification every 25th day of the month indicating I have to do a certain task.

I've been able to display the notification using the following code :

public class NotificationPublisher extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
    long[] pattern = {0, 300, 0};
    PendingIntent pi = PendingIntent.getActivity(context, 01234, intent, 0);
    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context)
            .setSmallIcon(R.drawable.small_logo_ico)
            .setContentTitle(context.getResources().getString(R.string.notification_title))
            .setContentText(context.getResources().getString(R.string.notification_content))
            .setVibrate(pattern)
            .setAutoCancel(true);

    mBuilder.setContentIntent(pi);
    mBuilder.setDefaults(Notification.DEFAULT_SOUND);
    mBuilder.setAutoCancel(true);
    NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    mNotificationManager.notify(01234, mBuilder.build());
}

}

Now this system only works when I have my app open and doesn't allow me to display this when the app is closed. I've searched around and came to this: Android notification at specific date After trying this out (the schedule part) I noticed that it doesn't work when I close the app, as I get an error about unregistering the Receiver, doing this (unregistering) results in the receiver being canceled, and the notification can not be showed.

code used for the schedule:

NotificationPublisher receiver = new NotificationPublisher();
    this.receiver = receiver;
    IntentFilter filter = new IntentFilter("ALARM_ACTION");
    registerReceiver(receiver, filter);

    Intent intent = new Intent("ALARM_ACTION");
    intent.putExtra("param", "My scheduled action");
    PendingIntent operation = PendingIntent.getBroadcast(this, 0, intent, 0);
    // I choose 15s after the launch of my application
    alarms.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()+15000, operation) ;

Is there anything I'm missing, or am I using the wrong methods to schedule a notification on a certain date? ( The current notification is set to be scheduled 15 seconds in the future, this is just for testing, I've got a function ready to display this at a certain date)

Community
  • 1
  • 1
Gerton
  • 676
  • 3
  • 14
  • 1
    http://stackoverflow.com/questions/31086226/show-a-notification-on-a-particular-date-and-time – HAXM Oct 10 '16 at 13:34

1 Answers1

0

This is used to notify on middle of the month. Maybe You can get from below code.

 Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.DAY_OF_MONTH, 15);
        calendar.set(Calendar.HOUR_OF_DAY, 12);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        if (calendar.getTimeInMillis() < System.currentTimeMillis()) {
            calendar.add(Calendar.DAY_OF_YEAR, 30);
        }
        Intent myIntent = new Intent(getApplicationContext(), MyReceiver.class);
        myIntent.putExtra("NOTI_MSG",getString(R.string.notification_sidas));
        PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), NOTI_REQ_CODE_SIDAS, myIntent, PendingIntent.FLAG_UPDATE_CURRENT);

        AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
        alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
                AlarmManager.INTERVAL_DAY * 30, pendingIntent);
    }
Ankita Shah
  • 1,866
  • 16
  • 31
  • I've changed the last part to : "alarmManager.setRepeating(AlarmManager.RTC, System.currentTimeMillis()+3000, 2000 , pendingIntent);" and kept the rest the same (of course changing references where necessary), any reason why this could not work? – Gerton Oct 10 '16 at 13:54