允许用户允许通知



我正在尝试在flutter/firebase/android中实现用户通知。

我添加了函数requestPermission((

class NavigatorView extends StatefulWidget {
const NavigatorView({Key? key}) : super(key: key);
@override
_NavigatorViewState createState() => _NavigatorViewState();
}
class _NavigatorViewState extends State<NavigatorView> {
String get userId => AuthService.firebase().currentUser!.id;
@override
void initState() {
super.initState();
requestPermission();
}
requestPermission() async {
FirebaseMessaging messaging = FirebaseMessaging.instance;
NotificationSettings settings = await messaging.requestPermission(
alert: true,
announcement: false,
badge: true,
carPlay: false,
criticalAlert: false,
provisional: false,
sound: true,
);
if (settings.authorizationStatus == AuthorizationStatus.authorized) {
log('User granted permission');
} else if (settings.authorizationStatus ==
AuthorizationStatus.provisional) {
log('User grander provisional permission');
} else {
log('User declined or has not accepted permission');
}
}
int currentIndex = 0;
final screens = [
//HomePage(),
const AllMyChats(),
const JobApplicationsView(),
const JobFilter(),
];
@override
Widget build(BuildContext context) {
return Scaffold(
body: screens[currentIndex],
bottomNavigationBar: BottomNavigationBar(
currentIndex: currentIndex,
selectedItemColor: Colors.white,
backgroundColor: Colors.blue,
type: BottomNavigationBarType.fixed,
selectedFontSize: 10,
onTap: (index) => setState(() => currentIndex = index),
items: const <BottomNavigationBarItem>[
BottomNavigationBarItem(
icon: Icon(Icons.person),
label: 'Chats',
),
BottomNavigationBarItem(
icon: Icon(Icons.school),
label: 'Jobs',
),
BottomNavigationBarItem(
icon: Icon(Icons.business),
label: 'Job Applications',
),
],
),
);
}
}

但它从不请求许可。我总是看到以下日志:

用户拒绝或未接受权限

这里缺少什么?

我最近在Android上遇到了这个问题。如果您使用的是较新的手机或模拟器,则需要将for gradle的构建修改为以sdk 33为目标,并编译sdk 33。较旧的sdk没有包含通知权限,因此通过将编译或目标sdk更新为更高的值,您应该为最终用户启用权限对话框。

相关内容

  • 没有找到相关文章

最新更新