[英]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.