无论我尝试什么

发布于 2025-01-21 11:45:43 字数 1957 浏览 0 评论 0原文

老实说,我开始相信标题中报道的问题是在商店中发布的新Android API的问题。我尝试在清单中设置Android:lainingMode =“ singletask”,以下是我的构建和显示通知的代码。我进行了调整和调整,但是每当按下通知时,应用程序都会重新启动。

//其他信息: Android Studio:2021.3 模拟:像素4 XL API 30 SDK:Android 11 还在物理设备上测试:true 缓存无效:true

//代码

private void showNotification(int notificationID, String title, String message, String channel_id, String channelName, String channelDescription, int notifIcon)
    {
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
            int importance = NotificationManager.IMPORTANCE_HIGH;
            NotificationChannel mChannel = new NotificationChannel(channel_id, channelName, importance);
            mChannel.setDescription(channelDescription);
            mChannel.setLightColor(Color.RED);
            mChannel.enableVibration(true);
            mChannel.enableLights(true);
            mChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
            mChannel.setShowBadge(false);
            mChannel.setLightColor(Color.RED);
            notificationManager.createNotificationChannel(mChannel);
        }

        NotificationCompat.Builder builder = new NotificationCompat.Builder(this, channel_id).setSmallIcon(notifIcon).setContentTitle(title).setContentText(message);

        Intent resultIntent = new Intent(this, MainActivity.class);
        resultIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
        TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
        stackBuilder.addParentStack(MainActivity.class);
        stackBuilder.addNextIntent(resultIntent);
        PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
        builder.setContentIntent(resultPendingIntent);
        builder.setOngoing(true);//s that it can't just be swiped away!
        //
        notificationManager.notify(notificationID, builder.build());
    }

I am honestly starting to believe the issue reported in the title is an issue with the new Android API required to publish on the store. I have tried setting android:launchMode="singleTask" in the manifest, and below is my code to build and show the notification. I have tweaked and tweaked but whenever the notification is pressed, the app restarts.

//other info:
Android Studio: 2021.3
Emulated: Pixel 4 XL API 30
SDK: Android 11
Also tested on physical device: true
Cache invalidated: true

//CODE

private void showNotification(int notificationID, String title, String message, String channel_id, String channelName, String channelDescription, int notifIcon)
    {
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
            int importance = NotificationManager.IMPORTANCE_HIGH;
            NotificationChannel mChannel = new NotificationChannel(channel_id, channelName, importance);
            mChannel.setDescription(channelDescription);
            mChannel.setLightColor(Color.RED);
            mChannel.enableVibration(true);
            mChannel.enableLights(true);
            mChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
            mChannel.setShowBadge(false);
            mChannel.setLightColor(Color.RED);
            notificationManager.createNotificationChannel(mChannel);
        }

        NotificationCompat.Builder builder = new NotificationCompat.Builder(this, channel_id).setSmallIcon(notifIcon).setContentTitle(title).setContentText(message);

        Intent resultIntent = new Intent(this, MainActivity.class);
        resultIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
        TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
        stackBuilder.addParentStack(MainActivity.class);
        stackBuilder.addNextIntent(resultIntent);
        PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
        builder.setContentIntent(resultPendingIntent);
        builder.setOngoing(true);//s that it can't just be swiped away!
        //
        notificationManager.notify(notificationID, builder.build());
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文