1

i am making an app that required to inform the user with a notification when a specific date is approaching.

I use a Client class as a 'middle man' between activity and a Service. Once i bind the service, i call a method that interacts with AlarmTask class that uses AlarmManager to set an alarm. And as a last step i send a PendingIntent to start my another class witch is a BroadcastReceiver for my notification.

My problem is that the onReceive() is not called. Code reaches all the way to alarmManager.set() correctly. I read many posts and tried different ways to register my BroadcastReceiver. Any ideas on what might be wrong?

AlarmTask

public class AlarmTask implements Runnable {
    private final Calendar date;
    private final AlarmManager alarmManager;
    private final Context context;
    private long mGoalId;

    public AlarmTask(Context context, Calendar date, long goalId) {
        this.context = context;
        this.alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
        this.date = date;
        mGoalId = goalId;
    }

    @Override
    public void run() {
        Log.e("AlarmTask", "run executed with request code: " + mGoalId);
        // Request to start the service when the alarm date is upon us
        Intent intent = new Intent(context, NotificationReceiver.class);
        intent.putExtra(NotificationReceiver.INTENT_NOTIFY, true);
        // mGoalId is the unique goal id that is gonna be used for deletion
        PendingIntent pendingIntent = PendingIntent.getBroadcast(context, (int)mGoalId, intent, 0);
        alarmManager.set(AlarmManager.RTC, date.getTimeInMillis(), pendingIntent);
    }
}

NotificationReceiver

public class NotificationReceiver extends BroadcastReceiver {
    // Unique id to identify the notification.
    private static final int NOTIFICATION = 123;
    public static final String INTENT_NOTIFY = "com.test.name.services.INTENT_NOTIFY";
    private NotificationManager notificationManager;

    @Override
    public void onReceive(Context context, Intent intent) {
        Log.e("onReceive", "Broadcast fired : " + intent);
        CharSequence title = "Alarm!!";
        int icon = R.drawable.goal;
        CharSequence text = "Your notification time is upon us.";
        long time = System.currentTimeMillis();

        final NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
        builder.setSmallIcon(icon);
        builder.setContentTitle(title);
        builder.setContentText(text);
        builder.setVibrate(new long[] { 0, 200, 100, 200 });
        final Notification notification = builder.build();

        notificationManager.notify(NOTIFICATION, notification);
    }
}

Manifest

<receiver android:name=".broadcasts.NotificationReceiver"></receiver>
  • I think you need to put a filter on this receiver stating which intent it needs to react to.e.g. ` ` You'll just need to set the action to the one that fits you better. – luizfzs Jul 23 '17 at 18:25
  • i did tried using filter(although i am not sure what fits me better). I also tried this `` . I still don't get the `Log` on the `onReceived()` – Stamatis Stiliats Jul 23 '17 at 19:42

0 Answers0