简体   繁体   中英

Android notifications not getting posted when scheduled with AlarmManager

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.

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.

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM