0

I have a calendar library I grabbed off of github: https://github.com/prolificinteractive/material-calendarview

And I'm having the user click on a date and add a reminder for that date, an alert dialog then pops up and asks them to enter the time they would like to be reminded on that day.

Now I was able to convert the text into a simpledate format and I spit it out into a string from a calendar object, so the date and time should be passing through the notification. But it doesn't seem to work anyways

Heres the code that sets the alarm:

Calendar cal = Calendar.getInstance();
        AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
        Intent notificationIntent = new Intent("android.media.action.DISPLAY_NOTIFICATION");
        notificationIntent.addCategory("android.intent.category.DEFAULT");
        PendingIntent broadcast = PendingIntent.getService(context, 100, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);

        try {
            cal.setTime(alertFormatter.parse(date));
            System.out.print("Date added successfully");
        } catch (ParseException e) {
            System.out.println("Failed to add date");
        }
        cal.add(Calendar.HOUR, Integer.parseInt(hour.getText().toString()));
        cal.add(Calendar.MINUTE, Integer.parseInt(minute.getText().toString()));
        cal.add(Calendar.SECOND, 0);
        if(spAMpm.getSelectedItem().equals("AM"))cal.add(Calendar.AM_PM, Calendar.AM);
        else if (spAMpm.getSelectedItem().equals("PM"))cal.add(Calendar.AM_PM, Calendar.PM);


        alarmManager.setExact(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), broadcast);

Then the receiver I created to do what I need it to do:

public class UpcomingWorkNotification extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {

Intent notificationIntent = new Intent(context, UpcomingWork.class);

TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
stackBuilder.addParentStack(UpcomingWork.class);
stackBuilder.addNextIntent(notificationIntent);

PendingIntent pendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);

NotificationCompat.Builder builder =
        new NotificationCompat.Builder(context)
        .setSmallIcon(R.drawable.ic_launcher)
        .setContentTitle("Four.oh")
        .setContentText("Assignment Due Soon!")
        .setContentIntent(pendingIntent);

NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, builder.build());

}
}

In the manifest I gave it this permission and added the receiver

<uses-permission android:name="android.permission.WAKE_LOCK"/>

<receiver android:name=".UpcomingWorkNotification">
  <action android:name="android.media.action.DISPLAY_NOTIFICATION" />
  <category android:name="android.intent.category.DEFAULT" />
</receiver>
QConscious
  • 87
  • 1
  • 1
  • 11

1 Answers1

2

I played around with this for a while and it seems like you have a few issues. One is this line,

PendingIntent broadcast = PendingIntent.getService(context, 100, notificationIntent,
      PendingIntent.FLAG_UPDATE_CURRENT);

You use getService(), but then have a BroadcastReceiver. I built a test setup, and if I did PendingIntent.getService(), my BroadcastReceiver never launched. But if I switched it to PendingIntent.getBroadcast() then the BroadcastReceiver did launch. PendingIntent.getBroadcast() takes the same arguments as getService().

The other line that seemed problematic is,

Intent notificationIntent = new Intent("android.media.action.DISPLAY_NOTIFICATION");

With that line, the receiver never launched. I had to change it to the following,

Intent notificationIntent = new Intent(theAppropriateContext, UpcomingWorkNotification.class);

notificationIntent.setAction("android.media.action.DISPLAY_NOTIFICATION");

Some references I used, the great Vogella tutorials, and this previous question on StackOverflow.

In the SO answer I linked, they seemed to solve the problem by using an intent-filter in the manifest. However, I tested with and without the intent-filter, and both cases worked. Although it would probably be better to put the intent-filter in, like this,

 <receiver android:name=".AlarmNotification">

                <action android:name = "android.media.action.DISPLAY_NOTIFICATION"/>
                <category android:name="android.intent.category.DEFAULT"/>

 </receiver>

EDIT: Did a little extra testing after getting a hunch. When using Intent.setAction(), then it works without the intent-filter. However, if I used an intent-filter, then the line as you have it,

Intent notificationIntent = new Intent("android.media.action.DISPLAY_NOTIFICATION");

does actually work. The benefit of this is that Intent.setAction() requires minimum sdk version 19. So you can support lower sdk versions if you use the intent-filter.

Matt Brown
  • 288
  • 2
  • 7