0

am trying to post notifications from my game after a certain interval. I call PostNotification() function from onReceive() method of my BroadcastReciever class to post the notification after 1 minute of starting the game.

BroadcastReceiver

public class NotificationReciever extends BroadcastReceiver
{

private static int count=0;

@Override
public void onReceive(Context context, Intent intent)
{
    try 
    {
         Bundle bundle = intent.getExtras();
         String message = bundle.getString("message");
         int id = bundle.getInt("id");
         PostNotification(message, id);
    }
    catch (Exception e) 
    {
         e.printStackTrace();
    }
    wl.release();
}

public void PostNotification(String notif, int id)
{
    Notification notify=new Notification(R.drawable.icon,
            notif,
            System.currentTimeMillis());
Intent intent = new Intent(MyUtil.getInstance().context, MyActivity.class);
        PendingIntent i=PendingIntent.getActivity(MyUtil.getInstance().context, 0, intent, 0);
        notify.setLatestEventInfo(MyUtil.getInstance().context, "Title", notif, i);  
        MyActivity.notifyMgr.notify(id, notify);
        }
    }
}

I am calling ScheduleNotification() from onCreate() of MyActivity

public void ScheduleNotification()
{
     Calendar cal = Calendar.getInstance();
     Intent intent = new Intent(MyUtil.getInstance().context, NotificationReciever.class);
     intent.putExtra("id", NOTIFY_ID);
     intent.putExtra("message", "message");
     PendingIntent sender = PendingIntent.getBroadcast(MyUtil.getInstance().context, NOTIFY_ID, intent, PendingIntent.FLAG_UPDATE_CURRENT);
     AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
     am.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), sender);
}

But i do not recieve any notification and get the following error in my logcat

  01-11 19:28:32.455: W/System.err(20661): java.lang.NullPointerException
01-11 19:28:32.475: W/System.err(20661):    at android.content.ComponentName.<init>(ComponentName.java:75)
01-11 19:28:32.475: W/System.err(20661):    at android.content.Intent.<init>(Intent.java:2893)
01-11 19:28:32.475: W/System.err(20661):    at com.games.TestGame.NotificationReciever.PostNotification(NotificationReciever.java:41)
01-11 19:28:32.475: W/System.err(20661):    at com.games.TestGame.NotificationReciever.onReceive(NotificationReciever.java:27)

I know that i am doing something wrong when creating intent for notification. I get notification correctly when i call is directly from my activity but something goes wrong when i call it through alarm

Intent intent = new Intent(MyUtil.getInstance().context, MyActivity.class);

Can anyone please tell me where i am going wrong.

glo
  • 1,408
  • 3
  • 25
  • 54

2 Answers2

1

The when parameter to the Notification constructor used by you is simply for display purposes. It won't delay the display of your Notification.

How about using an Alarm? It'll only accept an Intent, though.

class stacker
  • 5,357
  • 2
  • 32
  • 65
  • @Glinda Use _type_ RTC (I presume the alarm shouldn't wake up the device), and probably a class specific Intent will do. – class stacker Jan 11 '13 at 10:36
  • I tried this but it is not working for me for some reason. I have changed my question and reposted – glo Jan 11 '13 at 14:03
  • @Glinda a) make ScheduleNotification() private. b) Did you include your BroadcastReceiver in your app's manifest? c) I m not so sure a BroadcastReceivwer is really required; have you played around with Intent(context, class)? – class stacker Jan 11 '13 at 14:14
  • I have but how else do you schedule a notification. Like a day or 5 hours after the game is started – glo Jan 11 '13 at 14:25
  • @Glinda Mystical NullPointer problems with a stacktrace showing no methods of your app -- like your stacktzrace -- occur when there's something wrong with the app's manifest. Can you post that as well? – class stacker Jan 11 '13 at 14:37
  • @Glinda Unfortunately, your manifest does not show correctly for me. But I cannot see that you registered the broadcast receiver for your alarm. You need to regiter your NotificationReceiver. – class stacker Jan 11 '13 at 15:01
  • i have added that: – glo Jan 11 '13 at 15:04
  • 1
    @Glinda Okay you're doing something you shouldn't. Don't mess with contexts. In your BroadcastReceiver, you're accessing MyActivity.context. Please do not store the activity's context in its own class. In theory, it's a static variable and should exist. However, the classloader needs not have it loaded at that point in time. Dont use such a variable. If you need a context, use the context your BroadcastReceiver is called with. Pass the context parameter to your PostNotification method. – class stacker Jan 11 '13 at 15:11
  • @Glinda I should add that, besides any possible class loader issues, even if MyActivity.context exists, the object it references may still not exist any more, or be invalid. – class stacker Jan 11 '13 at 17:10
  • i have saved the context to a singleton class and tried again but still getting same error. It has got to do something with the Intent am creating for notification. – glo Jan 14 '13 at 06:26
  • 1
    @glo Yes, it _has_ to do with your intent, namely the context you provide for it. Don't save contexts unless you have to and you actively manage them (e.g. because of an asynchronous task). As I said before, get rid of the context variable you use, now in MyUtil. To create the Intent, use _this_ as the context. And in your BroadcastReceiver, use the context which you receive when it gets called. Try it. BTW, why is PostNotification public? – class stacker Jan 14 '13 at 07:25
  • Thank you. I was using stored context instead of context in onRecieve() method. This solved my problem – glo Jan 14 '13 at 09:58
1

It is total mistake of understanding. Look at the details of public Notification (int icon, CharSequence tickerText, long when). It is like below:

icon The resource id of the icon to put in the status bar.

tickerText The text that flows by in the status bar when the notification first activates.

when The time to show in the time field. In the System.currentTimeMillis timebase.

That means that it is just for show in the Notification Massage, not for the time of notification. If you want to give the notification at certain future time, you have to set an AlermManager for that time. The AlermManager will call the BroadcastReceiver. So you have to create a BroadcastReceiver also, in which you have to set the Notification.You can go through this link.

dev_android
  • 8,698
  • 22
  • 91
  • 148
  • the link you suggested does not exist anymore and i tried using alarm manager but i am not being able to send notification as i get error when creating an intent for it – glo Jan 14 '13 at 07:27
  • http://stackoverflow.com/questions/6649402/alarm-manager-scheduling-multiple-non-repeating-events – dev_android Jan 15 '13 at 12:01