通知不会在Android 10中被取消 [英] Notification does not get cancelled in Android 10
问题描述
问题:当我直接回复该通知时,我想取消该通知.它可以在Android N上运行,但不能在Android 10上运行.
Problem: I want to cancel a Notification when I directly reply to that Notification. It works in Android N but doesn't work on Android 10.
我的代码如下:
MainActivity.java
public class MainActivity extends AppCompatActivity {
public static final int NOTIFICATION_ID = 1256;
public static final String CHANNEL_1_ID = "channel1";
private Button btnDisplayNotification;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
createNotificationChannel();
btnDisplayNotification = findViewById(R.id.btnDisplayNotification);
btnDisplayNotification.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
displayNotification(MainActivity.this);
}
});
}
private void createNotificationChannel() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel1 = new NotificationChannel(
CHANNEL_1_ID,
"Channel 1",
NotificationManager.IMPORTANCE_HIGH
);
channel1.setDescription("This is Channel 1");
NotificationManager manager = getSystemService(NotificationManager.class);
manager.createNotificationChannel(channel1);
}
}
public static void displayNotification(Context context) {
Intent replyIntent;
PendingIntent replyPendingIntent = null;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
replyIntent = new Intent(context, ReceiverIntentService.class);
replyPendingIntent = PendingIntent.getService(context, 0, replyIntent, PendingIntent.FLAG_CANCEL_CURRENT);
} else {
replyIntent = new Intent(context, ReplyActivity.class);
replyIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
replyPendingIntent = PendingIntent.getActivity(context, 0, replyIntent, PendingIntent.FLAG_CANCEL_CURRENT);
}
RemoteInput remoteInput = new RemoteInput.Builder("key_text_reply")
.setLabel("Your answer...")
.build();
NotificationCompat.Action replyAction = new NotificationCompat.Action.Builder(
R.drawable.ic_reply,
"Reply", replyPendingIntent)
.addRemoteInput(remoteInput)
.build();
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(context, CHANNEL_1_ID);
notificationBuilder.setSmallIcon(R.drawable.ic_launcher_background)
.addAction(replyAction)
.setContentTitle("Hot Jobs")
.setContentText("Check out hot jobs based on your skills")
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setAutoCancel(true);
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context);
notificationManager.notify("NOTI_TAG", NOTIFICATION_ID, notificationBuilder.build());
}
}
ReceiverIntentService.java
public class ReceiverIntentService extends IntentService {
public ReceiverIntentService() {
super("blah");
}
@Override
protected void onHandleIntent(@Nullable Intent intent) {
Bundle remoteInput = RemoteInput.getResultsFromIntent(intent);
if (remoteInput != null) {
CharSequence replyText = remoteInput.getCharSequence("key_text_reply");
Log.e("NotiReply", "IS Reply is: " + replyText);
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(getApplicationContext());
if (notificationManager != null) {
stopForeground( true );
notificationManager.cancel("NOTI_TAG", NOTIFICATION_ID);
}
}
}
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="web.b.notificationreplydemo2">
<application
android:allowBackup="true"
android:icon="@drawable/ic_notifications_black_24dp"
android:label="@string/app_name"
android:roundIcon="@drawable/ic_notifications_black_24dp"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".ReplyActivity"></activity>
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name=".DirectReplyReceiver" />
<service
android:name=".ReceiverIntentService"
android:exported="false" />
</application>
</manifest>
在上面的代码中,我尝试使用 BroadcastReceiver ,但是发生了同样的问题.
In the above code, I have tried using BroadcastReceiver, but the same ISSUE happens.
我正在附上我要实现的屏幕截图.在下面.
I'm attaching a screenshot of what I want to achieve. It's below.
2020年4月29日更新
我在ANDROID 8和ANDROID 9模拟器上运行相同的项目.它可以在ANDROID 8上使用,但在ANDROID 9上可以使用相同的问题(回复后不会取消通知).
I run the same project on ANDROID 8 and ANDROID 9 emulators. It worked as aspected on ANDROID 8 but the same issue on ANDROID 9(Does not cancel notification after replying).
我在这里找到了相同的问题.
I found the same question here.
推荐答案
我建议您做以下事情:
- 检查您的服务的onHandleIntent方法是否被调用
- 检查
remoteInput
是否不为空 - 检查
notificationManager
是否不为空 - 尝试通过不带标签的ID取消通知
- check if your services onHandleIntent method is called
- check if
remoteInput
is not null - check if
notificationManager
is not null - try to cancel notification by ID without a tag
这篇关于通知不会在Android 10中被取消的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!