Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/193.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
设置NotificationCompat.Builder java android的ID_Android_Alarm - Fatal编程技术网

设置NotificationCompat.Builder java android的ID

设置NotificationCompat.Builder java android的ID,android,alarm,Android,Alarm,我当前正在使用以下代码创建警报: public NotificationCompat.Builder getChannelNotification(AbstractEvent event) { Intent activityIntent = new Intent(context, MainActivity.class); Intent broadcastIntent = new Intent(context, NotificationReceiver.class); br

我当前正在使用以下代码创建警报:

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)
}
这创建了警报,但我试图在按下“解除”按钮时解除警报

我试图在另一节课上使用以下方法来完成此操作:

NotificationManager manager = context.getSystemService(NotificationManager.class);
manager.cancel(id);
这将是好的,但我似乎无法设置我刚才发出的警报的ID


如何设置/访问我刚刚发出的警报的ID?

您必须手动创建ID,并使用通知进行设置,并将其存储在
共享参考文件中

NOTIFICATION_ID = notificationPreferences.getInt("notifnumber",0);
一旦您发出通知,就会增加值并存储它

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

您必须手动创建id,并将其与通知一起设置,然后将其存储在
SharedReferences

NOTIFICATION_ID = notificationPreferences.getInt("notifnumber",0);
一旦您发出通知,就会增加值并存储它

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

单击notification时要清除notificationbar上的通知单击notification时要清除notificationbar上的通知创建通知的位置(类、活动等)创建通知的位置(类、活动等)