public void Create(){ Log.d("limf", "创建通知"); Intent deleteIntent = new Intent(this, MainActivity.class); deleteIntent.setAction(ACTION_DELETE_NOTIFICATION); PendingIntent pendingDeleteIntent = PendingIntent.getBroadcast( this, 0, deleteIntent, PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { // Create the NotificationChannel CharSequence name = "My App Notification Channel"; String description = "My App Notifications"; int importance = NotificationManager.IMPORTANCE_DEFAULT; NotificationChannel channel = new NotificationChannel("com.example.myapplication", name, importance); channel.setDescription(description); // Register the channel with the system; you can't change the importance // or other notification behaviors after this NotificationManager notificationManager = getSystemService(NotificationManager.class); notificationManager.createNotificationChannel(channel); } Notification notification = new NotificationCompat.Builder(this, "com.example.myapplication") .setSmallIcon(R.drawable.ic_launcher_background) .setContentTitle("My notification") .setContentText("Hello World!!!!") .addAction(R.drawable.ic_launcher_foreground, "Delete", pendingDeleteIntent) .build(); NotificationManagerCompat.from(this).notify(CHANNEL_CHARGING_NOTIFICATION_ID, notification); } 为什么收不到Action
时间: 2024-01-12 21:02:51 浏览: 19
可能是因为您没有在广播接收器中注册 ACTION_DELETE_NOTIFICATION 操作。您需要在广播接收器的 IntentFilter 中添加 ACTION_DELETE_NOTIFICATION 操作来捕获该操作。例如:
```
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(ACTION_DELETE_NOTIFICATION);
registerReceiver(yourBroadcastReceiver, intentFilter);
```
另外,您还需要确保在 MainActivity 中处理 ACTION_DELETE_NOTIFICATION 操作。您可以在 MainActivity 的 onNewIntent() 方法中处理该操作。例如:
```
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
if (intent.getAction() != null && intent.getAction().equals(ACTION_DELETE_NOTIFICATION)) {
// 处理删除通知的操作
}
}
```