gpt4 book ai didi

java - 将通知安排在特定日期和时间

转载 作者:行者123 更新时间:2023-12-02 12:06:17 24 4
gpt4 key购买 nike

我是Call Manager的开发者,我正在尝试为我的应用程序实现通知提醒功能。这个想法是,用户可以为自己设置一个提醒,以调用列表中的特定人员。起初,在实现此功能时,通知会立即显示而无需安排 - 这是由于值设置不正确导致负毫秒数。然而,现在我已经纠正了这个问题,即使我有正确的毫秒值给 AlarmManager,通知也根本不会被安排。

安排通知的方法如下:

public void scheduleReminder(Notification notification, String date, String time){
String[] dateArray = date.split("/");
String[] timeArray = time.split(":|\\s+");
Date currentDate = new Date();
int notHour;

Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(System.currentTimeMillis());
cal.clear();
if(timeArray[2].equals("PM")){
notHour = Integer.parseInt(timeArray[0]);
notHour = notHour + 12;
cal.set(Calendar.YEAR, Integer.parseInt(dateArray[2]));
cal.set(Calendar.MONTH, Integer.parseInt(dateArray[0]) - 1);
cal.set(Calendar.DAY_OF_MONTH, Integer.parseInt(dateArray[1]));
cal.set(Calendar.HOUR_OF_DAY, notHour);
cal.set(Calendar.MINUTE, Integer.parseInt(timeArray[1]));
}
else{
cal.set(Calendar.YEAR, Integer.parseInt(dateArray[2]));
cal.set(Calendar.MONTH, Integer.parseInt(dateArray[0]) - 1);
cal.set(Calendar.DAY_OF_MONTH, Integer.parseInt(dateArray[1]));
cal.set(Calendar.HOUR_OF_DAY, Integer.parseInt(timeArray[0]));
cal.set(Calendar.MINUTE, Integer.parseInt(timeArray[1]));
}

Date reminderDate = cal.getTime();
long diffInMillis = reminderDate.getTime() - currentDate.getTime();

Intent notificationIntent = new Intent(this, NotificationPublisher.class);
notificationIntent.putExtra(NotificationPublisher.NOTIFICATION_ID, 1);
notificationIntent.putExtra(NotificationPublisher.NOTIFICATION, notification);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);

AlarmManager alarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + diffInMillis, pendingIntent);
}

我的广播接收器类如下:

package groovinchip.com.callmanager;

import android.app.Notification;
import android.app.NotificationManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

public class NotificationPublisher extends BroadcastReceiver {

public static String NOTIFICATION_ID = "notification-id";
public static String NOTIFICATION = "notification";

@Override
public void onReceive(Context context, Intent intent) {
NotificationManager notificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);

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

相关Android Manifest声明如下:

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

<receiver android:name="groovinchip.com.callmanager.NotificationPublisher"
android:enabled="true">
</receiver>

我很困惑为什么这不起作用 - 我仔细研究了我的 Google 结果试图找出答案。我已经在以下之间切换:1)使用System.ELAPSED_REALTIME_WAKEUP设置AlarmManager2)传递SystemClock.elapsedRealTime() + diffInMillies3)仅传递diffInMillis4) 传递一个仅代表几秒的整数值而不是 diffInMillis 来查看它是否有效

有谁能帮忙解决这个问题吗?非常感谢!

最佳答案

当您创建通知时,您是否设置了 channel ID?如果您在 API 26 上进行测试,如果广播接收器中没有一组且没有 channel ,则警报不会响起。

我有两种方法可以通过时间选择器和用户选择的闹钟创建和设置提醒。这是它们的源代码

private void createReminder(Notification notification) {
Intent notificationIntent = new Intent(this, NotificationPublisher.class);
notificationIntent.putExtra(NotificationPublisher.NOTIFICATION_ID, 1);
notificationIntent.putExtra(NotificationPublisher.NOTIFICATION, notification);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
long delay = alarmCalendar.getTimeInMillis() - Calendar.getInstance().getTimeInMillis();
long futureInMillis = SystemClock.elapsedRealtime() + delay;
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, futureInMillis, pendingIntent);
}

private Notification getNotification() {
String channelId = "Reminders";
PendingIntent newEntryActivityPendingIntent = PendingIntent.getActivity(this, 1, new Intent(this, NewEntryActivity.class), PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, channelId)
.setContentTitle(getString(R.string.app_name))
.setContentText(getString(R.string.reminder_content))
.setTicker(getString(R.string.app_name))
.setSmallIcon(R.drawable.notebook_notification_white)
.setDefaults(Notification.DEFAULT_SOUND)
.setAutoCancel(true)
.setContentIntent(newEntryActivityPendingIntent);
Log.i(TAG, "notification built");
return builder.build();
}

在我的应用程序中,我有一个通知提醒,并且我的广播接收器有一个单独的类,与您类似,这就是我的外观

    public class NotificationPublisher extends BroadcastReceiver {

private static final String TAG = "NotificationPublisher";
public static String NOTIFICATION_ID = "notification-id";
public static String NOTIFICATION = "notification";

@Override
public void onReceive(Context context, Intent intent) {
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= 26) {
NotificationChannel channel = new NotificationChannel("Reminders", "Reminders", NotificationManager.IMPORTANCE_DEFAULT);
notificationManager.createNotificationChannel(channel);
}
Notification notification = intent.getParcelableExtra(NOTIFICATION);
int id = intent.getIntExtra(NOTIFICATION_ID, 0);
Log.i(TAG, "notification sent");
notificationManager.notify(id, notification);
}
}

从外观上看,几乎和你的一模一样。

这对我来说很有效。如果我能以其他方式提供帮助,请告诉我。

关于java - 将通知安排在特定日期和时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46892042/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com