I have 2 questions:
1- I have been trying to integrate push notifications with my Flutter web project, onMessage is working fine, but the method onMessageOpenedApp is not triggered neither getInitialMessage(). Its working fine on android but not on web.
2- Is it enough to call message.getToken('vapidKey:') or I have to do something more?
my main.dart:
Future<void> _firebaseMessagingBackgroundHandler(RemoteMessage message) async {
launch(message.data['url']);
await Firebase.initializeApp();
}
void main() async {
WidgetsFlutterBinding.ensureInitialized();
FirebaseMessaging.onBackgroundMessage(_firebaseMessagingBackgroundHandler);
_setupLogging();
await EasyLocalization.ensureInitialized();
await Firebase.initializeApp();
await SharedPreferences.getInstance();
runApp(
EasyLocalization(
supportedLocales: [Locale('en')],
path: 'assets/translations',
assetLoader: CodegenLoader(),
fallbackLocale: Locale('en'),
child: MyApp()),
);
await SharedPreferencesManager.instance.initialize();
FirebaseMessaging messaging = FirebaseMessaging.instance;
await messaging.requestPermission(
alert: true,
announcement: false,
badge: true,
carPlay: false,
criticalAlert: false,
provisional: false,
sound: true,
);
await messaging.getToken(
vapidKey:
'my key');
}
my homepage:
class _HomePageState extends State<HomePage> {
Future setupInteractedMessage() async {
await FirebaseMessaging.instance
.getInitialMessage()
.then((RemoteMessage? message) {
if (message != null) {
_handleMessage(message);
}
});
FirebaseMessaging.onMessage.listen((message) {
showDialog(
context: context,
builder: (_) {
return GestureDetector(
onTap: () {
Navigator.of(context).pop();
_handleMessage(message);
},
child: AlertDialog(
title: Text(message.notification!.title!),
content: SingleChildScrollView(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [Text(message.notification!.body!)],
),
),
),
);
});
});
FirebaseMessaging.onMessageOpenedApp.listen(_handleMessage);
}
void _handleMessage(RemoteMessage message) {
if (message.data['type'] == 'NewOrder') {
AutoRouter.of(context).pushNamed('/order_list');
}
if (message.data['type'] == 'UpdateOrder') {
AutoRouter.of(context).pushNamed('/order_details/${message.data['id']}');
}
if (message.data['type'] == 'NewOrderTab') {
AutoRouter.of(context).pushNamed('/table_order/${message.data['id']}');
}
if (message.data['type'] == 'UpdateOrderTab') {
AutoRouter.of(context).pushNamed('/table_order/${message.data['id']}');
}
if (message.data['type'] == 'NewTable') {
AutoRouter.of(context).pushNamed('/table_list');
}
if (message.data['type'] == 'UpdateTable') {
AutoRouter.of(context).pushNamed('/table_list');
}
}
@override
void initState() {
if (!RestaurantBloc.instance.branch!.hasValue) {
RestaurantBloc.instance.getBranch(id: widget.id);
}
SignalRService signalRService = SignalRService();
signalRService.init();
setupInteractedMessage();
super.initState();
}