Как удалить уведомление, установленное календарем? - PullRequest
0 голосов
/ 16 мая 2018

Мои уведомления настроены следующим образом

 AlarmManager alarmManager = (AlarmManager) getActivity().getSystemService(Context.ALARM_SERVICE);
            Calendar cal = Calendar.getInstance();
            cal.add(Calendar.MONTH, month);
            cal.add(Calendar.DAY_OF_MONTH, day - 1);
            cal.add(Calendar.YEAR, year);
            cal.add(Calendar.HOUR_OF_DAY, Calendar.AM);

            Intent intent= new Intent(getActivity(), Receiver.class);
            PendingIntent broadcast = PendingIntent.getBroadcast(getActivity(), 100, intent, PendingIntent.FLAG_UPDATE_CURRENT);
            if (alarmManager!= null) {
                alarmManager.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), broadcast);
            }

Затем в моем классе приемника

public class Receiver extends BroadcastReceiver {

public String mContentTitle, mContentText;
private static final int uniqueID = 57891258;
public NotificationCompat.Builder notification;

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

    SharedPreferences prefs = context.getSharedPreferences("notifications", Context.MODE_PRIVATE);
    String name = prefs.getString("notificationname", null);

    GoogleSignInAccount account = GoogleSignIn.getLastSignedInAccount(context);
    if (account != null ) {
        String firstname =  account.getGivenName();
        mContentTitle = "It's time to do your " + name ;
        mContentText = firstname + ", make sure to finish your" + name + " by April 12";
    }

    Intent openApp = new Intent(context, MainActivity.class);
    PendingIntent contentIntent = PendingIntent.getActivity(context, 1,openApp, PendingIntent.FLAG_UPDATE_CURRENT);

    notification = new NotificationCompat.Builder(context, "Default")
            .setSmallIcon(R.drawable.ic_restaurant_black_24dp)
            .setContentTitle(mContentTitle)
            .setContentText(mContentText)
            .setLights(Color.GREEN, 2000, 1000)
            .setPriority(NotificationCompat.PRIORITY_MAX)
            .setStyle(new NotificationCompat.BigTextStyle().bigText(mContentText))
            .setContentIntent(contentIntent)
            .setAutoCancel(true);
    createNotificationChannel(context);

    NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context);
    notificationManager.notify(uniqueID, notification.build());

}

private void createNotificationChannel(Context context) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        CharSequence name = "ChannelName";
        String description = "FoodExpiration";
        int importance = NotificationManager.IMPORTANCE_DEFAULT;
        NotificationChannel channel = new NotificationChannel("Default", name, importance);
        channel.setDescription(description);
        NotificationManager notificationManager = context.getSystemService(NotificationManager.class);
        if (notificationManager != null) {
            notificationManager.createNotificationChannel(channel);
        }
    }
}

}

Теперь, если пользователь решит удалить уведомлениеКак бы я пошел делать это.Должен ли я изменить весь свой код в настройках уведомлений?Любая помощь приветствуется, спасибо.

1 Ответ

0 голосов
/ 16 мая 2018

Вы можете удалить каналы уведомлений, позвонив по номеру deleteNotificationChannel().

NotificationManager mNotificationManager =
        (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// The id of the channel.
String id = "channel_01";
mNotificationManager.deleteNotificationChannel(id);

Ссылка: Создание и управление каналом уведомлений

...