将通知安排到特定的日期和时间

时间:2017-10-23 14:36:04

标签: java android notifications alarmmanager

我是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)使用AlarmManager设置System.ELAPSED_REALTIME_WAKEUP 2)通过SystemClock.elapsedRealTime() + diffInMillies 3)仅通过diffInMillis 4)传递一个仅表示几秒而不是diffInMillis的整数值,以查看它是否可以正常工作

任何人都可以帮忙吗?非常感谢你!

1 个答案:

答案 0 :(得分:0)

创建通知时,您是否设置了频道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);
}

}

几乎和你的一样,从它的外观来看。

这对我很有用。如果我能以任何其他方式提供帮助,请告诉我。