簡體   English   中英

使用AlarmManager設置一些通知並取消其中之一

[英]Setting some notifications with AlarmManager and cancelling one of them

在我的程序中,我設置了一個帶有字符串及其時鍾的通知,以在其時鍾處獲得帶有該字符串的通知。(我的意思是用戶希望在要求的時間獲得帶有字符串的通知,並且用戶給了我很多字符串和他們的時鍾。)如果用戶想取消該通知,我不知道該怎么做。

我嘗試使用alarmManager.cancel(pendingIntent),但我想取消僅一個通知,而不是全部取消。 另外,它也不會取消任何通知。

這是主要的代碼

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);

}

這是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);

}



}

我想取消要求苛刻的通知,感謝您的幫助

使用已使用的0 ID設置警報

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

您需要使用alarmmanager為所有通知集設置唯一標識,然后根據唯一標識可以取消警報

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM