Pau*_*aul -1 android push-notification android-permissions
我正在使用应用程序,我需要使用推送通知.我知道推送通知是正常的权限,所以我不能在运行时询问它.但是我会在用户下载并安装应用程序时插入权限,通知应用程序应该发送推送通知.我该怎么做?我必须在清单中插入一些东西吗?谢谢
Rak*_*oni 44
以下是新更新的答案:在 API 33 中,我们需要推送通知的运行时权限:此处提供了完整文档,但您可以简单地使用如下所示:
在应用程序级别:
<uses-permission android:name="android.permission.POST_NOTIFICATIONS"/>
Run Code Online (Sandbox Code Playgroud)
您可以使用以下命令触发提示:
pushNotificationPermissionLauncher.launch(android.Manifest.permission.POST_NOTIFICATIONS)
Run Code Online (Sandbox Code Playgroud)
处理通知结果:
private val pushNotificationPermissionLauncher = registerForActivityResult(RequestPermission()) { granted ->
viewModel.inputs.onTurnOnNotificationsClicked(granted)
}
Run Code Online (Sandbox Code Playgroud)
Nil*_*hod 34
以下是如何在 Android 13 (Tiramisu) 中请求通知的运行时权限的完整示例
manifest首先,在您的文件中添加以下权限
<uses-permission android:name="android.permission.POST_NOTIFICATIONS"/>
Run Code Online (Sandbox Code Playgroud)
使用以下代码请求运行时权限
package com.example.myapplication
import android.app.NotificationChannel
import android.app.NotificationManager
import android.content.Context
import android.content.Intent
import android.content.pm.PackageManager
import android.graphics.BitmapFactory
import android.graphics.Color
import android.net.Uri
import android.os.Build
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.provider.Settings.ACTION_APPLICATION_DETAILS_SETTINGS
import android.widget.Button
import android.widget.Toast
import androidx.activity.result.contract.ActivityResultContracts
import androidx.core.app.NotificationCompat
import com.google.android.material.dialog.MaterialAlertDialogBuilder
class MainActivity : AppCompatActivity() {
private val notificationPermissionLauncher =
registerForActivityResult(ActivityResultContracts.RequestPermission()) { isGranted ->
hasNotificationPermissionGranted = isGranted
if (!isGranted) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (Build.VERSION.SDK_INT >= 33) {
if (shouldShowRequestPermissionRationale(android.Manifest.permission.POST_NOTIFICATIONS)) {
showNotificationPermissionRationale()
} else {
showSettingDialog()
}
}
}
} else {
Toast.makeText(applicationContext, "notification permission granted", Toast.LENGTH_SHORT)
.show()
}
}
private fun showSettingDialog() {
MaterialAlertDialogBuilder(this, com.google.android.material.R.style.MaterialAlertDialog_Material3)
.setTitle("Notification Permission")
.setMessage("Notification permission is required, Please allow notification permission from setting")
.setPositiveButton("Ok") { _, _ ->
val intent = Intent(ACTION_APPLICATION_DETAILS_SETTINGS)
intent.data = Uri.parse("package:$packageName")
startActivity(intent)
}
.setNegativeButton("Cancel", null)
.show()
}
private fun showNotificationPermissionRationale() {
MaterialAlertDialogBuilder(this, com.google.android.material.R.style.MaterialAlertDialog_Material3)
.setTitle("Alert")
.setMessage("Notification permission is required, to show notification")
.setPositiveButton("Ok") { _, _ ->
if (Build.VERSION.SDK_INT >= 33) {
notificationPermissionLauncher.launch(android.Manifest.permission.POST_NOTIFICATIONS)
}
}
.setNegativeButton("Cancel", null)
.show()
}
var hasNotificationPermissionGranted = false
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
findViewById<Button>(R.id.btnRequestPermission).setOnClickListener {
if (Build.VERSION.SDK_INT >= 33) {
notificationPermissionLauncher.launch(android.Manifest.permission.POST_NOTIFICATIONS)
} else {
hasNotificationPermissionGranted = true
}
}
findViewById<Button>(R.id.btnShowNotification).setOnClickListener {
if (checkSelfPermission(android.Manifest.permission.POST_NOTIFICATIONS) == PackageManager.PERMISSION_GRANTED) {
showNotification()
}
}
}
private fun showNotification() {
val channelId = "12345"
val description = "Test Notification"
val notificationManager =
getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val notificationChannel =
NotificationChannel(channelId, description, NotificationManager.IMPORTANCE_HIGH)
notificationChannel.lightColor = Color.BLUE
notificationChannel.enableVibration(true)
notificationManager.createNotificationChannel(notificationChannel)
}
val builder = NotificationCompat.Builder(this, channelId)
.setContentTitle("Hello World")
.setContentText("Test Notification")
.setSmallIcon(R.drawable.ic_launcher_foreground)
.setLargeIcon(
BitmapFactory.decodeResource(
this.resources, R.drawable
.ic_launcher_background
)
)
notificationManager.notify(12345, builder.build())
}
}
Run Code Online (Sandbox Code Playgroud)
如果您使用 jetpack compose,则使用以下代码
class MainActivity : ComponentActivity() {
@RequiresApi(VERSION_CODES.M)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
JetpackComposeNotifcationPermissionTheme {
val context = LocalContext.current
val permissionOpenDialog = remember { mutableStateOf(false) }
val rationalPermissionOpenDialog = remember { mutableStateOf(false) }
if (permissionOpenDialog.value) {
ShowSettingDialog(openDialog = permissionOpenDialog)
}
var hasNotificationPermission by remember {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
mutableStateOf(
ContextCompat.checkSelfPermission(
context,
Manifest.permission.POST_NOTIFICATIONS
) == PackageManager.PERMISSION_GRANTED
)
} else mutableStateOf(true)
}
val launcher = rememberLauncherForActivityResult(
contract = ActivityResultContracts.RequestPermission(),
onResult = { isGranted ->
if (!isGranted) {
if (shouldShowRequestPermissionRationale(Manifest.permission.POST_NOTIFICATIONS)) {
rationalPermissionOpenDialog.value = true
} else {
permissionOpenDialog.value = true
}
} else {
hasNotificationPermission = isGranted
}
}
)
if (rationalPermissionOpenDialog.value) {
ShowRationalPermissionDialog(openDialog = rationalPermissionOpenDialog) {
rationalPermissionOpenDialog.value = false
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
launcher.launch(Manifest.permission.POST_NOTIFICATIONS)
}
}
}
Column(
modifier = Modifier.fillMaxSize(),
verticalArrangement = Arrangement.Center,
horizontalAlignment = Alignment.CenterHorizontally
) {
Button(onClick = {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
launcher.launch(Manifest.permission.POST_NOTIFICATIONS)
}
}) {
Text(text = "Request permission")
}
Button(onClick = {
if (hasNotificationPermission) {
showNotification()
}
}) {
Text(text = "Show notification")
}
}
}
}
}
private fun showNotification() {
val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
val channelId = "12345"
val description = "Test Notification"
if (VERSION.SDK_INT >= VERSION_CODES.O) {
val notificationChannel =
NotificationChannel(channelId, description, NotificationManager.IMPORTANCE_HIGH)
notificationChannel.lightColor = Color.BLUE
notificationChannel.enableVibration(true)
notificationManager.createNotificationChannel(notificationChannel)
}
val notification = NotificationCompat.Builder(applicationContext, channelId)
.setSmallIcon(R.drawable.ic_launcher_foreground)
.setContentTitle("Hello Nilesh")
.setContentText("Test Notification")
.build()
notificationManager.notify(1, notification)
}
@Composable
fun ShowSettingDialog(openDialog: MutableState<Boolean>) {
if (openDialog.value) {
AlertDialog(
onDismissRequest = {
openDialog.value = false
},
title = {
Text(text = "Notification Permission")
},
text = {
Text("Notification permission is required, Please allow notification permission from setting")
},
buttons = {
Row(
modifier = Modifier.fillMaxWidth(),
horizontalArrangement = Arrangement.End,
verticalAlignment = Alignment.CenterVertically,
) {
TextButton(
onClick = {
openDialog.value = false
}
) {
Text("Cancel")
}
Spacer(modifier = Modifier.width(20.dp))
TextButton(
onClick = {
openDialog.value = false
val intent = Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS)
intent.data = Uri.parse("package:$packageName")
startActivity(intent)
},
) {
Text("Ok")
}
}
},
)
}
}
@Composable
fun ShowRationalPermissionDialog(openDialog: MutableState<Boolean>, onclick: () -> Unit) {
if (openDialog.value) {
AlertDialog(
onDismissRequest = {
openDialog.value = false
},
title = {
Text(text = "Alert")
},
text = {
Text("Notification permission is required, to show notification")
},
buttons = {
Row(
modifier = Modifier.fillMaxWidth(),
horizontalArrangement = Arrangement.End,
verticalAlignment = Alignment.CenterVertically,
) {
TextButton(
onClick = {
openDialog.value = false
}
) {
Text("Cancel")
}
Spacer(modifier = Modifier.width(20.dp))
TextButton(
onClick = onclick,
) {
Text("Ok")
}
}
},
)
}
}
}
Run Code Online (Sandbox Code Playgroud)
Kun*_*wla 24
如此处所述,您不需要推送通知的权限.
实际上,推送通知权限属于正常类别权限,例如INTERNET权限,而非危险类别权限.
您不必要求推送通知权限.
虽然联系人/位置是危险权限,因为您正在访问用户数据.因此始终需要请求用户允许它.
希望你明白. https://developer.android.com/guide/topics/security/permissions.html
小智 14
如果您的应用面向 Android 13 (Tiramisu) 或更高版本,则必须声明POST_NOTIFICATIONS权限,如下所示:
<uses-permission android:name="android.permission.POST_NOTIFICATIONS"/>
Run Code Online (Sandbox Code Playgroud)
有关更多详细信息,请参阅Android 开发人员文档中的通知运行时权限页面。
小智 7
两线解决方案
int permissionState = ContextCompat.checkSelfPermission(this, android.Manifest.permission.POST_NOTIFICATIONS);
// If the permission is not granted, request it.
if (permissionState == PackageManager.PERMISSION_DENIED) {
ActivityCompat.requestPermissions(this, new String[]{android.Manifest.permission.POST_NOTIFICATIONS}, 1);
}
Run Code Online (Sandbox Code Playgroud)
<uses-permission android:name="android.permission.POST_NOTIFICATIONS"/>// add this to menifest file
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
if (ActivityCompat.checkSelfPermission(this,Manifest.permission.POST_NOTIFICATIONS) != PackageManager.PERMISSION_GRANTED) {
requestPermissions(new String[] {Manifest.permission.POST_NOTIFICATIONS}, 1);
}
else {
// repeat the permission or open app details
}
}
Run Code Online (Sandbox Code Playgroud)
Fax*_*yev -4
尝试这个:
在您的 Activity 类中,添加以下代码:
private static final int NOTIFICATION_PERMISSION_CODE = 123;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.your_activity);
requestNotificationPermission();
// Some code
}
private void requestNotificationPermission() {
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_NOTIFICATION_POLICY) == PackageManager.PERMISSION_GRANTED)
return;
if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.ACCESS_NOTIFICATION_POLICY)) {
}
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_NOTIFICATION_POLICY}, NOTIFICATION_PERMISSION_CODE );
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
// Checking the request code of our request
if (requestCode == NOTIFICATION_PERMISSION_CODE ) {
// If permission is granted
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// Displaying a toast
Toast.makeText(this, "Permission granted now you can read the storage", Toast.LENGTH_LONG).show();
} else {
// Displaying another toast if permission is not granted
Toast.makeText(this, "Oops you just denied the permission", Toast.LENGTH_LONG).show();
}
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
34668 次 |
| 最近记录: |