如何在应用程序关闭时生成通知以进行事件提醒?

How to generate notification when the application is closed for event reminder?

我是移动开发的初学者,我正在开发一个生日应用程序,它将在一个人生日的前几天和生日那天生成通知。我不明白当应用程序不是 运行 时要使用什么样的服务来跟踪生日之前的日子,到目前为止,我已经设法使用以下代码生成点击通知。

public class MainActivity extends AppCompatActivity implements View.OnClickListener, ItemClickListener {

    public void sendOnBirthdayChannelRemainingDays(View v){
        String message = "This is a very happy birthday to you message";
        Intent activityIntent = new Intent(this, MainActivity.class);
        activityIntent.putExtra("toastMessage", message);
        activityIntent.addCategory(Intent. CATEGORY_LAUNCHER ) ;
        activityIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
        PendingIntent contentIntent = PendingIntent.getActivity(getApplicationContext()
        , 0, activityIntent, PendingIntent.FLAG_UPDATE_CURRENT);

        Notification notification = new NotificationCompat.Builder(this, CHANNEL_BIRTHDAY_DAYSREMAINING_ID)
                .setSmallIcon(R.drawable.ic_gift_box)
                .setContentTitle("Birthday")
                .setContentText("Its one day before your birthday")
                .setAutoCancel(true)
                .setPriority(NotificationCompat.PRIORITY_MAX)
                .setCategory(NotificationCompat.CATEGORY_MESSAGE)
                .setContentIntent(contentIntent)
                .build();

        notificationManager.notify(1, notification);

    }
}

我进行了研究并找到了解决方案。我们将不得不将 AlarmManager class 与系统服务一起使用,使用挂起的意图来传递指定的日期。 OS 闹钟服务会在指定日期通知我们并唤醒设备。

只需要在MainActivity中调用这个函数

private void setAlarm(String date) {
    AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);

    Intent intent = new Intent(getApplicationContext(), NotificationReceiver.class);
    intent.putExtra("date", date); 
    

    PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 1000, intent, PendingIntent.FLAG_ONE_SHOT);
    String dateandtime = date + " " + "10:00";
    DateFormat formatter = new SimpleDateFormat("d-M-yyyy hh:mm");
    try {
        Date date1 = formatter.parse(dateandtime);
        Toast.makeText(mContext, "Alarm set to" + date1.toString(), Toast.LENGTH_SHORT).show();
        am.set(AlarmManager.RTC_WAKEUP, date1.getTime(), pendingIntent);

    } catch (ParseException e) {
        e.printStackTrace();
    }
}

And create a NotificationReceiver class

public class NotificationReceiver 扩展 BroadcastReceiver {

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

    Bundle bundle = intent.getExtras();
    String date = bundle.getString("dateString");

    //On notification click

    Intent clickNotficationIntent = new Intent(context, MainActivity.class);
    clickNotficationIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    clickNotficationIntent.putExtra("dateString", date);
    clickNotficationIntent.putExtra("shan", "myMessage");
    
    //Click Pending Intent
    PendingIntent pendingIntentClick = PendingIntent.getActivity(context,  2000, clickNotficationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    //Notification Builder
    Notification notification = new NotificationCompat.Builder(context, CHANNEL_BIRTHDAY_DAYSREMAINING_ID)
            .setSmallIcon(R.drawable.ic_gift_box)
            .setContentTitle("Birthday")
            .setContentText("Its one day before your birthday")
            .setAutoCancel(true)
            .setPriority(NotificationCompat.PRIORITY_MAX)
            .setCategory(NotificationCompat.CATEGORY_MESSAGE)
            .setContentIntent(pendingIntentClick)
            .build();

    notificationManager.notify(1, notification);
}

}

CHANNEL_BIRTHDAY_DAYSREMAINING_ID 是我的通知渠道,我们只需要创建一个新的 class 并在那里定义渠道。