Closed Joseph-Nathan closed 2 years ago
The both FCM
and HMS
push sevices have "almost" the same methods/callbacks in terms of handling push actions.
You can just implement them separately, and init the correct one on app start.
huawei_hmsavailability can be used to choose the supported push notification service to init.
abstract class PushNotificationService {
void initialize();
Future<void> requestToken(Function(String? token) callback);
void onForegroundMessage(NotificationMessage message) {
print('onForegroundMessage: $message');
}
void onMessageOpenedApp(NotificationMessage message) {
print('onMessageOpenedApp: $message');
}
}
// top-level func.
void onBackgroundMessage(NotificationMessage message) {
print('onMessageOpenedApp: $message');
}
class NotificationMessage {
const NotificationMessage({
this.title,
this.message,
this.data = const <String, dynamic>{},
});
final String? title;
final String? message;
final Map<String, dynamic> payload;
}
PushNotificationService
for the both push services, and implement required methods:class FcmPushNotificationService extends PushNotificationService {
final FirebaseMessaging _fcm = FirebaseMessaging.instance;
@override
Future initialize() async {
await _fcm.requestPermission();
await _fcm.setForegroundNotificationPresentationOptions(
alert: false,
badge: false,
sound: false,
);
FirebaseMessaging.onMessage.listen(_onMessage);
// ...
}
void _onMessage(RemoteMessage message) {
// notify PushNotificationService
onForegroundMessage(message.toPushNotificationMessage()); // toPushNotificationMessage RemoteMessage extension
}
// ...
}
class HmsPushNotificationService extends PushNotificationService {
// ...
}
PushNotificationService
):late final PushNotificationService pushNotificationService;
final isHmsAvailable = ... // please check for huawei_hmsavailability (or another alternative)
if(isHmsAvailable) {
pushNotificationService = HmsPushNotificationService();
} else {
pushNotificationService = FcmPushNotificationService();
}
e.g. request push token:
pushNotificationService.requestToken().then((token) => print('Push token: $token'));
@fatihergin thanks for you reply , But it's client side , the problem in my server side , I used cloud functions to send data message to multi devices , same that Huawei devices does n' t get the Data Message from my cloud , because it send messages in cloud functions by fcm , in selotion to Handel it server side before we used your last comment ,
Thanks bro.
Well, the issue is not clear to me anymore.
Did you have a look on sample hms server demos?
Can you narrow the issue down a bit?
@fatihergin thanks , hms push server by node.js that i need . now i have array of devices tokens (ios device , gms devices , hms devices ), is there in idea to get tokens hms devices only ,
i want used push kit to hms tokens , and fcm to another tokens . now i used i used fcm to send to all tokens and that do n't work with hms devices . thanks for your help .
What about sending supported push notification service info to your server along with push token? Then, your server can choose the right push notification service to send pushes.
@fatihergin are you mean save when I store the user token , i should store if it hmsSupport , So that when I send to that token I can choose to use push kit or not . or you mean something else ?
Description My application already includes the FCM push notification services , I'd like to support both FCM and Push Kit on supported devices.
Expected behavior FCM push notification services work with not hms devices , and huawei_push work with hms devices .
Current behavior FCM push notification services work with not hms devices
Environment
Other can provide any doc to support both FCM and Push Kit in same Flutter app .