Reputation: 307
I am trying to schedule local notifications in my app. Here is my RootReceiver
class.
public class RebootReceiver extends BroadcastReceiver {
private String EVENT_CATEGORY = "notification_event";
@Override
public void onReceive(Context context, Intent intent) {
Debug.waitForDebugger();
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Intent intent1 = new Intent(context, AlarmScheduler.class);
PendingIntent intentExecuted = PendingIntent.getBroadcast(context, 0, intent1, PendingIntent.FLAG_CANCEL_CURRENT);
Calendar now = Calendar.getInstance();
if (!GeneralMethods.getBooleanPreference(context, ProperatiPreferences.APP_FIRST_LAUNCH)) {
intent1.putExtra(EVENT_CATEGORY, "");
now.add(Calendar.HOUR, 2);
alarmManager.set(AlarmManager.RTC_WAKEUP, now.getTimeInMillis(), intentExecuted);
} else if (!GeneralMethods.getBooleanPreference(context, ProperatiPreferences.SEARCH_AFTER_THREE_DAYS)) {
intent1.putExtra(EVENT_CATEGORY, "");
now.add(Calendar.DATE, 3);
alarmManager.set(AlarmManager.RTC_WAKEUP, now.getTimeInMillis(), intentExecuted);
}
}
}
In here as you can see, I want to create an intent in which I want to put some data (intent1
). However the intent is always empty without any extras inside of it. What am I doing wrong?
Here is how I try to retrieve extras from the intent.
public class AlarmScheduler extends BroadcastReceiver {
private String EVENT_CATEGORY = "notification_event";
@Override
public void onReceive(final Context context, final Intent intent) {
Log.d("com.properati.user", "AlarmScheduler.onReceive() called");
Intent eventService = new Intent(context, AlarmService.class);
context.startService(eventService);
}
and finally my AlarmService
class:
public class AlarmService extends Service {
private String EVENT_CATEGORY = "notification_event";
@Override
public IBinder onBind(final Intent intent) {
return null;
}
@Override
public int onStartCommand(final Intent intent, final int flags, final int startId) {
Log.d("com.properati.user", "event received in service: " + new Date().toString());
if(intent.getStringExtra(EVENT_CATEGORY).equals(ProperatiPreferences.APP_FIRST_LAUNCH)){
new PushNotification(getApplicationContext()).scheduleNonOpenedNotification(getApplicationContext());
}else if(intent.getStringExtra(EVENT_CATEGORY).equals(ProperatiPreferences.SEARCH_AFTER_THREE_DAYS)){
new PushNotification(getApplicationContext()).scheduleNoSearchAfterThreeDays(getApplicationContext());
}
return Service.START_NOT_STICKY;
}
Upvotes: 1
Views: 249
Reputation: 568
Try the following code in AlarmScheduler class
public class AlarmScheduler extends BroadcastReceiver {
private String EVENT_CATEGORY = "notification_event";
@Override
public void onReceive(final Context context, final Intent intent) {
Log.d("com.properati.user", "AlarmScheduler.onReceive() called");
Intent eventService = new Intent(context, AlarmService.class);
eventService.putExtra(intent.getStringExtra(EVENT_CATEGORY, ""));
context.startService(eventService);
}
Upvotes: 1
Reputation: 3311
after I checked the PendingIntent source in Android framework, the intent argument will be cloned by new Intent(intent)
. so you need set all data to intent1 before passing it to PendingIntent constructor.
@Override
public IIntentSender getIntentSender(int type,
String packageName, IBinder token, String resultWho,
int requestCode, Intent[] intents, String[] resolvedTypes,
int flags, Bundle options, int userId) {
enforceNotIsolatedCaller("getIntentSender");
// Refuse possible leaked file descriptors
if (intents != null) {
if (intents.length < 1) {
throw new IllegalArgumentException("Intents array length must be >= 1");
}
for (int i=0; i<intents.length; i++) {
Intent intent = intents[i];
if (intent != null) {
if (intent.hasFileDescriptors()) {
throw new IllegalArgumentException("File descriptors passed in Intent");
}
if (type == ActivityManager.INTENT_SENDER_BROADCAST &&
(intent.getFlags()&Intent.FLAG_RECEIVER_BOOT_UPGRADE) != 0) {
throw new IllegalArgumentException(
"Can't use FLAG_RECEIVER_BOOT_UPGRADE here");
}
intents[i] = new Intent(intent);
}
}
if (resolvedTypes != null && resolvedTypes.length != intents.length) {
throw new IllegalArgumentException(
"Intent array length does not match resolvedTypes length");
}
}
Upvotes: 0