Search code examples
androidandroid-intentandroid-serviceandroid-pendingintentandroid-broadcast

How to set Notification locally using Service and Broadcastreceiver?


I am new to android. I want to set Notification locally which fire it on specific time daily. When I start the service... Notification fires immediately...

i want to know how to fire the notification at particular time with out firing immediately

Step 1:

public class startservice extends Activity {

     @Override
     public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      Button button=(Button) findViewById(R.id.button1);
      button.setOnClickListener(new OnClickListener() {
            public void onClick(View arg0) {
            // TODO Auto-generated method stub
            Intent intent = new Intent(startservice.this,MainActivity.class);
            startservice.this.startService(intent);
        }
    });

     }

    }

Step 2: Start Service

public class MainActivity extends Service {

     AlarmManager am;
     Calendar calendar;
     int count=0;


     @Override
     public void onCreate() {
      super.onCreate();

      am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
             calendar=Calendar.getInstance();
             setNotification();

     }
     public void setNotification() {
          Intent intent = new Intent(this, Startnotification.class);
          calendar.set(Calendar.HOUR, 9); // At the hour you wanna fire
          calendar.set(Calendar.MINUTE,12); // Particular minute
          calendar.set(Calendar.SECOND, 0);
          double x = Math.random();
          String value=String.valueOf(x);
          intent.putExtra("name", value);
          sendBroadcast(intent);
          PendingIntent pendingIntent = PendingIntent.getBroadcast(this, count,intent, PendingIntent.FLAG_UPDATE_CURRENT);       
          am.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),3600000,pendingIntent);
        count++;
         }
    @Override
    public IBinder onBind(Intent arg0) {
        // TODO Auto-generated method stub
        return null;
    }

}

Step 3: BroadcastReceiver

public class Startnotification extends BroadcastReceiver {

     NotificationManager nm;


     @SuppressWarnings("deprecation")
    @Override
     public void onReceive(Context context, Intent intent) {
      nm = (NotificationManager) context
        .getSystemService(Context.NOTIFICATION_SERVICE);
      String getvalue= intent.getStringExtra("name");
      CharSequence from = getvalue;
      CharSequence message = "Android calendar...";
      PendingIntent contentIntent = PendingIntent.getService(context, 0, new Intent(),0);

      Notification notif = new Notification(R.drawable.ic_launcher,"Android calendar...", System.currentTimeMillis());
      notif.setLatestEventInfo(context, from, message, contentIntent);
      nm.notify(1, notif);
      System.out.println("BroadcastReceiver");
     }
    }

How to achieve it

Thanks in Advance...


Solution

  • In setNotification() you are calling

          sendBroadcast(intent);
    

    This calls your Startnotification.onReceive() immediately.

    Also, you set the HOUR in the Calendar instance like this:

    calendar.set(Calendar.HOUR, 9); // At the hour you wanna fire
    

    But you haven't explicitly set the AM/PM indicator for 12-hour time (HOUR is used to set 12-hour time). This means that the AM/PM indicator will be still be set to whatever it was when you called Calendar.getInstance(). If you run this code, for example, at 10:00 AM, and set the HOUR to 9, the alarm will go off immediately because the time is in the past.