0

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 ?

Shreyash Mahajan
  • 23,386
  • 35
  • 116
  • 188

2 Answers2

2

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);
    
Rudi Visser
  • 21,350
  • 5
  • 71
  • 97
Alexey A.
  • 1,389
  • 1
  • 11
  • 20
  • Ok Thanks for the reply. Let me try it. – Shreyash Mahajan Dec 23 '11 at 12:14
  • Is third one is the utility function ? And if it is then Where i have to call it? Suppose if i use toogleButton to on/off the alerm then should i have to call that function on toggle "on" right ?? – Shreyash Mahajan Dec 23 '11 at 12:22
  • Yes you should call it where you want to schedule the alarm. – Alexey A. Dec 23 '11 at 12:29
  • Will you please integrate it for my code. and let me give the link of that code. as i realy have no more time. and integration of Alerm is new to me. Thanks. – Shreyash Mahajan Dec 23 '11 at 12:48
  • I am not getting "com.alex.intent.action.ALARM_NOTIFICATION" and "notification_alarm_id" in your code. – Shreyash Mahajan Dec 23 '11 at 12:58
  • what is ExpiredListNotificationIntent ? – Shreyash Mahajan Dec 23 '11 at 13:01
  • Regarding manifest, you should declare the receiver: – Alexey A. Dec 23 '11 at 13:36
  • ExpiredListNotificationIntent it's "MyNotificationIntent" - corrected in my example. "com.alex.intent.action.ALARM_NOTIFICATION" it's an intent action name (specify your package in the name). – Alexey A. Dec 23 '11 at 13:45
  • Thanks for your support. I am near to create the notification. But there is some confution. Please see updated Question. and Should i have to write the utility function in to the MyNotificationIntent or in the activity where i am going to Switch it on/Off ? – Shreyash Mahajan Dec 24 '11 at 05:15
  • In AlarmNotificationReceiver class i am not able to create the Notification. Where i have to make the Notification ? – Shreyash Mahajan Dec 24 '11 at 05:27
  • Please help me for that. I realy need it. – Shreyash Mahajan Dec 24 '11 at 05:31
  • Utility function (point 3 in my example) should be written in your activity to register the alarm. And in AlarmNotificationReceiver you should create Notification. But why you can not create Notification in your AlarmNotificationReceiver? With broadcast receiver you get the Context. – Alexey A. Dec 24 '11 at 07:22
  • Ok I have done as you have said. But not able to create the Notification in the AlarmNotificationReceiver. See this http://stackoverflow.com/questions/8623208/why-i-am-not-able-to-create-the-notification-in-onreceiver-of-broadcastreceiver – Shreyash Mahajan Dec 24 '11 at 07:39
  • But the problem actually? Code doesn't compile? Compile but not working? Any errors? – Alexey A. Dec 24 '11 at 07:57
  • Please see the link that i have given in the previous comment. While i am going to create that function, i got some error at some line. Please meet me in Chat i realy need help. – Shreyash Mahajan Dec 26 '11 at 04:12
  • How did you implemented the MyNotificationIntent class ? – Junaid Apr 20 '12 at 10:15
1

http://android-er.blogspot.com/2010/10/simple-example-of-alarm-service-using.html

AlarmManager class provides access to the system alarm services. These allow you to schedule your application to be run at some point in the future. When an alarm goes off, the Intent that had been registered for it is broadcast by the system, automatically starting the target application if it is not already running.

you can also read this one: http://android.arnodenhond.com/tutorials/alarm-notification

You should be able to notify your user at a desired time without problem

ADDED

Or you can do:

 new Handler().postDelayed(new Runnable() { public void run() {
           //Shoot your notification here
      }
   }, 1000 * 60 * 5 );

** OR **

http://developer.android.com/resources/articles/timed-ui-updates.html

private Runnable mUpdateTimeTask = new Runnable() {
   public void run() {
       final long start = mStartTime;
       long millis = //something;
       int seconds = //something;
       int minutes = //something;
       seconds     =//something;

       mHandler.postAtTime(this,
               start + (((minutes * 60) + seconds + 1) * 1000));
   }
};
Waza_Be
  • 39,407
  • 49
  • 186
  • 260
  • direct code.....wow :) , first of all you should understand the concept thats the @Profete162 has provided here and try it out atleast for once. – Paresh Mayani Dec 23 '11 at 13:41