Search code examples
androidnotificationsandroid-ndkandroid-notificationsandroid-notification-bar

Android: Is it possible to set the notification to be notify on desire time ? and even if the project is not running?


In My application i am going to use this code to use the Notification:

 private void addDefaultNotification(){
    NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);

    int icon = R.drawable.icon;
    CharSequence text = "Notification Text";
    CharSequence contentTitle = "Notification Title";
    CharSequence contentText = "Sample notification text.";
    long when = System.currentTimeMillis();

    Intent intent = new Intent(this, NotificationViewer.class);
    PendingIntent contentIntent = PendingIntent.getActivity(this, 0, intent, 0);
    Notification notification = new Notification(icon,text,when);

    long[] vibrate = {0,100,200,300};
    notification.vibrate = vibrate;

    notification.ledARGB = Color.RED;
    notification.ledOffMS = 300;
    notification.ledOnMS = 300;

    notification.defaults |= Notification.DEFAULT_LIGHTS;
    //notification.flags |= Notification.FLAG_SHOW_LIGHTS;

    notification.setLatestEventInfo(this, contentTitle, contentText, contentIntent);

    notificationManager.notify(com.example.NotifivcationSample.Constants.NOTIFICATION_ID, notification);
}

rightnow i am getting the notofication. By calling that function. But i want is that it should be notify the user even if the application is not runing on at that time on device and notification should be notify on the desired time.

Is it possible ? If yes then please help me for that. Thanks.

Edited:

    public class AlarmNotificationReceiver extends BroadcastReceiver{
    //private Intent intent;
    private NotificationManager notificationManager;
    private Notification notification;
    @Override
    public void onReceive(Context context, Intent intent) {
        // TODO Auto-generated method stub

        long value1 = intent.getLongExtra("param1", 0);     
        String value2 = intent.getStringExtra("param2");

        addTwoMonthNotification();

    }
}

I have done like that but not able to create notification in that receiver class. Why ? and what should i have to do ?


Solution

  • Yes it is possible.

    You need to register your intent in AlarmManager and make the notification receiver class to wait for notification from AlarmManager when it's time to run.

    Basically you will need the following:

    1. Notification Intent class (subclass of Intent)

      public class MyNotificationIntent extends Intent {
          // basic implementation ...
      }
      
    2. NotificationReceiver class, subclass of BroadcastReceiver. Where you receive notification from AlarmManger and need to run your code to show notification (you already have that stuff)

      public class AlarmNotificationReceiver extends BroadcastReceiver{
      
          @Override
          public void onReceive(Context context, Intent intent){
      
              long value1 = intent.getLongExtra("param1", 0);
              String value2 = intent.getStringExtra("param2");
      
              // code to show the notification  
              ... 
              notificationManager.notify(...);
         }
      }
      
    3. Utility function to register your Notification in AlarmManager

      AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
      Intent intent = new MyNotificationIntent("com.alex.intent.action.ALARM_NOTIFICATION",
          Uri.parse("notification_alarm_id"), context, AlarmNotificationReceiver.class);
      intent.putExtra("param1", value1);
      intent.putExtra("param2", value2);
      
      PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
      alarmManager.set(AlarmManager.RTC_WAKEUP, timeToRun, pendingIntent);