Activity 立即显示而不是显示通知

Activity shown immediatly instead of showing a notification

我正在尝试使用 AlarmManager 在当天晚些时候向用户发送通知,无论他是否正在使用该应用程序。 我基本上粘贴了我在这里看到的内容:http://developer.android.com/guide/topics/ui/notifiers/notifications.html,但显然出了点问题,因为没有显示通知,而是显示了 AndroidLauncher Activity。

这是我的代码:

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);
    }

我对 Intents 和 PendingIntents 还是个新手,所以这可能是个愚蠢的错误。

~~~ ~~~ 编辑:谢谢您的帮助!现在我明白我需要一个 BroadcastReceiver,但我无法让它工作。看起来 onReceive() 从未被调用过 .

这是我的主要内容 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);
    }

还有我的 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());
    }
}

按照您的方式,警报管理器直接使用您的 resultIntent

您应该将通知创建代码移动到 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());
    }
}

并使用 AlarmManager 调用 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);