Search code examples
javaandroidsqlitenotificationsalarmmanager

Getting trouble to show birthday reminder


My application contains a SQLite database to store a person's information. I store birthday as a long value. I want to show notification if someone has birthday today in the saved list. I am retrieving the long birthday value, setting it to a calendar and comparing it with current calendar month and day. But the problem is if someone has birthday matching with current day the the notification arises but shows that it is the birthday of the last person in the list whatever be the birthday of the last person. Please help. Thanks in advance.

Here are the codes

calendar_now = Calendar.getInstance();
    calendar_now.setTimeInMillis(System.currentTimeMillis());
    int month_now = calendar_now.get(Calendar.MONTH);
    int day_now = calendar_now.get(Calendar.DAY_OF_MONTH);

    //Set a specific time to the calendar
    calendar_now.set(Calendar.HOUR_OF_DAY, 6);
    calendar_now.set(Calendar.MINUTE, 0);
    calendar_now.set(Calendar.SECOND, 0);

    dbHelper = new DbHelper(this);
    personArrayList = dbHelper.getPerson();

    birth_calendar = Calendar.getInstance();

    for (Person p : personArrayList) {

        name = p.getName(); //Get the person's name
        mobile = p.getMobile();
        byte_image = p.getByteImage();
        position = personArrayList.indexOf(p);
        DoB = p.getDateOfBirth();
        birth_calendar.setTimeInMillis(DoB);
        int birth_month = birth_calendar.get(Calendar.MONTH);
        int birthday = birth_calendar.get(Calendar.DAY_OF_MONTH);

        if ((birth_month == month_now) && (birthday == day_now)) {

            Intent intent = new Intent(this, NotificationReceiver.class);

            PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 100, intent,
                    PendingIntent.FLAG_UPDATE_CURRENT);

            AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
            alarmManager.setExact(AlarmManager.RTC_WAKEUP, calendar_now.getTimeInMillis(), pendingIntent);
        } else {
            Toast.makeText(this, "No birthday today", Toast.LENGTH_SHORT).show();
        }

        birth_calendar.clear();

    }

NotificationReceiver.java

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

    //Open PersonDetailsActivity on notification click
    Intent resultIntent = new Intent(context, PersonDetailsActivity.class);
    //Pass the values through intent to PersonDetailsActivity
    resultIntent.putExtra("name", MainActivity.name);
    resultIntent.putExtra("mobile", MainActivity.mobile);
    resultIntent.putExtra("DoB", MainActivity.DoB);
    resultIntent.putExtra("image", MainActivity.byte_image);
    resultIntent.putExtra("position", MainActivity.position);

    resultIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

    PendingIntent pendingIntent = PendingIntent.getActivity(context, 100,
            resultIntent, PendingIntent.FLAG_UPDATE_CURRENT);

    NotificationCompat.Builder mBuilder = (NotificationCompat.Builder)
            new NotificationCompat.Builder(context)
                    .setContentIntent(pendingIntent)
                    .setSmallIcon(R.drawable.ic_notification)
                    .setContentTitle("Birthday reminder")
                    .setContentText("It is " +MainActivity.name+ "'s birthday today, " +
                            "wish him/her \"Happy birthday!!\"")
                    .setVibrate(new long[]{250, 250, 250, 250})
                    .setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
                    .setAutoCancel(true);

    notificationManager.notify(100, mBuilder.build());
}

}


Solution

  • if ((birth_month == month_now) && (birthday == day_now)) {
    
                Intent intent = new Intent(this, NotificationReceiver.class);
                Intent intent = new Intent(context , DocumentSubFolder.class);
                intent.putExtra("name" , name);
                intent.putExtra("mobile" , mobile);
                intent.putExtra("Dob" , DoB);
                intent.putExtra("image" , byte_image);
                intent.putExtra("position" , position);
    
                PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 100, intent,
                        PendingIntent.FLAG_UPDATE_CURRENT);
    
                AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
                alarmManager.setExact(AlarmManager.RTC_WAKEUP, calendar_now.getTimeInMillis(), pendingIntent);
            } 
    

    NotificationReceiver.java

    resultIntent.putExtra("name", intent.getStringExtra("name));
        resultIntent.putExtra("mobile", intent.getStringExtra("mobile));
        resultIntent.putExtra("DoB", intent.getStringExtra("Dob));
        resultIntent.putExtra("image", intent.getStringExtra("image));
        resultIntent.putExtra("position", intent.getStringExtra("position));
    

    Replace your code with this.