Notification on specific day on android

I am looking to codes that I could make a notification through a day and time I don't know , so the user will determine the day and I have to send notification whenever this day comes , also if the user click on repeat must repeat it ,

I see a lot of code by there were just for time not day can anyone help me to modify them so I can reach what I am looking for ?

Here is the code i got .

MainActivity

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);

        Intent notificationIntent = new Intent("android.media.action.DISPLAY_NOTIFICATION");
        notificationIntent.addCategory("android.intent.category.DEFAULT");

        PendingIntent broadcast = PendingIntent.getBroadcast(this, 100, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);

        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.SECOND, 15);
        alarmManager.setExact(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), broadcast);
    }
}

NotificationActivity

public class NotificationActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.notification_activity);
    }
}

AlarmReceiver

public class AlarmReceiver extends BroadcastReceiver{
    @Override
    public void onReceive(Context context, Intent intent) {
        Intent notificationIntent = new Intent(context, NotificationActivity.class);

        TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
        stackBuilder.addParentStack(NotificationActivity.class);
        stackBuilder.addNextIntent(notificationIntent);

        PendingIntent pendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);

        NotificationCompat.Builder builder = new NotificationCompat.Builder(context);

        Notification notification = builder.setContentTitle("Demo App Notification")
            .setContentText("New Notification From Demo App..")
            .setTicker("New Message Alert!")
            .setSmallIcon(R.mipmap.ic_launcher)
            .setContentIntent(pendingIntent).build();

        NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.notify(0, notification);
    }
}

Answers


Create a Calendar instance like this based on the user input:

Calendar calendar = Calendar.getInstance();
// Settings calendar for 01/05/2016 12:33:00 AM
calendar.set(Calendar.YEAR, 2016);
calendar.set(Calendar.MONTH, 4); // January has value 0
calendar.set(Calendar.DAY_OF_MONTH, 1);
calendar.set(Calendar.HOUR_OF_DAY, 12);
calendar.set(Calendar.MINUTE, 33);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.AM_PM, Calendar.AM );

And to repeat the alarm repeatedly use setInexactRepeating():

As in android documentation:

Schedule a repeating alarm that has inexact trigger time requirements; for example, an alarm that repeats every hour, but not necessarily at the top of every hour.

For example, to repeat the alarm every day at the specified time(in calendar),

// Milliseconds of 24 hours
long interval = 24 * 60 * 60 * 1000;
alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP,
                calendar.getTimeInMillis(), interval , broadcast);

And setExact() will not repeat alarms.

Schedule an alarm to be delivered precisely at the stated time.


Complete code with Notification and Alarm management.

public class MyAlarmReceiver extends BroadcastReceiver
{
    // The app's AlarmManager, which provides access to the system alarm services.
    private AlarmManager alarmMgr = null;
    private NotificationManager mNotificationManager;
    NotificationCompat.Builder builder;

    public TankAlarmReceiver()
    {

    }


    @Override
    public void onReceive(Context context, Intent intent)
    {
       mNotificationManager = (NotificationManager)
                context.getSystemService(Context.NOTIFICATION_SERVICE);

        Intent intent = new Intent(context, NotificationActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);

        // If you use intent extras, remember to call PendingIntent.getActivity() with the flag
        // PendingIntent.FLAG_UPDATE_CURRENT, otherwise the same extras will be reused for every
        // notification.
        PendingIntent contentIntent = PendingIntent.getActivity(context, deviceId.hashCode(), intent,
                PendingIntent.FLAG_UPDATE_CURRENT);

        NotificationCompat.Builder mBuilder =
                new NotificationCompat.Builder(context)
                        .setSmallIcon(getNotificationIcon())
                        .setContentTitle(title)
                        .setStyle(new NotificationCompat.BigTextStyle()
                                .bigText(msg))
                        .setContentText(msg);

        mBuilder.setContentIntent(contentIntent);
        mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
    }

    /**
     * Sets a repeating alarm that runs once a day at approximately 8:30 a.m. When the
     * alarm fires, the app broadcasts an Intent to this WakefulBroadcastReceiver.
     *
     * @param settingsModel
     */
    public void setAlarm(Context context, TankSettingsModel settingsModel)
    {
        if(alarmMgr == null)
            alarmMgr = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);

        // Initialize calendar and PendingIntent here

        long alarmIntervalInMin = 1 * 60 * 60 * 1000;
        long triggerAtMillis = calendar.getTimeInMillis();
        alarmMgr.setInexactRepeating(AlarmManager.RTC_WAKEUP,
                triggerAtMillis, alarmIntervalInMin, pendingIntent);

        // Enable {@code SampleBootReceiver} to automatically restart the alarm when the
        // device is rebooted.
        ComponentName receiver = new ComponentName(context, MyBootReceiver.class);
        PackageManager pm = context.getPackageManager();

        pm.setComponentEnabledSetting(receiver,
                PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
                PackageManager.DONT_KILL_APP);
    }

    /**
     * Cancels the alarm.
     *
     * @param deviceId
     */

    public void cancelAlarm(Context context, String deviceId)
    {
        // Code for cancelling the alarm comes here...
    }
}

I think you can use the following.

cal.set(Calendar.DAY_OF_MONTH, Yourday);


Need Your Help

Wordwrap to multiple cells in excel

excel vba excel-vba

I am currently working on a excel VBA macro that auto-fills a form using data from another sheet.

user search engine - go back and forth

php sql caching search

I am working on a search engine. Users can search for other users givin specific parameters (such for example age, gender, city, hair color). He is also able to sort the results in different ways.