如何在Android中安排通知

45

我想设置一个未来的通知时间。 我已经有了创建通知的代码,但是找不到可以安排通知的选项。

如何安排通知?


2
这仍然很有用,因为另一个问题是在此之前3年提出的,一些基本行为已经发生了变化,所以谢谢! - Afzal N
如果仍然遇到相同的问题,这可能会有所帮助:https://developer.android.com/training/scheduling/alarms#boot - user9339131
2个回答

46

不适用于Oreo+(编辑)

以上回答很好,但是没有考虑到用户可能会重新启动设备(这将清除由AlarmManager安排的PendingIntent)。

您需要创建一个WakefulBroadcastReceiver,其中包含AlarmManager以安排投递PendingIntent。当WakefulBroadcastReceiver处理意图时-发布通知并发出WakefulBroadcastReceiver以完成信号。

WakefulBroadcastReceiver

    /**
     * When the alarm fires, this WakefulBroadcastReceiver receives the broadcast Intent
     * and then posts the notification.
     */
    public class WakefulReceiver extends WakefulBroadcastReceiver {
        // provides access to the system alarm services.
        private AlarmManager mAlarmManager;

        public void onReceive(Context context, Intent intent) {
            //// TODO: post notification
            WakefulReceiver.completeWakefulIntent(intent);
        }

        /**
         * Sets the next alarm to run. When the alarm fires,
         * the app broadcasts an Intent to this WakefulBroadcastReceiver.
         * @param context the context of the app's Activity.
         */
        public void setAlarm(Context context) {
            mAlarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
            Intent intent = new Intent(context, WakefulReceiver.class);
            PendingIntent alarmIntent = PendingIntent.getBroadcast(context, 0, intent, 0);

            Calendar calendar = Calendar.getInstance();
            calendar.setTimeInMillis(System.currentTimeMillis());
            //// TODO: use calendar.add(Calendar.SECOND,MINUTE,HOUR, int);
            //calendar.add(Calendar.SECOND, 10);

            //ALWAYS recompute the calendar after using add, set, roll
            Date date = calendar.getTime();

            mAlarmManager.setExact(AlarmManager.RTC_WAKEUP, date.getTime(), alarmIntent);

            // Enable {@code BootReceiver} to automatically restart when the
            // device is rebooted.
            //// TODO: you may need to reference the context by ApplicationActivity.class
            ComponentName receiver = new ComponentName(context, BootReceiver.class);
            PackageManager pm = context.getPackageManager();
            pm.setComponentEnabledSetting(receiver, PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
                    PackageManager.DONT_KILL_APP);
        }

        /**
         * Cancels the next alarm from running. Removes any intents set by this
         * WakefulBroadcastReceiver.
         * @param context the context of the app's Activity
         */
        public void cancelAlarm(Context context) {
            Log.d("WakefulAlarmReceiver", "{cancelAlarm}");

            mAlarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
            Intent intent = new Intent(context, WakefulReceiver.class);
            PendingIntent alarmIntent = PendingIntent.getBroadcast(context, 0, intent, 0);

            mAlarmManager.cancel(alarmIntent);

            // Disable {@code BootReceiver} so that it doesn't automatically restart when the device is rebooted.
            //// TODO: you may need to reference the context by ApplicationActivity.class
            ComponentName receiver = new ComponentName(context, BootReceiver.class);
            PackageManager pm = context.getPackageManager();
            pm.setComponentEnabledSetting(receiver, PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
                    PackageManager.DONT_KILL_APP);
        }

BootReceiver

    public class BootReceiver extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
            if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)) {
            context = ApplicationActivity.class;
            AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
            Intent intent = new Intent(context, WakefulReceiver.class);
            PendingIntent alarmIntent = PendingIntent.getBroadcast(context, 0, intent, 0);

            Calendar calendar = Calendar.getInstance();
            calendar.setTimeInMillis(System.currentTimeMillis());
            //// TODO: use calendar.add(Calendar.SECOND,MINUTE,HOUR, int);
            //calendar.add(Calendar.SECOND, 10);

            //ALWAYS recompute the calendar after using add, set, roll
            Date date = calendar.getTime();

            alarmManager.setExact(AlarmManager.RTC_WAKEUP, date.getTime(), alarmIntent);
            }
        }
    }

AndroidManifest.xml

<receiver android:name=".WakefulReceiver"/>

<receiver android:name=".BootReceiver"
    android:enabled="false">
    <intent-filter>
        <action android:name="android.intent.action.BOOT_COMPLETED"/>
    </intent-filter>
</receiver>

3
BootReceiver.class中,您不应手动输入"android.intent.action.BOOT_COMPLETED",而应该使用Intent.ACTION_BOOT_COMPLETED - SARose
3
请记得在 Manifest 文件中添加权限:<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />。 - Pelanes
8
" WakefulBroadcastReceiver 在Android 8.0中已经弃用,因此这个解决方案可能在Android 8.0上无法使用。" - DevAndroid
2
Android 8 Oreo的解决方案似乎是扩展BroadcastReceiver而不是WakefulBroadcastReceiver,请参见https://dev59.com/5FcO5IYBdhLWcg3wcRFk#45825690。 - outofthecave
2
有人能编辑并解释一下为什么它不应该在SDK 26+中使用吗? - Allan Veloso
显示剩余7条评论

42

你需要使用PendingIntentBroadCastReceiver 来完成这件事 -

public void scheduleNotification(Context context, long delay, int notificationId) {//delay is after how much time(in millis) from current time you want to schedule the notification
        NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
                .setContentTitle(context.getString(R.string.title))
                .setContentText(context.getString(R.string.content))
                .setAutoCancel(true)
                .setSmallIcon(R.drawable.app_icon)
                .setLargeIcon(((BitmapDrawable) context.getResources().getDrawable(R.drawable.app_icon)).getBitmap())
                .setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION));

        Intent intent = new Intent(context, YourActivity.class);
        PendingIntent activity = PendingIntent.getActivity(context, notificationId, intent, PendingIntent.FLAG_CANCEL_CURRENT);
        builder.setContentIntent(activity);

        Notification notification = builder.build();

        Intent notificationIntent = new Intent(context, MyNotificationPublisher.class);
        notificationIntent.putExtra(MyNotificationPublisher.NOTIFICATION_ID, notificationId);
        notificationIntent.putExtra(MyNotificationPublisher.NOTIFICATION, notification);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(context, notificationId, notificationIntent, PendingIntent.FLAG_CANCEL_CURRENT);

        long futureInMillis = SystemClock.elapsedRealtime() + delay;
        AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
        alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, futureInMillis, pendingIntent);
    }

此外,您需要在接收器类中显示通知 -

public class MyNotificationPublisher extends BroadcastReceiver {

    public static String NOTIFICATION_ID = "notification_id";
    public static String NOTIFICATION = "notification";

    @Override
    public void onReceive(final Context context, Intent intent) {

        NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

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

最后,使用合适的参数调用scheduleNotification()即可!


每天中午12点,例如...这些代码是放在主活动中吗? - Si8
5
当应用程序在“最近使用应用程序列表”中被滑动时,这段代码安排的闹钟将被取消。如何避免这个问题? - NghiaDao
2
似乎接受的答案应该是下面apelsoczi的答案。如果手机重新启动,安排通知而没有保证在预定日期交付似乎相对无用。 - Aaron Smentkowski
1
这个答案是不正确的,因为你正在将图像放到意图中。这是反模式,可能会导致错误。请查看:https://dev59.com/pOo6XIcBkEYKwwoYJA3p - stillwaiting
13
除非在 AndroidManifest.xml 文件的 <application> 标签下添加以下内容,否则无法正常工作: <receiver android:name=".MyNotificationPublisher" /> 注意,MyNotificationPublisher.java 文件与之在同一文件夹中。 - enarcee
显示剩余2条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接