简体   繁体   中英

Setting some notifications with AlarmManager and cancelling one of them

in my program i set notifications with a string and its clock to get a notification with that string at its clock.(I mean user wants to get a notification with a string at demanding time, and user gave me a lot of strings and their clocks.) And if the user want to cancel that notification i don't know how to do it.

I tried alarmManager.cancel(pendingIntent) but I want to cancel the only one notification not all of them. Also, it doesn't cancel any notification too.

here is the code in the main

public void gecikmeliGoster(String plan, int yil, int ay, int gun, String saat){

        NotificationCompat.Builder builder;

        NotificationManager bildirimYoneticisi = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

        Intent ıntent = new Intent(HatirlaticiKur.this, AlarmReciever.class);

        PendingIntent gidilecekIntent = PendingIntent.getActivity(this, 1, ıntent, PendingIntent.FLAG_UPDATE_CURRENT);


        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            String kanalId = "kanalId";
            String kanalAd = "kanalAd";
            String kanalTanım = "kanalTanım";
            int kanalOnceligi = NotificationManager.IMPORTANCE_HIGH;

            NotificationChannel kanal = bildirimYoneticisi.getNotificationChannel(kanalId);

            if (kanal == null) {
                kanal = new NotificationChannel(kanalId, kanalAd, kanalOnceligi);
                kanal.setDescription(kanalTanım);
                bildirimYoneticisi.createNotificationChannel(kanal);
            }

            builder = new NotificationCompat.Builder(this, kanalId);

            builder.setContentTitle("Hatırlatıcı")
                    .setContentText(plan)
                    .setSmallIcon(R.drawable.hatirlatici)
                    .setAutoCancel(true)
                    .setContentIntent(gidilecekIntent);
        } else {

            builder = new NotificationCompat.Builder(this);

            builder.setContentTitle("Hatırlatıcı")
                    .setContentText(plan)
                    .setSmallIcon(R.drawable.hatirlatici)
                    .setContentIntent(gidilecekIntent)
                    .setAutoCancel(true)
                    .setPriority(Notification.PRIORITY_HIGH);
        }
        Intent broadcastIntent = new Intent(HatirlaticiKur.this, AlarmReciever.class);

        broadcastIntent.putExtra("nesne", builder.build());

        PendingIntent gidilecekBroadcast = PendingIntent.getBroadcast(this, 0, broadcastIntent, PendingIntent.FLAG_UPDATE_CURRENT);

        //long gecikme = SystemClock.elapsedRealtime() + 5000; //bu 5 saniyede bir bildirim gelmesi için falan
        String[] noktasiz = saat.split(":");
        int akrep = Integer.parseInt(noktasiz[0]);
        int yelkovan = Integer.parseInt(noktasiz[1]);

        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(System.currentTimeMillis());
        calendar.set(Calendar.YEAR, yil);
        calendar.set(Calendar.MONTH, ay);
        calendar.set(Calendar.DATE, gun);
        calendar.set(Calendar.HOUR_OF_DAY, akrep);
        calendar.set(Calendar.MINUTE, yelkovan);
        calendar.set(Calendar.SECOND, 0);

        AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);

        alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 0, gidilecekBroadcast);

}

and here is the code in AlarmReceiver

public class AlarmReciever extends BroadcastReceiver {

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

    NotificationManager bildirimYoneticisi = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

    Notification bildirim = intent.getParcelableExtra("nesne");

    bildirimYoneticisi.notify(1,bildirim);

}



}

I want to cancel demanding notifications, i will appreciate your help

use used 0 id for setting alarm

 PendingIntent gidilecekBroadcast = PendingIntent.getBroadcast(this, 0, broadcastIntent, PendingIntent.FLAG_UPDATE_CURRENT);

You need uniqueid for all notification set using alarmmanager then based on uniqueid you can cancel alarm

PendingIntent gidilecekBroadcast  = 
PendingIntent.getBroadcast(getApplicationContext(), uniqueid , broadcastIntent, 
PendingIntent.FLAG_UPDATE_CURRENT)
alarmManager.cancel(gidilecekBroadcast);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM