You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
79 lines
2.2 KiB
Dart
79 lines
2.2 KiB
Dart
import 'dart:convert';
|
|
|
|
import 'package:firebase_core/firebase_core.dart';
|
|
import 'package:firebase_messaging/firebase_messaging.dart';
|
|
import 'package:flutter/material.dart';
|
|
|
|
import '../../models/app_notification.dart';
|
|
import 'notification_manger.dart';
|
|
|
|
class FirebaseNotificationManger {
|
|
static FirebaseMessaging messaging = FirebaseMessaging.instance;
|
|
static String? token;
|
|
|
|
static Future<String?> getToken() async {
|
|
NotificationSettings settings = await messaging.requestPermission(
|
|
alert: true,
|
|
announcement: false,
|
|
badge: true,
|
|
carPlay: false,
|
|
criticalAlert: false,
|
|
provisional: false,
|
|
sound: true,
|
|
);
|
|
|
|
if (settings.authorizationStatus == AuthorizationStatus.authorized) {
|
|
token = await messaging.getToken();
|
|
}
|
|
return token;
|
|
}
|
|
|
|
static initialized(BuildContext context) async {
|
|
await Firebase.initializeApp();
|
|
NotificationSettings settings;
|
|
try {
|
|
settings = await messaging.requestPermission(
|
|
alert: true,
|
|
announcement: false,
|
|
badge: true,
|
|
carPlay: false,
|
|
criticalAlert: false,
|
|
provisional: false,
|
|
sound: true,
|
|
);
|
|
} catch (error) {
|
|
return;
|
|
}
|
|
|
|
if (settings.authorizationStatus != AuthorizationStatus.authorized) {
|
|
return;
|
|
}
|
|
|
|
// Also handle any interaction when the app is in the background via a
|
|
// Stream listener
|
|
FirebaseMessaging.onBackgroundMessage(firebaseMessagingBackgroundHandler);
|
|
|
|
FirebaseMessaging.onMessageOpenedApp.listen((RemoteMessage message) {
|
|
AppNotification notification = AppNotification.fromJson(message.data);
|
|
Navigator.pushNamed(
|
|
context,
|
|
notification.path??"",
|
|
arguments: notification.requestId,
|
|
);
|
|
});
|
|
|
|
FirebaseMessaging.onMessage.listen((RemoteMessage message) {
|
|
AppNotification notification = AppNotification.fromJson(message.data);
|
|
NotificationManger.showNotification(
|
|
title: message.notification?.title,
|
|
subtext: message.notification?.body,
|
|
hashcode: int.tryParse(notification.requestId??"0") ?? 1,
|
|
payload: json.encode(message.data),
|
|
);
|
|
return;
|
|
});
|
|
}
|
|
}
|
|
|
|
Future<void> firebaseMessagingBackgroundHandler(RemoteMessage message) async {}
|