I have made an app and integrated firebase into it. But, when I want to send a notification from Firebase – Engage – Messaging to my app it goes very late, and sometimes it doesn’t go. I also see an error like this: There was an error loading the storage bucket
What is the reason? Any helps?
Is it from server or from my code?
import 'package:firebase_core/firebase_core.dart';
import 'package:firebase_messaging/firebase_messaging.dart';
import 'package:flutter/material.dart';
import 'package:overlay_support/overlay_support.dart';
import 'package:push_notification/notification_badge.dart';
import 'package:push_notification/push_notification_model.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return OverlaySupport(
child: MaterialApp(
debugShowCheckedModeBanner: false,
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: const HomePage(),
),
);
}
}
class HomePage extends StatefulWidget {
const HomePage({Key? key}) : super(key: key);
@override
State<HomePage> createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
//init some values
late final FirebaseMessaging _messaging;
late int _totalNotificationCounter;
//model
PushNotificationModel? _notificationInfo;
//register notification
void registerNotification() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp();
//instance for firebase messaging
_messaging = FirebaseMessaging.instance;
//three type of state in notification
//not determined(null), granted(true) and decline (false)
NotificationSettings settings = await _messaging.requestPermission(
alert: true,
badge: true,
provisional: false,
sound: true,
);
if (settings.authorizationStatus == AuthorizationStatus.authorized) {
print("User granted the permission");
//main message
FirebaseMessaging.onMessage.listen((RemoteMessage message) {
PushNotificationModel notification = PushNotificationModel(
title: message.notification!.title,
body: message.notification!.body,
dataTitle: message.data["title"],
dataBody: message.data["body"],
);
setState(() {
_totalNotificationCounter++;
_notificationInfo = notification;
});
if (notification != null) {
showSimpleNotification(
Text(_notificationInfo!.title!),
leading:
NotificationBadge(totalNotificaion: _totalNotificationCounter),
subtitle: Text(_notificationInfo!.body!),
background: Colors.cyan[700],
duration: const Duration(seconds: 2),
);
}
});
} else {
print("permission declined by user");
}
}
@override
void initState() {
registerNotification();
_totalNotificationCounter = 0;
super.initState();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text("Push Notification"),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
const Text(
"Bildirim",
textAlign: TextAlign.center,
style: TextStyle(
color: Colors.black,
fontSize: 20,
),
),
const SizedBox(height: 12),
// showing notification badge which
//will count the total notification
//that we receive
NotificationBadge(totalNotificaion: _totalNotificationCounter),
const SizedBox(height: 32),
// if notificationInfo is not null
_notificationInfo != null
? Column(
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Text(
"Başlık : ${_notificationInfo!.dataTitle ?? _notificationInfo!.title}",
style: const TextStyle(
fontWeight: FontWeight.bold,
fontSize: 16,
),
),
const SizedBox(height: 12),
Text(
"İçerik : ${_notificationInfo!.dataBody ?? _notificationInfo!.body}",
style: const TextStyle(
fontWeight: FontWeight.bold,
fontSize: 16,
),
),
],
)
: Container(),
],
),
),
);
}
}
2
Answers
I was also getting this error in Firebase Cloud Messaging. but when I initialized everything in RealTime Database and Storage from firebase, I chose the database location when I was initializing the real-time database (US central). this worked for me, hope it will work for you also.
try using fcm token
final fcmToken = await FirebaseMessaging.instance.getToken();
add this token in your firebase