AlarmManager + NotificationManager 不工作

AlarmManager + NotificationManager not working

首先,我要感谢您点击此主题。 非常感谢您花时间阅读本文。

我正计划让应用程序在特定时间推送通知。 我在网上看了一些教程,但似乎不起作用。

代码本身没有错误消息,但没有发出通知。

这是我的 "Notification_receiver.java" 文件:

package com.example.reviewerapplication;

import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Build;

import androidx.annotation.RequiresApi;
import androidx.core.app.NotificationCompat;

public class Notification_receiver extends BroadcastReceiver {



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

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

    Intent repeating_intent = new Intent(context,Chapterchoice.class);
    String daily10 = "daily";
    intent.putExtra("daily",daily10);
    repeating_intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    PendingIntent pendingIntent = PendingIntent.getActivity(context,100,repeating_intent,PendingIntent.FLAG_UPDATE_CURRENT);



    NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
            .setContentIntent(pendingIntent)
            .setSmallIcon(R.drawable.ic_launcher)
            .setContentTitle("Sample Title")
            .setContentText("same message")
            .setAutoCancel(true);

    if (intent.getAction().equals("MY_NOTIFICATION_MESSAGE")) {
        notificationManager.notify(100, builder.build());
    }


  }
}

这是我的 MainActivity 上的内容:

       //Daily Notification
    val calendar = Calendar.getInstance()

    calendar.set(Calendar.HOUR_OF_DAY,21)
    calendar.set(Calendar.MINUTE,53)
    calendar.set(Calendar.SECOND,0)

    val intent2 = Intent(this, Notification_receiver::class.java)
    intent2.setAction("MY_NOTIFICATION_MESSAGE")
    var pendingIntent = PendingIntent.getBroadcast(this,100,intent2,PendingIntent.FLAG_UPDATE_CURRENT )
    var alarmManager = getSystemService(Context.ALARM_SERVICE) as AlarmManager
    alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,calendar.timeInMillis,AlarmManager.INTERVAL_DAY,pendingIntent)

其实我刚刚修好了

我的问题是我在清单中使用了 "activity" 而不是 "receiver"