简体   繁体   English

使用 Flutter notification_permissions package 监听设备通知权限

[英]listen to device notification permission using Flutter notification_permissions package

I am using the Flutter notification_permissions package.我正在使用 Flutter notification_permissions package。

I can retrieve the device notification permission status asynchronously by calling:我可以通过调用异步检索设备通知权限状态:

PermissionStatus p = await NotificationPermissions.getNotificationPermissionStatus()

However, I do not want to do this just once.但是,我不想只做一次。 I want to listen to the notification status as a stream, so that when it changes I can respond accordingly in my UI.我想以 stream 的形式收听通知状态,以便当它发生变化时,我可以在我的 UI 中做出相应的响应。

I tried the following:我尝试了以下方法:

NotificationPermissions.getNotificationPermissionStatus().asStream().listen((PermissionStatus s) {
    print('Notification permissions: $s');   
}

However, this stream does not emit events when I change the notification settings on the device.但是,当我更改设备上的通知设置时,此 stream 不会发出事件。

How can I achieve this?我怎样才能做到这一点?

I have achieved my desired functionality by creating a stateful widget that listens to the AppLifecycleState.通过创建一个监听 AppLifecycleState 的有状态小部件,我实现了我想要的功能。 I then call my async function whenever the app resumes to check the status of notification settings.然后,每当应用程序恢复检查通知设置的状态时,我都会调用我的异步 function。

More info here: Flutter: Update Widgets On Resume?更多信息在这里: Flutter:在简历上更新小部件?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM