android - 通知不会在 Android 10 中取消

标签 android notifications broadcastreceiver android-notifications android-10.0

问题:当我直接回复那个通知时,我想取消一个通知。它适用于 Android N,但不适用于 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,但同样的问题发生了。

我附上了我想要实现的目标的屏幕截图。它在下面。

enter image description here

2020 年 4 月 29 日更新

我在 ANDROID 8 和 ANDROID 9 模拟器上运行同一个项目。它在 ANDROID 8 上按方面工作,但在 ANDROID 9 上出现相同问题(回复后不取消通知)。

我找到了相同的 question here.

最佳答案

我会建议你做接下来的事情:

  1. 检查您的服务是否调用了 onHandleIntent 方法
  2. 检查remoteInput是否不为空
  3. 检查notificationManager是否不为空
  4. 尝试取消不带标签的ID通知

关于android - 通知不会在 Android 10 中取消,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61478154/

相关文章:

Android PendingIntent 将您带到一个已经存在的 Activity ?

Android 通知,带有按钮单击删除通知

android - 如何在 BroadcastReceiver 中知道 App 是否在前台运行?

java - 将裁剪后的照片保存到 FirebaseStorage 时应用崩溃

android - 如何在我们自己的 Android 应用程序上使用 KML 绘制叠加层?

java - 从 pubnub Android 检索数据

Java 检索返回的 ArrayList 到 HashMap

amazon-web-services - SNS 中的订阅已存在,并且需要使用 1 个使用 cloudformation 的订阅

android - 无法接收网络相关事件

android - 无法注册 BroadcastReceiver