简体   繁体   English

设置NotificationCompat.Builder的Java android的ID

[英]Setting the ID of NotificationCompat.Builder java android

I am currently creating an alert with the following code: 我目前正在使用以下代码创建警报:

public NotificationCompat.Builder getChannelNotification(AbstractEvent event) {
    Intent activityIntent = new Intent(context, MainActivity.class);
    Intent broadcastIntent = new Intent(context, NotificationReceiver.class);
    broadcastIntent.putExtra("id",event.getId());

    PendingIntent actionIntent = PendingIntent.getBroadcast(context, 0, broadcastIntent, PendingIntent.FLAG_UPDATE_CURRENT);

    return new NotificationCompat.Builder(getApplicationContext(), channelID)
            .setContentTitle(event.getTitle())
            .setContentText("event begins in: " + event.getStartTime)
            .setSmallIcon(R.drawable.android)
            .setShortcutId("1")
            .addAction(R.mipmap.ic_launcher, "Dismiss", actionIntent)
}

This created the alert fine, but I am atempting to then dismiss the alert when I press the "Dismiss" button. 这创建了警报罚款,但是当我按下“关闭”按钮时,我正试图解除警报。

I am trying to do this in another class by using: 我试图通过使用以下方法在另一个类中执行此操作:

NotificationManager manager = context.getSystemService(NotificationManager.class);
manager.cancel(id);

This would be fine, However I cannot seem to set the ID of the alarm I have just made. 这样会很好,但我似乎无法设置我刚刚制作的闹钟的ID。

How can I set/access the ID of the alarm I have just made? 如何设置/访问我刚刚制作的闹钟的ID?

you have to create the id manually and set it with the notification and store it in a sharedPreferences 您必须手动创建id并使用通知进行设置并将其存储在sharedPreferences

NOTIFICATION_ID = notificationPreferences.getInt("notifnumber",0);

as soon as you notify , you increment the value and store it 一旦通知,您将增加值并存储它

manager.notify(NOTIFICATION_ID, notification);
editor.putInt("notifnumber", NOTIFICATION_ID + 1);
editor.commit();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Android NoClassDefFoundError NotificationCompat.Builder - Android NoClassDefFoundError NotificationCompat.Builder NotificationCompat.Builder() 不接受 Channel Id 作为参数 - NotificationCompat.Builder() not accepting Channel Id as argument Android NotificationCompat.Builder addPerson示例 - Android NotificationCompat.Builder addPerson example NotificationCompat.Builder是否与所有Android手机都不兼容? - Is NotificationCompat.Builder not compatible for all Android mobiles? NotificationCompat.Builder 在 Android O 中已弃用 - NotificationCompat.Builder deprecated in Android O NotificationCompat.Builder - NotificationCompat.Builder 如何使其工作NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this,NOTIFICATION_CHANNEL_ID); - How to make it work NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID); NotificationCompat.Builder中的NoClassDefFound - NoClassDefFound in NotificationCompat.Builder NotificationCompat.Builder 已弃用 - NotificationCompat.Builder is deprecated Android:如何在通知范围内调整NotificationCompat.Builder的largeIcon - Android: How to adjust the largeIcon of a NotificationCompat.Builder in the bounds of the notification
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM