feat: PWA notifications non blocking
This commit is contained in:
parent
b2a4ffeb9b
commit
c42c4e9be7
@ -10,7 +10,7 @@ void main() async {
|
||||
options: DefaultFirebaseOptions.currentPlatform,
|
||||
);
|
||||
|
||||
await NotificationService().initialize();
|
||||
NotificationService().initialize();
|
||||
|
||||
runApp(MyApp());
|
||||
}
|
||||
|
||||
@ -19,21 +19,43 @@ class NotificationService {
|
||||
];
|
||||
|
||||
Future<void> initialize() async {
|
||||
try {
|
||||
if (!kIsWeb) {
|
||||
print('Notifications are only supported on web platform');
|
||||
return;
|
||||
}
|
||||
|
||||
if (!_isNotificationSupported()) {
|
||||
print('Notifications are not supported in this browser');
|
||||
return;
|
||||
}
|
||||
|
||||
_messaging = FirebaseMessaging.instance;
|
||||
|
||||
await _requestPermission();
|
||||
await _subscribeToTopics();
|
||||
await _setupMessageHandlers();
|
||||
} catch (e) {
|
||||
print('Error initializing notifications: $e');
|
||||
}
|
||||
}
|
||||
|
||||
bool _isNotificationSupported() {
|
||||
if (!kIsWeb) return false;
|
||||
|
||||
// Check if the browser supports notifications
|
||||
try {
|
||||
// This will throw an error if notifications are not supported
|
||||
return true; // Firebase already handles browser support checks
|
||||
} catch (e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> _requestPermission() async {
|
||||
if (_messaging == null) return;
|
||||
|
||||
try {
|
||||
NotificationSettings settings = await _messaging!.requestPermission(
|
||||
alert: true,
|
||||
announcement: false,
|
||||
@ -53,6 +75,9 @@ class NotificationService {
|
||||
} else {
|
||||
print('User declined or has not accepted permission for notifications');
|
||||
}
|
||||
} catch (e) {
|
||||
print('Error requesting notification permission: $e');
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> _subscribeToTopics() async {
|
||||
@ -150,6 +175,50 @@ class NotificationService {
|
||||
if (_messaging == null) return null;
|
||||
return await _messaging!.getToken(vapidKey: vapidKey);
|
||||
}
|
||||
|
||||
Future<NotificationStatus> getNotificationStatus() async {
|
||||
if (!kIsWeb) {
|
||||
return NotificationStatus.notSupported;
|
||||
}
|
||||
|
||||
if (!_isNotificationSupported()) {
|
||||
return NotificationStatus.notSupported;
|
||||
}
|
||||
|
||||
if (_messaging == null) {
|
||||
return NotificationStatus.notInitialized;
|
||||
}
|
||||
|
||||
try {
|
||||
NotificationSettings settings = await _messaging!.getNotificationSettings();
|
||||
|
||||
switch (settings.authorizationStatus) {
|
||||
case AuthorizationStatus.authorized:
|
||||
return NotificationStatus.enabled;
|
||||
case AuthorizationStatus.provisional:
|
||||
return NotificationStatus.provisional;
|
||||
case AuthorizationStatus.denied:
|
||||
return NotificationStatus.denied;
|
||||
case AuthorizationStatus.notDetermined:
|
||||
return NotificationStatus.notDetermined;
|
||||
default:
|
||||
return NotificationStatus.denied;
|
||||
}
|
||||
} catch (e) {
|
||||
print('Error getting notification status: $e');
|
||||
return NotificationStatus.error;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
enum NotificationStatus {
|
||||
enabled,
|
||||
provisional,
|
||||
denied,
|
||||
notDetermined,
|
||||
notSupported,
|
||||
notInitialized,
|
||||
error
|
||||
}
|
||||
|
||||
// Background message handler must be a top-level function
|
||||
|
||||
Loading…
Reference in New Issue
Block a user