mirror of
https://bitbucket.org/myhomie/myhomie_app.git
synced 2025-12-06 00:51:19 +00:00
168 lines
4.7 KiB
Dart
168 lines
4.7 KiB
Dart
import 'dart:io';
|
|
|
|
import 'package:firebase_messaging/firebase_messaging.dart';
|
|
import 'package:flutter/cupertino.dart';
|
|
import 'package:flutter/material.dart';
|
|
import 'package:fluttertoast/fluttertoast.dart';
|
|
import 'package:myhomie_app/Helpers/NotificationManager.dart';
|
|
import 'package:myhomie_app/Models/homieContext.dart';
|
|
|
|
import '../constants.dart';
|
|
|
|
class PushNotificationService {
|
|
//final FirebaseMessaging _fcm;
|
|
|
|
PushNotificationService(); //this._fcm
|
|
|
|
goToPage(Map<String, dynamic> message) {
|
|
var notification = PushNotificationMessage(
|
|
title: message['notification']['title'],
|
|
body: message['notification']['body'],
|
|
data: DataNotification(
|
|
collapseKey: message['data']['title'],
|
|
type: message['data']['type'])
|
|
);
|
|
|
|
//PageSwitcher(notification.data.type);
|
|
|
|
Fluttertoast.showToast(
|
|
msg: 'onResume or onLaunch '+ notification.title + ' : ' + notification.body,
|
|
toastLength: Toast.LENGTH_SHORT,
|
|
gravity: ToastGravity.BOTTOM,
|
|
timeInSecForIosWeb: 1,
|
|
backgroundColor: kMainColor,
|
|
textColor: Colors.white,
|
|
fontSize: 16.0
|
|
);
|
|
}
|
|
|
|
Future initialise(HomieAppContext? homieAppContext, {required BuildContext context}) async {
|
|
if (Platform.isIOS) {
|
|
//_fcm.requestPermission(); // IosNotificationSettings
|
|
FirebaseMessaging.instance.requestPermission();
|
|
}
|
|
|
|
//_fcm.subscribeToTopic("main");
|
|
|
|
FirebaseMessaging.instance.subscribeToTopic("main");
|
|
|
|
if(homieAppContext != null) {
|
|
if (homieAppContext.userId != null) {
|
|
print('MyHomie USER ID for fcm notification = ' + homieAppContext.userId.toString());
|
|
FirebaseMessaging.instance.subscribeToTopic(homieAppContext.userId!);
|
|
}
|
|
}
|
|
|
|
// If you want to test the push notification locally,
|
|
// you need to get the token and input to the Firebase console
|
|
// https://console.firebase.google.com/project/YOUR_PROJECT_ID/notification/compose
|
|
String? token = await FirebaseMessaging.instance.getToken();
|
|
print("FirebaseMessaging token: $token");
|
|
|
|
FirebaseMessaging.instance.getInitialMessage().then(
|
|
(value) {
|
|
debugPrint("COUCOUCOUC $value");
|
|
},
|
|
);
|
|
|
|
FirebaseMessaging.onMessage.listen((data) {
|
|
var notification;
|
|
print("onMessage: $data");
|
|
print(data);
|
|
|
|
if (Platform.isAndroid) {
|
|
notification = PushNotificationMessage(
|
|
title: data.notification!.title!,
|
|
body: data.notification!.body!,
|
|
data: DataNotification(
|
|
//labelButton: message['data']['labelButton'],
|
|
type: data.messageType!)
|
|
);
|
|
}
|
|
/*if (Platform.isIOS) {
|
|
notification = PushNotificationMessage(
|
|
title: message['aps']['alert']['title'],
|
|
body: message['aps']['alert']['body'],
|
|
data: DataNotification(
|
|
labelButton: message['labelButton'],
|
|
type: message['type'])
|
|
);
|
|
}*/
|
|
|
|
NotificationManager.handleNotificationMsg(notification);
|
|
// show notification UI here
|
|
});
|
|
|
|
FirebaseMessaging.onMessageOpenedApp.listen((RemoteMessage message) {
|
|
print('A new onMessageOpenedApp event was published!');
|
|
|
|
/*Navigator.pushNamed(
|
|
context,
|
|
'/message',
|
|
arguments: MessageArguments(message, true),
|
|
);*/
|
|
});
|
|
|
|
/*_fcm.configure(
|
|
onMessage: (Map<String, dynamic> message) async {
|
|
|
|
|
|
},
|
|
onLaunch: (Map<String, dynamic> message) async {
|
|
print("onLaunch: $message");
|
|
this.goToPage(message);
|
|
|
|
},
|
|
onResume: (Map<String, dynamic> message) async {
|
|
print("onResume: $message");
|
|
this.goToPage(message);
|
|
},
|
|
);*/
|
|
}
|
|
}
|
|
|
|
class PushNotificationMessage {
|
|
final String title;
|
|
final String body;
|
|
final DataNotification data;
|
|
PushNotificationMessage({
|
|
required this.title,
|
|
required this.body,
|
|
required this.data
|
|
});
|
|
}
|
|
|
|
class DataNotification {
|
|
final String? collapseKey;
|
|
final String type;
|
|
final String? labelButton;
|
|
DataNotification({
|
|
this.collapseKey,
|
|
required this.type,
|
|
this.labelButton
|
|
});
|
|
}
|
|
|
|
/*PageSwitcher(String type) {
|
|
switch(type) {
|
|
case "home":
|
|
pageController.jumpToPage(NavItemIcon.home.index);
|
|
break;
|
|
case "advices":
|
|
// In case of new advice, need to reload advices list automatically
|
|
pageController.jumpToPage(NavItemIcon.advices.index);
|
|
break;
|
|
case "scan":
|
|
pageController.jumpToPage(NavItemIcon.scan.index);
|
|
break;
|
|
case "shop":
|
|
pageController.jumpToPage(NavItemIcon.wallet.index);
|
|
break;
|
|
case "profile":
|
|
pageController.jumpToPage(NavItemIcon.profile.index);
|
|
break;
|
|
default:
|
|
pageController.jumpToPage(NavItemIcon.home.index);
|
|
break;
|
|
}
|
|
}*/ |