You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android 6.0应用通知权限申请与启动通知功能失效求助

修复Android应用启动时通知权限请求及欢迎通知问题

问题原因

  1. 未主动发起权限请求:当前代码仅检查权限状态,若未授予权限直接return,没有调用权限请求API触发系统权限弹窗。
  2. 版本适配缺失:POST_NOTIFICATIONS是Android 13(API 33)才新增的运行时权限,Android 6.0(API 23)至Android 12(API 32)区间内,通知权限默认授予,无需运行时请求。

修复方案

关键修改点

  • 增加版本判断,仅在API 33及以上版本请求POST_NOTIFICATIONS权限
  • 调用ActivityCompat.requestPermissions()主动发起权限请求
  • 重写onRequestPermissionsResult()方法,处理用户权限操作后的通知发送逻辑
  • 封装通知发送逻辑,避免代码重复

修改后的完整代码

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.app.NotificationCompat;
import androidx.core.app.NotificationManagerCompat;

import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;

public class gamer_login_page extends AppCompatActivity {
    private static final int NOTIFICATION_PERMISSION_REQUEST_CODE = 1001;

    private void createNotificationChannel() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            CharSequence name = "login channel";
            String description = "channel for bootup notifications";
            int importance = NotificationManager.IMPORTANCE_DEFAULT;
            NotificationChannel channel = new NotificationChannel("muffet", name, importance);
            channel.setDescription(description);
            NotificationManager notificationManager = getSystemService(NotificationManager.class);
            notificationManager.createNotificationChannel(channel);
        }
    }

    private void sendWelcomeNotification() {
        NotificationCompat.Builder builder = new NotificationCompat.Builder(this, "muffet")
                .setSmallIcon(R.drawable.hypnos)
                .setContentTitle("Welcome")
                .setContentText("You have booted up Forked Decisions Alpha. I hope your time here is satisfactory!")
                .setPriority(NotificationCompat.PRIORITY_DEFAULT);

        Intent intent = new Intent(this, gamer_login_page.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_IMMUTABLE);

        builder.setContentIntent(pendingIntent);

        NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);

        if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.POST_NOTIFICATIONS) == PackageManager.PERMISSION_GRANTED) {
            notificationManager.notify(1, builder.build());
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_gamer_login_page);

        createNotificationChannel();

        // 仅在Android 13及以上版本请求通知权限
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
            if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.POST_NOTIFICATIONS) != PackageManager.PERMISSION_GRANTED) {
                ActivityCompat.requestPermissions(
                        this,
                        new String[]{android.Manifest.permission.POST_NOTIFICATIONS},
                        NOTIFICATION_PERMISSION_REQUEST_CODE
                );
                return;
            }
        }

        // 权限已授予,直接发送通知
        sendWelcomeNotification();
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (requestCode == NOTIFICATION_PERMISSION_REQUEST_CODE) {
            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                // 用户授予权限后发送欢迎通知
                sendWelcomeNotification();
            }
            // 可在此添加用户拒绝权限后的提示逻辑
        }
    }
}

额外说明

  • Android 6.0至12版本中,通知权限默认开启,应用启动后会直接发送欢迎通知
  • 若用户拒绝权限,可在onRequestPermissionsResult中添加引导用户手动开启权限的逻辑

内容的提问来源于stack exchange,提问作者Shoba

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.12 02:44:50