如何在Android Oreo上在特定时间提供通知?

11

我正在寻找一种在Android应用程序中创建"偏好设置",使得用户可以在设置中设定特定时间,在该时间发送通知的方法。我查看了不同的线程,例如这个,但是在Android Oreo上无法使用。

有人能帮助我或者指导我如何实现吗?


1
既然没有人回复...通知真的必须在特定时间发送吗?在许多情况下不需要,但如果答案是肯定的,那么AlarmManager.setAlarmClock是唯一现实的本地替代方案。另一个解决方案是使用高优先级FCM:Firebase Cloud Message。如果可以容忍长达~10分钟的延迟,则类似AlarmManager.setExactAndAllowWhileIdle的东西可能会有用。否则,请查看您的应用程序是否可以容忍使用JobScheduler。 - Elletlar
是的,我想在用户指定的时间发送每日通知。 Alarm Manager 可以在我的情况下工作,但我正在寻找一种实现方法。确切地说,我需要适用于 Android Oreo 的教程。 - Tricky Bay
2个回答

19

查看不同帖子并进行了一些对AlarmManager实现的研究后,这是对我有效的内容。

这个基础是这个帖子和安排重复提醒Android文档

这是我的当前实现:

我有一个SwitchPreference和一个TimePicker实现在Settings中。

SwitchPreference询问用户是否要启用每日重复通知。

TimePicker设置通知时间。

MainActivityOnCreate方法或者你正在读取SharedPreferences的任何地方做如下操作:

PreferenceManager.setDefaultValues(this, R.xml.preferences, false);
SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(this);
Boolean dailyNotify = sharedPref.getBoolean(SettingsActivity.KEY_PREF_DAILY_NOTIFICATION, true);
PackageManager pm = this.getPackageManager();
ComponentName receiver = new ComponentName(this, DeviceBootReceiver.class);
Intent alarmIntent = new Intent(this, AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, alarmIntent, 0);
AlarmManager manager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);

// if user enabled daily notifications
if (dailyNotify) {
    //region Enable Daily Notifications
    Calendar calendar = Calendar.getInstance();
    calendar.setTimeInMillis(System.currentTimeMillis());
    calendar.set(Calendar.HOUR_OF_DAY, sharedPref.getInt("dailyNotificationHour", 7));
    calendar.set(Calendar.MINUTE, sharedPref.getInt("dailyNotificationMin", 15));
    calendar.set(Calendar.SECOND, 1);
    // if notification time is before selected time, send notification the next day
    if (calendar.before(Calendar.getInstance())) {
        calendar.add(Calendar.DATE, 1);
    }
    if (manager != null) {
        manager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
                AlarmManager.INTERVAL_DAY, pendingIntent);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            manager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
        }
    }
    //To enable Boot Receiver class
    pm.setComponentEnabledSetting(receiver,
            PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
            PackageManager.DONT_KILL_APP);
    //endregion
} else { //Disable Daily Notifications
    if (PendingIntent.getBroadcast(this, 0, alarmIntent, 0) != null && manager != null) {
        manager.cancel(pendingIntent);
        //Toast.makeText(this,"Notifications were disabled",Toast.LENGTH_SHORT).show();
    }
    pm.setComponentEnabledSetting(receiver,
            PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
            PackageManager.DONT_KILL_APP);
}

接下来添加AlarmReceiver类,该类实现BroadcastReceiver,代码如下:

public class AlarmReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {

    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(Objects.requireNonNull(context));
    SharedPreferences.Editor sharedPrefEditor = prefs.edit();

    NotificationManager nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    Intent notificationIntent = new Intent(context, MainActivity.class);

    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
            | Intent.FLAG_ACTIVITY_SINGLE_TOP);

    PendingIntent pendingI = PendingIntent.getActivity(context, 0,
            notificationIntent, 0);
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
            NotificationChannel channel = new NotificationChannel("default",
                    "Daily Notification",
                    NotificationManager.IMPORTANCE_DEFAULT);
            channel.setDescription("Daily Notification");
            if (nm != null) {
                nm.createNotificationChannel(channel);
            }
        }
        NotificationCompat.Builder b = new NotificationCompat.Builder(context, "default");
        b.setAutoCancel(true)
                .setDefaults(NotificationCompat.DEFAULT_ALL)
                .setWhen(System.currentTimeMillis())
                .setSmallIcon(R.mipmap.ic_launcher_foreground)
                .setTicker("{Time to watch some cool stuff!}")
                .setContentTitle("My Cool App")
                .setContentText("Time to watch some cool stuff!")
                .setContentInfo("INFO")
                .setContentIntent(pendingI);

        if (nm != null) {
            nm.notify(1, b.build());
            Calendar nextNotifyTime = Calendar.getInstance();
            nextNotifyTime.add(Calendar.DATE, 1);
            sharedPrefEditor.putLong("nextNotifyTime", nextNotifyTime.getTimeInMillis());
            sharedPrefEditor.apply();
        }
    }
}

如果设备被关闭或重新启动,则系统将关闭AlarmManager,因此在BOOT COMPLETE上重新启动它,请添加此类:

public class DeviceBootReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
    if (Objects.equals(intent.getAction(), "android.intent.action.BOOT_COMPLETED")) {
        // on device boot complete, reset the alarm
        Intent alarmIntent = new Intent(context, AlarmReceiver.class);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, alarmIntent, 0);

        AlarmManager manager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
        final SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(Objects.requireNonNull(context));

        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(System.currentTimeMillis());
        calendar.set(Calendar.HOUR_OF_DAY, sharedPref.getInt("dailyNotificationHour", 7));
        calendar.set(Calendar.MINUTE, sharedPref.getInt("dailyNotificationMin", 15));
        calendar.set(Calendar.SECOND, 1);

        Calendar newC = new GregorianCalendar();
        newC.setTimeInMillis(sharedPref.getLong("nextNotifyTime", Calendar.getInstance().getTimeInMillis()));

        if (calendar.after(newC)) {
            calendar.add(Calendar.HOUR, 1);
        }

        if (manager != null) {
            manager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
                    AlarmManager.INTERVAL_DAY, pendingIntent);
        }
    }
}
}

最后不要忘记将这些权限添加到AndroidManifest中:

<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

并在AndroidManifest中注册您的接收器。

<application
    <!--YOUR APPLICATION STUFF-->

    <receiver android:name=".DeviceBootReceiver"
        android:enabled="false">
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED" />
        </intent-filter>
    </receiver>
    <receiver android:name=".AlarmReceiver" />
通知应在由TimePicker指定的特定时间设置,并且如果用户启用了SwitchPreference,则应该设置它。更新(2022年8月):对于Android 12设备,您需要将pendingIntent的标志从0修改为FLAG_IMMUTABLE,如下所示:
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, alarmIntent, FLAG_IMMUTABLE);

在 Build.VERSION_CODES.R 版本之前,默认情况下假定 PendingIntents 是可变的,除非设置了 FLAG_IMMUTABLE 标志。从 Build.VERSION_CODES.S 开始,在创建 PendingIntents 时必须显式指定其可变性,可以使用 FLAG_IMMUTABLE 或 FLAG_MUTABLE 标志。您可以在此处找到有关 PendingIntent 标志的更多信息。


3

在StackOverflow上有很多关于如何实现这种功能的例子,但不幸的是,它们都无法正常工作,因为谷歌改变了AlarmManager的工作方式。

  • Android 6:Doze
  • Android 7:激进的Doze [关闭屏幕后不久禁用CPU]
  • Android 8:对后台服务的额外限制

唯一允许开发人员在特定时间唤醒设备的AlarmManager选项是AlarmManager.setAlarmClock。

使用setAlarmClock

除了闹钟外,您还可以设置一个意图,以便在点击闹钟时启动您的应用程序。

通过高优先级FCM(Firebase Cloud Message),也可以在特定时间唤醒设备。

但总之,没有其他选择:'setExact'和相关方法不再像名称所述那样工作。


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