Baguette
Baguette

Reputation: 159

Activity shown immediatly instead of showing a notification

I'm trying to use the AlarmManager to send a notification to the user later in the day, whether he is using the application or not. I basically pasted what I saw here : http://developer.android.com/guide/topics/ui/notifiers/notifications.html, but obviously something went wrong because instead of showing a notification, the AndroidLauncher Activity is shown.

Here is my code :

public void planNotification() {
        AlarmManager am = (AlarmManager)this.getSystemService(Context.ALARM_SERVICE);

        NotificationCompat.Builder mBuilder =
                new NotificationCompat.Builder(this)
                .setSmallIcon(R.drawable.icon)
                .setContentTitle("My notification")
                .setContentText("Hello World!");

        Intent resultIntent = new Intent(this, AndroidLauncher.class);

        TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
        stackBuilder.addParentStack(AndroidLauncher.class);
        stackBuilder.addNextIntent(resultIntent);
        PendingIntent resultPendingIntent =
                stackBuilder.getPendingIntent(
                    0,
                    PendingIntent.FLAG_UPDATE_CURRENT
                );
        mBuilder.setContentIntent(resultPendingIntent);

        int currentApiVersion = android.os.Build.VERSION.SDK_INT;

        if (currentApiVersion >= 19)
            am.setWindow(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + 5000, 5000, resultPendingIntent);
        else
            am.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + 86400000, resultPendingIntent);
    }

I'm still new to Intents and PendingIntents, so it may be a dumb mistake.

~~~ ~~~ EDIT : Thank you for your help ! Now that I understood that I need a BroadcastReceiver, I can't get it to work. It looks like onReceive() is never called.

Here is my Main Activity :

@Override
    public void planNotification() {
        AlarmManager am = (AlarmManager)this.getSystemService(Context.ALARM_SERVICE);

        Intent intent = new Intent(this, AlarmReceiver.class);
        PendingIntent alarmIntent = PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

        int currentApiVersion = android.os.Build.VERSION.SDK_INT;

        if (currentApiVersion >= 19)
            am.setWindow(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + 5000, 5000, alarmIntent);
        else
            am.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + 5000, alarmIntent);
    }

And my BroadcastReceiver :

public class AlarmReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        System.out.println("ON RECEIVE");

        NotificationCompat.Builder mBuilder =
            new NotificationCompat.Builder(context)
            .setSmallIcon(R.drawable.icon)
            .setContentTitle("My notification")
            .setContentText("Hello World!");

        Intent resultIntent = new Intent(context, AndroidLauncher.class);

        TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
        stackBuilder.addParentStack(AndroidLauncher.class);
        stackBuilder.addNextIntent(resultIntent);
        PendingIntent resultPendingIntent =
            stackBuilder.getPendingIntent(
                0,
                PendingIntent.FLAG_UPDATE_CURRENT
            );
        mBuilder.setContentIntent(resultPendingIntent);
        NotificationManager mNotificationManager =
            (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        mNotificationManager.notify(AndroidLauncher.notifId, mBuilder.build());
    }
}

Upvotes: 0

Views: 109

Answers (1)

tachyonflux
tachyonflux

Reputation: 20211

The way you have it, the alarm manager is directly using your resultIntent.

You should move your notification creation code to a BroadcastReceiver:

public class AlarmReceiver extends BroadcastReceiver{
    @Override
    public void onReceive(Context context, Intent intent) {
        NotificationCompat.Builder mBuilder =
            new NotificationCompat.Builder(this)
            .setSmallIcon(R.drawable.icon)
            .setContentTitle("My notification")
            .setContentText("Hello World!");

        Intent resultIntent = new Intent(this, AndroidLauncher.class);

        TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
        stackBuilder.addParentStack(AndroidLauncher.class);
        stackBuilder.addNextIntent(resultIntent);
        PendingIntent resultPendingIntent =
            stackBuilder.getPendingIntent(
                0,
                PendingIntent.FLAG_UPDATE_CURRENT
            );
        mBuilder.setContentIntent(resultPendingIntent);
        NotificationManager mNotificationManager =
            (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        mNotificationManager.notify(mId, mBuilder.build());
    }
}

And use the AlarmManager to call the BroadcastReceiver:

Intent intent = new Intent(this, AlarmReceiver.class);
PendingIntent alarmIntent = PendingIntent.getBroadcast(this, 0, intent, 0);
if (currentApiVersion >= 19)
    am.setWindow(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + 5000, 5000, alarmIntent);
else
    am.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + 86400000, alarmIntent);

Upvotes: 1

Related Questions