Commit 0a60f20c authored by Lucas Abruzzi da Rosa's avatar Lucas Abruzzi da Rosa
Browse files

Setup do firebase para web (sem envio de token)

parent e497cb80
Showing with 43 additions and 18 deletions
+43 -18
import 'package:comunicacao_hsl/model/mixin/network_basic_operations_mixin.dart';
import 'package:comunicacao_hsl/model/network/communication.dart';
import 'package:firebase_messaging/firebase_messaging.dart';
import 'package:flutter/foundation.dart';
class LoginModel with NetworkBasicOperationsMixin {
/// Singleton constructor
......@@ -18,7 +19,8 @@ class LoginModel with NetworkBasicOperationsMixin {
String? token;
Future<bool> connect(int withMatricula) async {
String? token = await FirebaseMessaging.instance.getToken();
String? token;
if (!kIsWeb) token = await FirebaseMessaging.instance.getToken();
var response = await post(body: {
"registry": withMatricula,
if (token != null) "firebaseMessagingToken": token,
......
......@@ -4,14 +4,13 @@ import 'package:comunicacao_hsl/model/network/communication.dart';
import 'package:firebase_core/firebase_core.dart';
import 'package:firebase_messaging/firebase_messaging.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/foundation.dart';
class HomeController {
final ValueNotifier<List<Announcement>> announcements = ValueNotifier([]);
final ValueNotifier<bool> haveLoadedData = ValueNotifier(false);
final ValueNotifier<int> nonViewedCount = CommunicationModel().nonViewedCount;
late FirebaseMessaging _messaging;
HomeController();
void loadCommunications() {
......@@ -44,10 +43,10 @@ class HomeController {
String? token = await FirebaseMessaging.instance.getToken();
print('Firebase Messaging token: $token');
_messaging = FirebaseMessaging.instance;
FirebaseMessaging.onBackgroundMessage(firebaseMessagingBackgroundHandler);
NotificationSettings settings = await _messaging.requestPermission(
NotificationSettings settings =
await FirebaseMessaging.instance.requestPermission(
alert: true,
badge: true,
provisional: false,
......@@ -56,16 +55,6 @@ class HomeController {
if (settings.authorizationStatus == AuthorizationStatus.authorized) {
print('User granted permission');
FirebaseMessaging.onMessage.listen((RemoteMessage message) {
print(
"Firebase onMessage: ${message.messageId} ${message.notification?.title} ${message.notification?.body}");
});
FirebaseMessaging.onMessageOpenedApp.listen((RemoteMessage message) {
print(
"Firebase onMessageOpenedApp: ${message.messageId} ${message.notification?.title} ${message.notification?.body}");
});
} else {
print('User declined or has not accepted permission');
}
......
......@@ -112,7 +112,7 @@ packages:
name: firebase_core
url: "https://pub.dartlang.org"
source: hosted
version: "1.7.0"
version: "1.8.0"
firebase_core_platform_interface:
dependency: transitive
description:
......@@ -140,14 +140,14 @@ packages:
name: firebase_messaging_platform_interface
url: "https://pub.dartlang.org"
source: hosted
version: "3.0.6"
version: "3.0.7"
firebase_messaging_web:
dependency: transitive
description:
name: firebase_messaging_web
url: "https://pub.dartlang.org"
source: hosted
version: "2.0.6"
version: "2.0.7"
flutter:
dependency: "direct main"
description: flutter
......
importScripts("https://www.gstatic.com/firebasejs/8.6.1/firebase-app.js");
importScripts("https://www.gstatic.com/firebasejs/8.6.1/firebase-messaging.js");
firebase.initializeApp({
apiKey: "AIzaSyA7PR5gYMb3qNwUg6Hbyqx0Z1cebALSl1c",
authDomain: "comunicacaohsl-37dd1.firebaseapp.com",
projectId: "comunicacaohsl-37dd1",
storageBucket: "comunicacaohsl-37dd1.appspot.com",
messagingSenderId: "463039167215",
appId: "1:463039167215:web:835a78c252399fa7ebb91a",
validKey: "BETNjn_A0k_TC8u1nJOVKcV18YeXozDNfGy1Ey02NxK5WSsyO4GDcXEWapPkoEmo5H6bXua4vgm-_NoTVsJTS-k",
});
const messaging = firebase.messaging();
messaging.onBackgroundMessage((message) => {
console.log("onBackgroundMessage", message);
});
\ No newline at end of file
......@@ -41,6 +41,21 @@
<!-- This script installs service_worker.js to provide PWA functionality to
application. For more information, see:
https://developers.google.com/web/fundamentals/primers/service-workers -->
<script src="https://www.gstatic.com/firebasejs/8.6.1/firebase-app.js"></script>
<script src="https://www.gstatic.com/firebasejs/8.6.1/firebase-messaging.js"></script>
<script>
firebase.initializeApp({
apiKey: "AIzaSyA7PR5gYMb3qNwUg6Hbyqx0Z1cebALSl1c",
authDomain: "comunicacaohsl-37dd1.firebaseapp.com",
projectId: "comunicacaohsl-37dd1",
storageBucket: "comunicacaohsl-37dd1.appspot.com",
messagingSenderId: "463039167215",
appId: "1:463039167215:web:835a78c252399fa7ebb91a",
validKey: "BETNjn_A0k_TC8u1nJOVKcV18YeXozDNfGy1Ey02NxK5WSsyO4GDcXEWapPkoEmo5H6bXua4vgm-_NoTVsJTS-k",
});
</script>
<script>
var serviceWorkerVersion = null;
var scriptLoaded = false;
......@@ -61,6 +76,7 @@
// Wait for registration to finish before dropping the <script> tag.
// Otherwise, the browser will load the script multiple times,
// potentially different versions.
navigator.serviceWorker.register("firebase-messaging-sw.js");
var serviceWorkerUrl = 'flutter_service_worker.js?v=' + serviceWorkerVersion;
navigator.serviceWorker.register(serviceWorkerUrl)
.then((reg) => {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment