Android 10中通知无法被取消。

4
问题:我想在直接回复通知时取消通知。在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.
2个回答

1

我建议你做以下事情:

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

哈哈...太有趣了!不幸的是,我尝试了你建议的所有方法。 - Maulik Dodia

0
....
int notiId = x;
String notiTag = x;
....

// cancel notification
NotificationManager.cancel(notiId);
NotificationManager.cancel(notiTag, notiId);

回答需要支持信息 您的回答可以通过提供更多的支持信息来改进。请[编辑]以添加进一步的细节,例如引用或文档,以便他人可以确认您的回答是否正确。您可以在帮助中心找到关于如何撰写良好回答的更多信息。 - moken

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接