[英]Scheduling a notification to a specific date and time
我是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>
我很困惑為什么這不起作用 - 我仔細研究了我的谷歌結果試圖弄清楚。 我在以下之間切換:1) 使用System.ELAPSED_REALTIME_WAKEUP
設置AlarmManager
2) 傳遞SystemClock.elapsedRealTime() + diffInMillies
3) 僅傳遞diffInMillis
4) 傳遞僅代表幾秒鍾的整數值而不是diffInMillis
以查看它是否適用全部
任何人都可以幫助解決這個問題? 非常感謝!
創建通知時是否設置了頻道 ID? 如果您在 API 26 上進行測試,如果沒有一組以及廣播接收器中的一個頻道,則警報不會響起。
我有兩種方法可以從時間選擇器和用戶選擇的警報中創建和設置提醒。 這是他們的源代碼
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);
}
}
從外觀上看,幾乎和你的一模一樣。
這對我很有效。 如果我能以任何其他方式提供幫助,請告訴我。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.