我在应用程序中工作,当应用程序启动时,我需要询问用户是否要接收推送通知。我知道推送通知不是一个危险的权限,但我需要显示弹出窗口可能选择。 这是我的清单
<uses-permission android:name="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE"/>
这是应该显示弹出窗口的运行时方法
public void receiveNotification(View v) {
boolean hasPermission = (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.BIND_NOTIFICATION_LISTENER_SERVICE) == PackageManager.PERMISSION_GRANTED);
if (hasPermission) {
String token = FirebaseInstanceId.getInstance().getToken();
Log.d(Tag, "Token"+token);
Toast.makeText(MainActivity.this, token, Toast.LENGTH_SHORT).show();
}
else {
ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.BIND_NOTIFICATION_LISTENER_SERVICE}, REQUEST_CODE_ASK_PERMISSIONS);
}
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
@NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == REQUEST_CODE_ASK_PERMISSIONS) {
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
String token = FirebaseInstanceId.getInstance().getToken();
Log.d(Tag, "Token"+token);
Toast.makeText(MainActivity.this, token, Toast.LENGTH_SHORT).show();
}
else {
// Permission Denied
Toast.makeText(MainActivity.this, "message", Toast.LENGTH_SHORT)
.show();
}
}
}
有人知道我怎么能显示这个弹出窗口?它的importat :) 谢谢!