通知和请求权限功能不适用于 Android Studio Android 6.0

问题描述 投票:0回答:1

我正在 Android Studio [Android 6.0] 中使用 Java 构建一个应用程序。我希望用户允许权限并收到启动应用程序的通知,但我当前的代码不起作用。如下图:

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 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);
        }
    }

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

//-------------notify the user that they have entered the app
        createNotificationChannel();

        NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
                .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);

// notificationId is a unique int for each notification that you must define
        if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.POST_NOTIFICATIONS) != PackageManager.PERMISSION_GRANTED) {
            // TODO: Consider calling
            //    ActivityCompat#requestPermissions
            // here to request the missing permissions, and then overriding
            //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
            //                                          int[] grantResults)
            // to handle the case where the user grants the permission. See the documentation
            // for ActivityCompat#requestPermissions for more details.
            return;
        }
        notificationManager.notify(1, builder.build());

//-------------notification over

清单文件包含

<uses-permission android:name="android.permission.POST_NOTIFICATIONS"></uses-permission>
,但在启动时不会请求用户的许可。 你能告诉我如何解决这个问题吗?谢谢

android-studio android-notifications android-permissions
1个回答
0
投票

阅读有关运行时权限的文档此处还可以查看 GitHub 上的权限示例此处 祝你好运

© www.soinside.com 2019 - 2024. All rights reserved.