// creating notification function public void createNotification(Context context, String msg, String msgText, String msgAlert) { int id = (int) System.currentTimeMillis(); Intent i = new Intent(context, StopAlarm.class); i.putExtra("passuser", user); i.putExtra("inputCode", inputCode); i.putExtra( "reminderTitle", titleOfReminder); // store the title of the reminder which need to pass to the next activity i.putExtra("reminderStartTime", startTimeOfReminder); // store the start time of the reminder i.putExtra("remminderStartDate", startDateOfReminder); // store the start date of the reminder i.putExtra("repeat", repeatReminder); // store if user want this alarm to repeat the schedule. i.putExtra("selectedHour", selectedHour); i.putExtra("selectedMinute", selectedMinute); i.putExtra("selectedDay", selectedDay); i.putExtra("selectedMonth", selectedMonth); i.putExtra("selectedYear", selectedYear); i.putExtra("notirequestcode", notiRequestCode); notificIntent = PendingIntent.getActivity(context, id, i, 0); // using notification builder to set up the notification layout and configuration NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context) .setSmallIcon(R.drawable.ic_alarm_24dp) .setContentTitle(msg) .setTicker(msgAlert) .setContentText(msgText); mBuilder.setContentIntent(notificIntent); mBuilder.setDefaults(NotificationCompat.DEFAULT_VIBRATE); Uri uri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); mBuilder.setSound( uri); // this is to set the sound so that when notification is received, there will also // have default sound being released mBuilder.setAutoCancel(true); mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); mNotificationManager.notify( 1, mBuilder.build()); // to set to receive notification for each alarm change the int here. }
// will auto trigger weekly alarm to ring when user first choose the option for repeating weekly public void repeatForWeekly() { selectedDay = String.valueOf( Integer.parseInt(selectedDay) + 7); // keep increase by 7 because repeat weekly if (Integer.valueOf(selectedDay) > 29) { selectedDay = String.valueOf(1); selectedMonth = String.valueOf(Integer.valueOf(selectedMonth) + 1); if (Integer.valueOf(selectedMonth) > 12) { selectedMonth = String.valueOf(1); selectedYear = String.valueOf(Integer.valueOf(selectedYear) + 1); } } Calendar calendar = Calendar.getInstance(); calendar.set(Calendar.MONTH, (Integer.valueOf(selectedMonth) - 1)); // need lesser one month calendar.set(Calendar.YEAR, Integer.valueOf(selectedYear)); calendar.set(Calendar.DAY_OF_MONTH, Integer.valueOf(selectedDay)); calendar.set(Calendar.HOUR_OF_DAY, Integer.valueOf(selectedHour)); // 24 hour clock calendar.set(Calendar.MINUTE, Integer.valueOf(selectedMinute)); Intent intent = new Intent( StopAlarm.this, StopAlarm .class); // this will trigger at specific timing, and when alarm rings, it would // redirect to the alarm page // want to pass these variables to the next activity, so pass it through intent intent.putExtra( "reminderTitle", titleOfReminder); // store the title of the reminder which need to pass to the next activity intent.putExtra( "reminderStartTime", startTimeOfReminder); // store the start time of the reminder intent.putExtra( "remminderStartDate", startDateOfReminder); // store the start date of the reminder intent.putExtra( "repeat", repeatReminder); // store if user want this alarm to repeat the schedule. intent.putExtra("selectedHour", selectedHour); intent.putExtra("selectedMinute", selectedMinute); intent.putExtra("selectedDay", selectedDay); intent.putExtra("selectedMonth", selectedMonth); intent.putExtra("selectedYear", selectedYear); // for the synchronise code and user intent.putExtra("inputCode", inputCode); intent.putExtra("passuser", user); // for unique notification code int _id = (int) System .currentTimeMillis(); // this is to help generate different request code so that can // have set multiple alarm notification each time intent.putExtra( "notirequestcode", _id); // this is to pass the request code of the alarm to the next activity so when cancel // able to match the same request ode PendingIntent sender = PendingIntent.getActivity( this, _id, intent, PendingIntent.FLAG_CANCEL_CURRENT | PendingIntent .FLAG_UPDATE_CURRENT); // need to set these flag inorder for the extras to get // updated and pass together with the pending intent AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE); // setting weekly interval so that it will trigger the next week alarm again am.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), weeklyRepeat, sender); }