创建一个 android 通知,即使在应用程序关闭时,它也会在每天的某个时间重复出现

Creating an android notification that reoccurs every day at some time even when app is closed

关于这个主题的很多问题都有过时的答案(1-4岁)。

android 的文档没有引导我找到具体的解决方案,但帮助我理解了 AlarmManager 和 NotificationCompat。我的代码在我的 MainActivity

中看起来像这样
Intent notifyIntent = new Intent(this, MyReceiver.class);

PendingIntent pendingIntent = PendingIntent.getBroadcast(this, NOTIFICATION_REMINDER, 
notifyIntent, PendingIntent.FLAG_UPDATE_CURRENT);

Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, hours);
calendar.set(Calendar.MINUTE, minutes);
calendar.set(Calendar.SECOND, seconds);

AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,  calendar.getTimeInMillis(),
     AlarmManager.INTERVAL_DAY,
     pendingIntent); 

我的 BroadcastReceiver 看起来像这样

public class MyReceiver extends BroadcastReceiver {
    public MyReceiver() {
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        Log.d("Test", "RAN");
        Intent intent1 = new Intent(context, MyNewIntentService.class);
        context.startService(intent1);
    }
}

我的 IntentService 看起来像这样

public class MyNewIntentService extends IntentService {
    private static final int NOTIFICATION_ID = 3;

    public MyNewIntentService() {
        super("MyNewIntentService");
    }

    @Override
    protected void onHandleIntent(Intent intent) {
        NotificationManager mNotificationManager =
                (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
            NotificationChannel channel = new NotificationChannel("SH",
                    "Simple",
                    NotificationManager.IMPORTANCE_DEFAULT);
            channel.setDescription("Notifs");
            mNotificationManager.createNotificationChannel(channel);
        }

        NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(getApplicationContext(), "SH")
                .setSmallIcon(R.mipmap.ic_launcher) // notification icon
                .setContentTitle("Title") // title for notification
                .setContentText("Message")// message for notification
                .setAutoCancel(true); // clear notification after click
        Intent intent1 = new Intent(getApplicationContext(), MainActivity.class);
        PendingIntent pi = PendingIntent.getActivity(this, 0, intent1, PendingIntent.FLAG_UPDATE_CURRENT);
        mBuilder.setContentIntent(pi);
        mNotificationManager.notify(0, mBuilder.build());

    }
}

我已将这些添加到我的 AndroidManifest.xml 中,就在我的 activity 下方

<receiver
    android:name=".MyReceiver"
    android:enabled="true"
    android:exported="true" >
</receiver>
<service
    android:name=".MyNewIntentService"
    android:exported="false" >
</service>

通知为我触发,但在应用程序关闭时不会触发。在 android 文档中,似乎 android 试图限制后台处理的数量和应用程序的时间,因此 AlarmManager 没有 运行 在准确的时间。

如何将它变成我的应用程序的可靠通知提醒,即使应用程序已关闭,运行几乎每天都在同一时间发送?

你快到了。非常接近还有一件事。 您应该使用 startForegrounService() 而不是 startService()。在那种情况下,您的闹钟将始终响起。一些提示:

  1. 您应该在清单中声明前台服务
  2. API 等级高于 23
  3. 必须使用 alarmManager.setExactAndAllowWhileIdle

您还可以查看 了解更多信息。

更新:这个方法我在小米红米note 8上用过,但是突然不行了!目前,对于自定义 OS 设备来说,它可能不是一个非常可靠的解决方案。希望google想出一个稳妥的解决方案。