Reputation: 23606
In my app, i am setting the Notification as like below code:
// 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);
Broadcast class for notification is:
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);
}
}
All Works good with this code. and i got the Notification. But using this i got notification everytime when ever i restart the device. and there are no any message in that notification. Why it is happend like this??? Please help me for this. or what wrong in my code ???
Thanks. Edited
I have done like this in 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>
Upvotes: -1
Views: 298
Reputation: 15619
Is your AlarmReceiverNotificationForTwoMonth
class specified in your manifest as the receiver of the BOOT_COMPLETED broadcast?
If so, then the AlarmReceiverNotifictionForTwoMonth.onReceive()
method will be triggered every time the device has booted, and as a result show a notification.
EDIT: Your approach with using the AlarmManager is good. The only problem is that alarms are not restored when the device is rebooted. Therefore you need to recreate any existing alarm when the device reboots. My suggestion would be to change your manifest to this:
<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>
Then create a new class
public class RecreateAlarms extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// reschedule alarms here
}
I don't know your logic when an alarm should be triggered. You may need to save an alarm to a file/database in your AlarmReceiverNotificationForTwoMonth method and read it in the RescheduleAlarms to know if there are any alarms to recreate
Upvotes: 1