1

I am showing a notification based on time time by user , when I start the app it always displays the notification as keeps on repeating even after cancel, which it incorrect

I doesn't display the notifications on time as set on .setWhen

MainActivity.java

calendar=Calendar.getInstance();
day=calendar.get(Calendar.DAY_OF_MONTH);
month=(calendar.get(Calendar.MONTH));
year=calendar.get(Calendar.YEAR);

cLoader=new CursorLoader(this, BirthdayProvider.CONTENT_URI, null, BirthdayProvider.EVENT_DATE+"='"+day+"' and "+BirthdayProvider.EVENT_MONTH+"='"+month+"'", null, null);
c=cLoader.loadInBackground();
c.moveToFirst();
while (c.moveToNext()) {

calendar.set(Calendar.MONTH, (month-1));
calendar.set(Calendar.YEAR, year);
calendar.set(Calendar.DAY_OF_MONTH, day);

calendar.set(Calendar.HOUR_OF_DAY,19);//set the alarm time
calendar.set(Calendar.MINUTE, 06);
calendar.set(Calendar.SECOND,0);
long calMillis=calendar.getTimeInMillis();
int systemTimeID=(int)System.currentTimeMillis();   


Intent myIntent = new Intent(getBaseContext(), MyReceiver.class);
pendingIntent = PendingIntent.getBroadcast(MainActivity.this, systemTimeID, myIntent,0);             
AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, calMillis, pendingIntent);

MyReceiver.java

public void onReceive(Context context, Intent intent) {
        // TODO Auto-generated method stub
        Toast.makeText(context.getApplicationContext(), "MyReceiver started", Toast.LENGTH_SHORT).show();       
        Intent service1 = new Intent(context, MyAlarmService.class);        
        context.startService(service1);
    }

MyAlarmService.java

public void onCreate() 
    {
       // TODO Auto-generated method stub  
       super.onCreate();
       calendar=Calendar.getInstance();
        date=calendar.get(Calendar.DAY_OF_MONTH);
        month=(calendar.get(Calendar.MONTH)+1);
        year=calendar.get(Calendar.YEAR);

          cLoader=new CursorLoader(this, BirthdayProvider.CONTENT_URI, null, BirthdayProvider.EVENT_DATE+"='"+date+"' and "+BirthdayProvider.EVENT_MONTH+"='"+month+"'", null, null);
        c=cLoader.loadInBackground();
      Toast.makeText(getBaseContext(), "My service started ", Toast.LENGTH_SHORT).show();
    }

public void onStart(Intent intent, int startId)
   {
       super.onStart(intent, startId);

       /*int count=intent.getIntExtra("count", 0);
       nameArray=intent.getStringArrayListExtra("name");
       numberArray=intent.getStringArrayListExtra("number");*/



    while (c.moveToNext()) {
        calendar.set(Calendar.MONTH,(month-1) ); 
        calendar.set(Calendar.YEAR, year); 
        calendar.set(Calendar.DAY_OF_MONTH, date); 

        calendar.set(Calendar.HOUR_OF_DAY, 5);
        calendar.set(Calendar.MINUTE, 19); 
        calendar.set(Calendar.SECOND, 0); 
        calendar.set(Calendar.AM_PM,Calendar.PM);

        count=count+1;





        mManager = (NotificationManager) this.getApplicationContext().getSystemService(this.getApplicationContext().NOTIFICATION_SERVICE);

        Intent intent1 = new Intent(this.getApplicationContext(),SearchListActivity.class);        
        intent1.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        intent1.addFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);

        PendingIntent pendingNotificationIntent = PendingIntent.getActivity( this.getApplicationContext(),0, intent1,PendingIntent.FLAG_UPDATE_CURRENT);
        Uri uri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);


        RemoteViews remoteView=new RemoteViews(getPackageName(), R.layout.normal_status_bar);
        remoteView.setTextViewText(R.id.title_name, name);


        Notification.Builder builder = new Notification.Builder(this);
        builder.setContentIntent(pendingNotificationIntent)
        .setContent(remoteView)
        .setSmallIcon(R.drawable.app_icon)
        .setLargeIcon(bitmapImage)
        .setTicker("Today's Events")
        .setWhen(calendar.getTimeInMillis())
        .setAutoCancel(true)
        .setContentTitle(name)
        .setVibrate(new long[] {0, 200, 200, 600, 600})
        .setNumber(count)
        .setSound(uri)
        .setLights(Color.BLUE, 1000, 1000)
        .setContentText("[ Event type : "+eventype+" ]\n\n"+"[ Turns : "+turns+" ]");
        Notification notification = builder.build(); 
        mManager.notify(count, notification);


    }

    }
user3595649
  • 63
  • 1
  • 7

1 Answers1

1

The problem is with the month value set. Don't minus it calendar.get(Calendar.MONTH) will give the correct month index.

    while (c.moveToNext()) {
    calendar.set(Calendar.MONTH,(month) ); 

So when you minus its becomes the previous month(not the current month as you expect). Month values starts from 0. That's the reason the alarm is trigger immediately you start the app( since alarm is past)

As said on the comments - setWhen is just a timestamp to show in notification when it occurs. It doesn't mean notification need to be show on on the time set in when

Libin
  • 16,967
  • 7
  • 61
  • 83
  • Even if I put Calendar.MAY for current month, then also it doesn't work – user3595649 May 06 '14 at 15:33
  • Try removing the `calendar.set(Calendar.AM_PM,Calendar.PM);` line. Also if you want to set time for today just `get the calendar Instance` and set only the hour and mins, getInstance() will have the current date information – Libin May 07 '14 at 00:24