重新启动或启动并取消特定日期的通知后,如何处理AlarmManager?

时间:2019-07-16 07:28:11

标签: android notifications alarmmanager android-pendingintent bootcompleted

我试图根据某些条件在我的android应用中显示一些本地通知,为此我正在使用AlarmManager和PendingIntents。现在,我也想在设备重启后恢复我的本地通知,为此,我知道我应该使用RECEIVE_BOOT_COMPLETED,但是我对实现部分感到困惑。我有一个疑问,例如我是否应该在使用触发通知的同一Receiver类中处理启动,还是应该有一个单独的Reciever类。还有如何取消特定日期的通知。

Manifest.xml '''

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

<receiver android:name=".BootReceiver"
        android:enabled="false">
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED"></action>
        </intent-filter>
    </receiver> 

我正在通过“主要活动”中的PendingIntent调用/启动我的NotificationReciever类 然后在使用ComponentName和PackageManager从此处设置警报之前,先启用我的BootReceiver类 '''

ComponentName receiver = new ComponentName(context, BootReceiver.class);
PackageManager pm = context.getPackageManager();

pm.setComponentEnabledSetting(receiver,
                PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
                PackageManager.DONT_KILL_APP);

Intent intent = new Intent(mCtx, NotificationReciever.class);
intent.putExtra(END_DATE, treatment.getEndDate());
intent.putExtra(MEDICINE_NAME,   treatment.getMedicineName());
AlarmManager alarmManager = (AlarmManager)    mCtx.getSystemService(ALARM_SERVICE);
intent.putExtra(REQUEST_CODE, req1);
pendingIntent = PendingIntent.getBroadcast(mCtx, req1, intent, 0);
alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, time1, AlarmManager.INTERVAL_DAY, pendingIntent);

通知接收者类 '''

public class NotificationReciever extends BroadcastReceiver {

private final String CHANNEL_ID = "personal_notifications";
public static String NOTIFICATION_MESSAGE = "notificationMessage";
private Context context;

@Override
public void onReceive(Context context, Intent intent) {
    System.out.println("Utils Inside NotificationReciever onReceive");
    String message = intent.getStringExtra(NOTIFICATION_MESSAGE);
    this.context = context;

    String msg = intent.getStringExtra("MEDICINE_TIME");
    System.out.println("Utils medicineTime " + msg);
    int requestCode = intent.getIntExtra("REQUEST_CODE", 0);
    System.out.println("Utils requestCode  " + requestCode);

    Intent resultIntent = new Intent(context, MainActivity.class);
    buildNotification(context, resultIntent, msg);
}

public void buildNotification(Context context, Intent resultIntent, String msg) {
    System.out.println("Utils Inside callTheMethod");

    NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    Uri soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

    resultIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);

    PendingIntent pendingIntent = PendingIntent.getActivity(context, 100, resultIntent, 0);
    NotificationCompat.Builder builder = new NotificationCompat.Builder(context, CHANNEL_ID);
    builder.setSmallIcon(R.drawable.ic_dosage_c);
    builder.setContentIntent(pendingIntent);
    builder.setContentTitle("It's time to take your medicine");
    builder.setAutoCancel(true);
    builder.setContentText(msg);
    builder.setSound(soundUri);
    builder.setLights(Color.RED, 1000, 1000);
    builder.setColor(color);

    Notification notification = builder.build();

    notification.flags |= Notification.FLAG_AUTO_CANCEL;

    notificationManager.notify(102938, notification);
}
}

BootReciever类 '''

public class BootReceiver extends BroadcastReceiver {

private final String CHANNEL_ID = "personal_notifications";
private Context context;

@Override
public void onReceive(Context context, Intent intent) {
    if (intent.getAction().equals("android.intent.action.BOOT_COMPLETED")) {
        System.out.println("Utils Inside BootReceiver onReceive");
        this.context = context;

        String msg = intent.getStringExtra("MEDICINE_TIME");
        System.out.println("Utils medicineTime " + msg);
        int requestCode = intent.getIntExtra("REQUEST_CODE", 0);
        System.out.println("Utils requestCode  " + requestCode);

        Intent resultIntent = new Intent(context, MainActivity.class);
        resultIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);

        PendingIntent pendingIntent = PendingIntent.getActivity(context, 100, resultIntent, 0);
        buildNotification(context, pendingIntent, msg);
    }
}

public void buildNotification(Context context, PendingIntent pendingIntent, String msg) {
    System.out.println("Utils Inside callTheMethod");

    NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    Uri soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

    int color = 0xffffaa00;
    NotificationCompat.Builder builder = new NotificationCompat.Builder(context, CHANNEL_ID);
    builder.setSmallIcon(R.drawable.ic_dosage_c);
    builder.setContentIntent(pendingIntent);
    builder.setContentTitle("It's time to take your medicine reboot!");
    builder.setAutoCancel(true);
    builder.setContentText(msg);
    builder.setSound(soundUri);
    builder.setLights(Color.RED, 1000, 1000);
    builder.setColor(color);

    Notification notification = builder.build();

    notification.flags |= Notification.FLAG_AUTO_CANCEL;

    notificationManager.notify(102938, notification);
}

0 个答案:

没有答案