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.
74 lines
2.4 KiB
Dart
74 lines
2.4 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 'package:test_sa/models/system_notification_model.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);
|
|
|
|
// todo @sikander, check notifications payload, because notification model is different to need to check from backend
|
|
|
|
// FirebaseMessaging.onMessageOpenedApp.listen((RemoteMessage message) {
|
|
// SystemNotificationModel notification = SystemNotificationModel.fromJson(message.data);
|
|
// if (notification.path == null || notification.path.isEmpty) return;
|
|
// Navigator.pushNamed(context, notification.path, arguments: notification.requestId);
|
|
// });
|
|
//
|
|
// FirebaseMessaging.onMessage.listen((RemoteMessage message) {
|
|
// SystemNotificationModel notification = SystemNotificationModel.fromJson(message.data);
|
|
// NotificationManger.showNotification(
|
|
// title: message.notification.title, subtext: message.notification.body, hashcode: int.tryParse(notification.requestId ?? "") ?? 1, payload: json.encode(message.data));
|
|
// return;
|
|
// });
|
|
}
|
|
}
|
|
|
|
Future<void> firebaseMessagingBackgroundHandler(RemoteMessage message) async {}
|