HMS-Core / hms-flutter-plugin

This repo contains all of Flutter HMS plugins.
https://developer.huawei.com/consumer/en/doc/overview/HMS-Core-Plugin?ha_source=hms1
Apache License 2.0
278 stars 139 forks source link

[huawei_push] support both FCM and Push Kit in same Flutter APP . #149

Closed Joseph-Nathan closed 2 years ago

Joseph-Nathan commented 2 years ago

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 .

fatihergin commented 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.

To make the above comment more clear:

  1. Create an abstract class which will handle the notification actions:
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;
}
  1. Extend 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 {
    // ...
}
  1. Choose the supported push service and init (second part of this step can be moved into a factory method in PushNotificationService):
late final PushNotificationService pushNotificationService;

final isHmsAvailable = ...  // please check for huawei_hmsavailability (or another alternative)

if(isHmsAvailable) {
    pushNotificationService = HmsPushNotificationService();
} else {
    pushNotificationService = FcmPushNotificationService();
}
  1. Use it whereever you want:

e.g. request push token:

pushNotificationService.requestToken().then((token) => print('Push token: $token'));

Joseph-Nathan commented 2 years ago

@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.

fatihergin commented 2 years ago

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?

Joseph-Nathan commented 2 years ago

@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 .

fatihergin commented 2 years ago

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.

Joseph-Nathan commented 2 years ago

@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 ?