wip: starting implementing firebase messaging implementation
continuous-integration/drone/pr Build is failing

This commit is contained in:
2023-08-17 21:00:05 +02:00
parent 18e8d1c754
commit 1be187b83d
27 changed files with 1372 additions and 0 deletions
@@ -0,0 +1,19 @@
// Copyright (C) 2023 WYATT GROUP
// Please see the AUTHORS file for details.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
abstract class CloudMessagingBlocFirebase {
static String get testString => 'Package: Cloud Messaging Bloc Firebase';
}
@@ -0,0 +1,115 @@
import 'dart:async';
import 'dart:convert';
import 'package:firebase_messaging/firebase_messaging.dart'
hide RemoteNotification;
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
import 'package:wyatt_cloud_messaging_bloc_base/wyatt_cloud_messaging_bloc.dart';
class CloudMessagingFirebaseDataSource extends CloudMessagingRemoteDataSource {
final FirebaseMessaging _firebaseMessagingInstance =
FirebaseMessaging.instance;
final FlutterLocalNotificationsPlugin _localNotificationInstance =
FlutterLocalNotificationsPlugin();
@override
Future<Stream<RemoteNotification>> onNotification() {
throw UnimplementedError();
}
@override
Future<Stream<RemoteNotification>> onNotificationBackground() {
throw UnimplementedError();
}
@override
Future<Stream<RemoteNotification>> onNotificationForeground() async {
await _firebaseMessagingInstance
.setForegroundNotificationPresentationOptions(
alert: true,
badge: true,
sound: true,
);
const AndroidNotificationChannel channel = AndroidNotificationChannel(
'high_importance_channel',
'High Importance Notifications',
description: 'This channel is used for important notifications.',
importance: Importance.max,
);
const initializationSettingsAndroid = AndroidInitializationSettings(
'@drawable/ic_stat_onesignal_default',
);
const initializationSettings = InitializationSettings(
android: initializationSettingsAndroid,
iOS: DarwinInitializationSettings(),
);
final StreamController<RemoteNotification> foregroundStream =
StreamController<RemoteNotification>();
await _localNotificationInstance.initialize(
initializationSettings,
onDidReceiveNotificationResponse: (message) {
foregroundStream.add(
RemoteNotification(
data:
jsonDecode(message.payload.toString()) as Map<String, dynamic>,
),
);
},
);
await _localNotificationInstance
.resolvePlatformSpecificImplementation<
AndroidFlutterLocalNotificationsPlugin>()
?.createNotificationChannel(channel);
FirebaseMessaging.onMessage.listen(
(message) {
final notification = RemoteNotification(
title: message.notification?.title,
body: message.notification?.body,
);
final android = message.notification?.android;
if (message.notification != null && android != null) {
_localNotificationInstance.show(
notification.hashCode,
notification.title,
notification.body,
NotificationDetails(
android: AndroidNotificationDetails(
channel.id,
channel.name,
channelDescription: channel.description,
icon: android.smallIcon,
),
),
payload: message.data['path'].toString(),
);
}
},
);
return foregroundStream.stream;
}
@override
Future<String?> getToken() => _firebaseMessagingInstance.getToken();
@override
Future<void> requestPermissions() =>
_firebaseMessagingInstance.requestPermission();
@override
Future<void> subscribeToTopic(String topic) =>
_firebaseMessagingInstance.subscribeToTopic(topic);
@override
Future<void> unsubscribeFromTopic(String topic) =>
_firebaseMessagingInstance.unsubscribeFromTopic(topic);
}
@@ -0,0 +1,20 @@
// Copyright (C) 2023 WYATT GROUP
// Please see the AUTHORS file for details.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
/// A
library wyatt_cloud_messaging_bloc_firebase;
export 'src/cloud_messaging_bloc_firebase.dart';