0

I am trying to create an app that sends an android notification 7 days before a specified date. Right now, I am reading in the right date and setting the calendar correctly, but for some reason, the alarm is not triggering. Additionally, when I do adb shell dumpsys alarm to see if the alarm is set, the dump does not display any alarms related to my app. My code that creates the AlarmManager and notification right now is

calendar.set(year, month, day, hour, min);
calendar.add(Calendar.DAY_OF_MONTH, -7);
AlarmManager alarmManager = (AlarmManager) context.getSystemService(ALARM_SERVICE);
Intent intent = new Intent("android.media.action.DISPLAY_NOTIFICATION");
PendingIntent broadcast = PendingIntent.getBroadcast(context, 100, intent, PendingIntent.FLAG_UPDATE_CURRENT);
alarmManager.setExact(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), broadcast);

What could be going wrong with this? Is this not the correct way to set an alarm? I got the idea from here

rma
  • 1,853
  • 1
  • 22
  • 42
  • You can refer [this](https://stackoverflow.com/a/35124436/5860777) and [this one](https://stackoverflow.com/a/35127736/5860777). This is not the answer of your question but you can get some idea from it.You need to little bit change in your code using this reference and it works fine. – Andy Developer Jun 06 '17 at 09:31
  • Are you sure that the dump doesn't display the alarm? I tried the exact same code and in my dump the alarm is displayed correctly? – MPhil Jun 06 '17 at 10:24
  • Could it be that this is not called from an activity? I had to pass the context in to perform this code. – rma Jun 06 '17 at 17:59

1 Answers1

0

I am doing to the following: use getNotification() and your calandar.getTime() as parameters for scheduleNotification

private void scheduleNotification(Notification notification, Date alarmTime) {

    Intent notificationIntent = new Intent(this, NotificationReceiver.class);
    notificationIntent.putExtra(NotificationReceiver.NOTIFICATION_ID, 1);
    notificationIntent.putExtra(NotificationReceiver.NOTIFICATION, notification);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);

    AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
    alarmManager.set(AlarmManager.RTC_WAKEUP, alarmTime.getTime(), pendingIntent);
}

private Notification getNotification() {
    int colour = getNotificationColour();
    Bitmap largeNotificationImage = getLargeNotificationImage();
    return new RandomNotification(this).getNotification(
            "Text",
            "More text",
            getNotificationImage(),
            largeNotificationImage,
            colour);
}

private int getNotificationColour() {
    return ContextCompat.getColor(this, R.color.colorAccent);
}

private Bitmap getLargeNotificationImage() {
    return BitmapFactory.decodeResource(this.getResources(),
            R.mipmap.ic_launcher);
}

My RandomNotification Class:

public class RandomNotification {

    private Context context;

    public RandomNotification(Context context) {
        this.context = context;
    }

    public Notification getNotification(String title, String message, int imageResourceId, Bitmap largeResource, int colour) {

        Uri soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        Notification.Builder builder = new Notification.Builder(context)
                .setContentTitle(title)
                .setContentText(message)
                .setSmallIcon(imageResourceId)
                .setLargeIcon(largeResource)
                .setTicker("Ticker")
                .addAction(0, "Button", getButtonIntent())
                .setSound(soundUri)
                .setLights(Color.BLUE, 300, 100)
                .setAutoCancel(true)
                .setPriority(Notification.PRIORITY_MAX)
                .setContentIntent(getPendingNotificationIntent());

        handleNotificationColor(builder, colour);

        Notification notification = builder.build();
        notification.flags = Notification.FLAG_AUTO_CANCEL | Notification.FLAG_SHOW_LIGHTS;
        return notification;
    }

    private Notification.Builder handleNotificationColor(Notification.Builder builder, int colour) {
        if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            builder.setColor(colour);
        }
        return builder;
    }

    private PendingIntent getPendingNotificationIntent() {
        Intent notificationIntent = new Intent(context, HomeActivity.class);
        notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
                | Intent.FLAG_ACTIVITY_SINGLE_TOP);
        PendingIntent pendingNotificationIntent = PendingIntent.getActivity(context, 1,
                notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
        return pendingNotificationIntent;
    }

    private PendingIntent getButtonIntent() {
        Intent notificationButtonReceiver = new Intent(context, NotificationButtonReceiver.class);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, notificationButtonReceiver, PendingIntent.FLAG_CANCEL_CURRENT);
        return pendingIntent;
    }

}

My notification button receiver class:

public class NotificationButtonReceiver extends BroadcastReceiver {



    private Context context;

    @Override
    public void onReceive(Context context, Intent intent) {
        this.context = context;
        // do something

    }


}

My updated notification receiver class

public class NotificationReceiver extends BroadcastReceiver {

    public static String NOTIFICATION_ID = "notification-id";

    public static String NOTIFICATION = "notification";

    @Override
    public void onReceive(Context context, Intent intent) {
        NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

        Notification notification = intent.getParcelableExtra(NOTIFICATION);
        int id = intent.getIntExtra(NOTIFICATION_ID, 0);
        notificationManager.notify(id, notification);
    }
}

My manifest file:

<receiver
    android:name=".NotificationButtonReceiver"
    android:exported="true">
    <intent-filter>
        <action android:name="android.intent.action.NOTIFY" />
    </intent-filter>
</receiver>
    <receiver
        android:name=".NotificationReceiver"
        android:exported="true">
        <intent-filter>
            <action android:name="android.intent.action.NOTIFY" />
        </intent-filter>
    </receiver>
archLucifer
  • 389
  • 1
  • 10