简体   繁体   English

使用AlarmManager计划时未发布Android通知

[英]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. 我从BroadcastReciever类的onReceive()方法调用PostNotification()函数,在开始游戏1分钟后发布通知。

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 我从MyActivity的onCreate()调用ScheduleNotification()

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 但我没有收到任何通知,并在我的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); 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. 您使用的Notification构造函数的when参数仅用于显示目的。 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) . 查看public Notification (int icon, CharSequence tickerText, long when)的详细信息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. icon要放入状态栏的图标的资源ID。

tickerText The text that flows by in the status bar when the notification first activates. tickerText通知首次激活时状态栏中流动的文本。

when The time to show in the time field. 时间显示在时间字段中。 In the System.currentTimeMillis timebase. 在System.currentTimeMillis时基中。

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. 如果您希望在将来某个时间发出通知,则必须为该时间设置AlermManager。 The AlermManager will call the BroadcastReceiver. AlermManager将调用BroadcastReceiver。 So you have to create a BroadcastReceiver also, in which you have to set the Notification.You can go through this link . 所以你还必须创建一个BroadcastReceiver,你必须在其中设置Notification。你可以通过这个链接

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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