Android:一旦将Notification设置为设备,每次重新启动设备时都会发出无消息通知?

时间:2012-01-12 11:00:02

标签: android notifications android-ndk android-manifest push-notification

在我的应用中,我将通知设置为如下代码:

    // for the PAYE 18 APRIL 2011 // 1  
    AM_EM_APRIL_2011 = (AlarmManager) getApplicationContext().getSystemService(Context.ALARM_SERVICE);
    Intent in1 = new Intent(this, AlarmReceiverNotificationForEveryMonth.class);
    in1.putExtra("MyMessage","Your PAYE return is DUE on 20th April 2011.");
    PI_EM_APRIL_2011 = PendingIntent.getBroadcast(this, 1, in1, PendingIntent.FLAG_UPDATE_CURRENT);
    Calendar calendar_PAYE_18_APRIL_2011 = Calendar.getInstance();
    calendar_PAYE_18_APRIL_2011.setTimeInMillis(System.currentTimeMillis());
    calendar_PAYE_18_APRIL_2011.set(2011, 3, 18,mHour, mMinute, 0);
    AM_EM_APRIL_2011.set(AlarmManager.RTC_WAKEUP, calendar_PAYE_18_APRIL_2011.getTimeInMillis(),PI_EM_APRIL_2011);

通知的广播课程是:

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

    // My Notification Code
    notificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
    int icon = R.drawable.app_icon;
    //System.out.println("The ID Number is: "+Long.parseLong(intent.getData().getSchemeSpecificPart()) );
    contentText = intent.getStringExtra("MyMessage");
    System.out.println("The Message is: "+intent.getStringExtra("MyMessage"));
    CharSequence text = "Your tax amount due period";
    CharSequence contentTitle = "Tax Calculator App";

    long when = System.currentTimeMillis();

    intent = new Intent(context, MenuPageActivity.class);
    intent.putExtra("twoMonth", "twoMonth");
    PendingIntent contentIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

    notification = new Notification(icon,text,when);

    long[] vibrate = {0,100,200,300};
    notification.vibrate = vibrate;  // To vibrate the Device

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

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

    notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
    notificationManager.notify(NotificationConstants.NOTIFICATION_ID_TWO_MONTH, notification);
}

}

所有作品都适用于此代码。我得到了通知。但是使用这个我每次重启设备时都会收到通知。并且该通知中没有任何消息。为什么会这样发生? 请帮帮我。或者我的代码有什么问题???

感谢。 的被修改

我在manifest.xml中已经这样做了

<!-- To receive the Alarm Notification for two months-->
    <receiver android:name=".AlarmReceiverNotificationForTwoMonth" android:enabled="true">  
         <intent-filter>                 
            <action android:name="android.intent.action.BOOT_COMPLETED"/>                 
            <category android:name="android.intent.category.DEFAULT"/>             
         </intent-filter>   
    </receiver>  

1 个答案:

答案 0 :(得分:1)

您的清单中指定的AlarmReceiverNotificationForTwoMonth类是BOOT_COMPLETED广播的接收者吗?

如果是这样,那么每次设备启动时都会触发AlarmReceiverNotifictionForTwoMonth.onReceive()方法,从而显示通知。

编辑:使用AlarmManager的方法很好。唯一的问题是重启设备时不会恢复警报。因此,您需要在设备重新启动时重新创建任何现有警报。我的建议是将你的清单改为:

    <receiver android:name=".AlarmReceiverNotificationForTwoMonth" android:enabled="true" >
    </receiver>
    <receiver android:name=".RecreateAlarms" >
        <intent-filter >
            <action android:name="android.intent.action.BOOT_COMPLETED" />
        </intent-filter>
    </receiver>

然后创建一个新类

public class RecreateAlarms extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {

    // reschedule alarms here
}

当触发警报时,我不知道你的逻辑。您可能需要在AlarmReceiverNotificationForTwoMonth方法中将警报保存到文件/数据库,并在RescheduleAlarms中读取它以了解是否有任何警报要重新创建