Android 为什么取消通知时不调用deleteIntent(PendingIntent)?
为什么取消通知时不调用DeleteIntentPendingContent 我在“额外挑战”中使用deleteentent编写android教程 但是,它根本不会被调用。在API 27上的模拟器中运行 当我滑动通知以取消时,不会调用cancelNotification方法 在文档中,我看到了水印,但它不在文本中 不确定它是否真的被弃用了,或者我是否错误地使用了deleteIntent 在NotifyMe应用程序中,有一个用例,其中 按钮与应用程序的状态不匹配:当用户解除应用程序时 通过刷走或清除整个通知来发送通知 抽屉在这种情况下,你的应用程序无法知道 通知已取消,必须更改按钮状态 创建另一个挂起的意图,让应用程序知道用户已 取消通知,并相应地切换按钮状态 提示:请检查NotificationCompat.Builder类,以获取以下方法: 如果用户拒绝通知,则传递意图 更新:从下面Commonware的有用答案中,我更正了接收器的注册,以便对同一个IntentFilter使用多个操作。然而,即使我尝试了悬挂帐篷的所有不同标志,它仍然失败。 当您按下通知中的更新按钮,然后向右滑动时,“活动”中的按钮不会重置其状态,因为PendingEvent未启动 这是我的更新代码Android 为什么取消通知时不调用deleteIntent(PendingIntent)?,android,Android,为什么取消通知时不调用DeleteIntentPendingContent 我在“额外挑战”中使用deleteentent编写android教程 但是,它根本不会被调用。在API 27上的模拟器中运行 当我滑动通知以取消时,不会调用cancelNotification方法 在文档中,我看到了水印,但它不在文本中 不确定它是否真的被弃用了,或者我是否错误地使用了deleteIntent 在NotifyMe应用程序中,有一个用例,其中 按钮与应用程序的状态不匹配:当用户解除应用程序时 通过刷走或清除
package com.onedropaflame.notifyme;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.NotificationCompat;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
private static final String PRIMARY_CHANNEL_ID = "primary_notification_channel";
private NotificationManager mNotifyManager;
private static final int NOTIFICATION_ID = 0;
private static final String ACTION_UPDATE_NOTIFICATION =
"com.example.android.notifyme.ACTION_UPDATE_NOTIFICATION";
private static final String ACTION_CANCEL_NOTIFICATION =
"com.example.android.notifyme.ACTION_CANCEL_NOTIFICATION";
private NotificationReceiver mReceiver = new NotificationReceiver();
public class NotificationReceiver extends BroadcastReceiver {
public NotificationReceiver() {
}
@Override
public void onReceive(Context context, Intent intent) {
switch (intent.getAction()) {
case ACTION_UPDATE_NOTIFICATION:
updateNotification();;
break;
case ACTION_CANCEL_NOTIFICATION:
cancelNotification();
break;
}
}
}
public void createNotificationChannel() {
mNotifyManager = (NotificationManager)
getSystemService(NOTIFICATION_SERVICE);
if (android.os.Build.VERSION.SDK_INT >=
android.os.Build.VERSION_CODES.O) {
// Create a NotificationChannel
NotificationChannel notificationChannel = new NotificationChannel(PRIMARY_CHANNEL_ID,
"Mascot Notification", NotificationManager
.IMPORTANCE_HIGH);
notificationChannel.enableLights(true);
notificationChannel.setLightColor(Color.RED);
notificationChannel.enableVibration(true);
notificationChannel.setDescription("Notification from Mascot");
mNotifyManager.createNotificationChannel(notificationChannel);
}
}
private Button button_notify;
private Button button_cancel;
private Button button_update;
public void sendNotification() {
Intent updateIntent = new Intent(ACTION_UPDATE_NOTIFICATION);
PendingIntent updatePendingIntent = PendingIntent.getBroadcast
(this, NOTIFICATION_ID, updateIntent, PendingIntent.FLAG_ONE_SHOT);
Intent cancelIntent = new Intent(ACTION_CANCEL_NOTIFICATION);
PendingIntent cancelPendingIntent = PendingIntent.getBroadcast
(this, NOTIFICATION_ID, cancelIntent, PendingIntent.FLAG_ONE_SHOT);
NotificationCompat.Builder notifyBuilder = getNotificationBuilder();
notifyBuilder.addAction(R.drawable.ic_update, "Update Notification", updatePendingIntent);
notifyBuilder.setDeleteIntent(cancelPendingIntent);
mNotifyManager.notify(NOTIFICATION_ID, notifyBuilder.build());
setNotificationButtonState(false, true, true);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button_notify = findViewById(R.id.notify);
button_notify.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
sendNotification();
}
});
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(ACTION_CANCEL_NOTIFICATION);
intentFilter.addAction(ACTION_UPDATE_NOTIFICATION);
createNotificationChannel();
registerReceiver(mReceiver,intentFilter);
button_update = findViewById(R.id.update);
button_update.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//Update the notification
updateNotification();
}
});
button_cancel = findViewById(R.id.cancel);
button_cancel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//Cancel the notification
cancelNotification();
}
});
setNotificationButtonState(true, false, false);
}
@Override
protected void onDestroy() {
unregisterReceiver(mReceiver);
super.onDestroy();
}
public void updateNotification() {
Bitmap androidImage = BitmapFactory
.decodeResource(getResources(),R.drawable.mascot_1);
NotificationCompat.Builder notifyBuilder = getNotificationBuilder();
notifyBuilder.setStyle(new NotificationCompat.BigPictureStyle()
.bigPicture(androidImage)
.setBigContentTitle("Notification Updated!"));
mNotifyManager.notify(NOTIFICATION_ID, notifyBuilder.build());
setNotificationButtonState(false, false, true);
}
public void cancelNotification() {
mNotifyManager.cancel(NOTIFICATION_ID);
setNotificationButtonState(true, false, false);
}
private NotificationCompat.Builder getNotificationBuilder(){
Intent notificationIntent = new Intent(this, MainActivity.class);
PendingIntent notificationPendingIntent = PendingIntent.getActivity(this,
NOTIFICATION_ID, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder notifyBuilder = new NotificationCompat.Builder(this, PRIMARY_CHANNEL_ID)
.setContentTitle("You've been notified!")
.setContentText("This is your notification text.")
.setSmallIcon(R.drawable.ic_android)
.setContentIntent(notificationPendingIntent)
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setDefaults(NotificationCompat.DEFAULT_ALL)
.setAutoCancel(true);
return notifyBuilder;
}
void setNotificationButtonState(Boolean isNotifyEnabled,
Boolean isUpdateEnabled,
Boolean isCancelEnabled) {
button_notify.setEnabled(isNotifyEnabled);
button_update.setEnabled(isUpdateEnabled);
button_cancel.setEnabled(isCancelEnabled);
}
}
看起来好像您没有注册操作取消通知的接收者,只是操作更新通知。看起来好像您没有注册操作取消通知的接收者,只是操作更新通知。Commonware发布了两个需要注册的操作的正确答案。 然而,这还不够。我更正了接收器的注册,以便对同一个IntentFilter使用多个操作。然而,即使我尝试了悬挂帐篷的所有不同标志,它仍然失败。当您按下通知中的更新按钮,然后向右滑动时,“活动”中的按钮不会重置其状态,因为PendingEvent未启动 解决方案:我发现我必须在updateNotification期间再次设置CancelPendingEvent。我不知道它丢失的原因
public void updateNotification() {
Bitmap androidImage = BitmapFactory
.decodeResource(getResources(),R.drawable.mascot_1);
NotificationCompat.Builder notifyBuilder = getNotificationBuilder();
notifyBuilder.setStyle(new NotificationCompat.BigPictureStyle()
.bigPicture(androidImage)
.setBigContentTitle("Notification Updated!"));
// >>>>> SET AGAIN! >>>>>>>>>
Intent cancelIntent = new Intent(ACTION_CANCEL_NOTIFICATION);
PendingIntent cancelPendingIntent = PendingIntent.getBroadcast
(this, NOTIFICATION_ID, cancelIntent, PendingIntent.FLAG_ONE_SHOT);
notifyBuilder.setDeleteIntent(cancelPendingIntent);
// >>>>>>>>>>>>>>
mNotifyManager.notify(NOTIFICATION_ID, notifyBuilder.build());
setNotificationButtonState(false, false, true);
}
Commonware发布了两个需要注册的操作的正确答案。 然而,这还不够。我更正了接收器的注册,以便对同一个IntentFilter使用多个操作。然而,即使我尝试了悬挂帐篷的所有不同标志,它仍然失败。当您按下通知中的更新按钮,然后向右滑动时,“活动”中的按钮不会重置其状态,因为PendingEvent未启动 解决方案:我发现我必须在updateNotification期间再次设置CancelPendingEvent。我不知道它丢失的原因
public void updateNotification() {
Bitmap androidImage = BitmapFactory
.decodeResource(getResources(),R.drawable.mascot_1);
NotificationCompat.Builder notifyBuilder = getNotificationBuilder();
notifyBuilder.setStyle(new NotificationCompat.BigPictureStyle()
.bigPicture(androidImage)
.setBigContentTitle("Notification Updated!"));
// >>>>> SET AGAIN! >>>>>>>>>
Intent cancelIntent = new Intent(ACTION_CANCEL_NOTIFICATION);
PendingIntent cancelPendingIntent = PendingIntent.getBroadcast
(this, NOTIFICATION_ID, cancelIntent, PendingIntent.FLAG_ONE_SHOT);
notifyBuilder.setDeleteIntent(cancelPendingIntent);
// >>>>>>>>>>>>>>
mNotifyManager.notify(NOTIFICATION_ID, notifyBuilder.build());
setNotificationButtonState(false, false, true);
}
非常感谢。这是个错误,但并没有完全解决。单击任一更新按钮后,它会更新通知,但现在滑动通知会将其删除,但不再触发PendingEvent以调用cancelNotification并重置按钮状态。以下是我注册取消的方式:`registerReceivermReceiver,new IntentFilterACTION\u cancel\u通知`@likejudo:那不行。要么有两个单独的BroadcastReceiver对象,要么有一个标识这两个操作的IntentFilter。将接收者视为保存在HashMap中,由接收者对象设置关键帧。你用同一把钥匙做了两次推杆。这也不起作用IntentFilter IntentFilter=新的IntentFilter;intentFilter.addActionACTION\u取消\u通知;intentFilter.addActionACTION\u更新\u通知;registerReceiver,intentFilter;`我甚至试着改为“当前”和“否”,我把所有的代码都放在了更新后的问题中。我更正了接收器的注册,以便对同一个IntentFilter使用多个操作。然而,即使我尝试了悬挂帐篷的所有不同标志,它仍然失败。当您按下通知中的更新按钮,然后向右滑动时,“活动”中的按钮不会重置其状态,因为PendingEvent未启动。谢谢。这是个错误,但并没有完全解决。单击任一更新按钮后,它会更新通知,但现在滑动通知会将其删除,但不再触发PendingEvent以调用cancelNotification并重置按钮状态。以下是我注册取消的方式:`registerReceivermReceiver,new IntentFilterACTION\u cancel\u通知`@likejudo:那不行。要么有两个单独的BroadcastReceiver对象,要么有一个标识b的IntentFilter
其他行动。将接收者视为保存在HashMap中,由接收者对象设置关键帧。你用同一把钥匙做了两次推杆。这也不起作用IntentFilter IntentFilter=新的IntentFilter;intentFilter.addActionACTION\u取消\u通知;intentFilter.addActionACTION\u更新\u通知;registerReceiver,intentFilter;`我甚至试着改为“当前”和“否”,我把所有的代码都放在了更新后的问题中。我更正了接收器的注册,以便对同一个IntentFilter使用多个操作。然而,即使我尝试了悬挂帐篷的所有不同标志,它仍然失败。当您按下通知中的更新按钮,然后向右滑动时,“活动”中的按钮不会重置其状态,因为PendingEvent未启动。