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 11:02:51 浏览: 266
可能是因为你没有在BroadcastReceiver中正确地处理ACTION_DELETE_NOTIFICATION这个action。请确认你的BroadcastReceiver中能够正确地接收并处理这个action,例如:
```
public class MyBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (action != null && action.equals(ACTION_DELETE_NOTIFICATION)) {
// 在这里处理删除通知的逻辑
// ...
}
}
}
```
如果你已经正确地处理了ACTION_DELETE_NOTIFICATION这个action,但仍然无法收到通知删除的事件,请提供更多的代码和详细的错误信息,以便我们更好地帮助你解决问题。
相关问题
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); NotificationCompat.Builder builder = 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); Notification build = builder.build(); NotificationManagerCompat.from(this).notify(CHANNEL_CHARGING_NOTIFICATION_ID, build); } 报错说 No Channel found for pkg=com.example.myapplication 如何解决
这个错误提示是因为在创建通知时使用了不存在的通知渠道,需要先创建一个通知渠道,然后将其与通知相关联。可以按照以下步骤解决:
1. 在应用的创建时,创建通知渠道,代码如下:
```java
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel("com.example.myapplication", "MyApp", NotificationManager.IMPORTANCE_HIGH);
NotificationManager notificationManager = getSystemService(NotificationManager.class);
notificationManager.createNotificationChannel(channel);
}
```
2. 在创建通知时,指定通知渠道,代码如下:
```java
NotificationCompat.Builder builder = 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);
Notification build = builder.build();
NotificationManagerCompat.from(this).notify(CHANNEL_CHARGING_NOTIFICATION_ID, build);
```
这样就可以解决该错误。
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
可能是因为您没有在广播接收器中注册 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)) {
// 处理删除通知的操作
}
}
```
阅读全文