From c00e09fddda6af367316a1c9501f585a5eccd171 Mon Sep 17 00:00:00 2001 From: Sikander Saleem Date: Thu, 29 Aug 2024 15:08:27 +0300 Subject: [PATCH] huawei push notifications integrated. --- android/app/build.gradle | 1 + android/app/src/main/AndroidManifest.xml | 35 +++++++++++++++ .../java/com/example/test_sa/HService.java | 25 +++++++++++ .../firebase_notification_manger.dart | 45 ++++++++++++++----- lib/models/user.dart | 9 ++-- pubspec.lock | 4 +- pubspec.yaml | 2 +- 7 files changed, 101 insertions(+), 20 deletions(-) create mode 100644 android/app/src/main/java/com/example/test_sa/HService.java diff --git a/android/app/build.gradle b/android/app/build.gradle index d946affb..9aadf76f 100644 --- a/android/app/build.gradle +++ b/android/app/build.gradle @@ -84,4 +84,5 @@ flutter { dependencies { implementation 'com.huawei.agconnect:agconnect-core:1.5.2.300' + implementation 'com.huawei.hms:push:6.7.0.300' } diff --git a/android/app/src/main/AndroidManifest.xml b/android/app/src/main/AndroidManifest.xml index c1ce832f..13ec16d8 100644 --- a/android/app/src/main/AndroidManifest.xml +++ b/android/app/src/main/AndroidManifest.xml @@ -68,5 +68,40 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/android/app/src/main/java/com/example/test_sa/HService.java b/android/app/src/main/java/com/example/test_sa/HService.java new file mode 100644 index 00000000..6ff86bab --- /dev/null +++ b/android/app/src/main/java/com/example/test_sa/HService.java @@ -0,0 +1,25 @@ +package com.example.test_sa; + +import android.util.Log; + +import com.huawei.hms.push.HmsMessageService; +import com.huawei.hms.push.RemoteMessage; + +public class HService extends HmsMessageService { + + @Override + public void onMessageReceived(RemoteMessage remoteMessage) { + super.onMessageReceived(remoteMessage); + + if (remoteMessage != null) { + System.out.println("remote:"+remoteMessage.getData()); + if (!remoteMessage.getData().isEmpty()) { + Log.d("HMS", "Payload" + remoteMessage.getData()); + } + + if (remoteMessage.getNotification() != null) { + Log.d("HMS", "Message Notification Body: " + remoteMessage.getNotification().getBody()); + } + } + } +} \ No newline at end of file diff --git a/lib/controllers/notification/firebase_notification_manger.dart b/lib/controllers/notification/firebase_notification_manger.dart index 7862cd34..8218f30d 100644 --- a/lib/controllers/notification/firebase_notification_manger.dart +++ b/lib/controllers/notification/firebase_notification_manger.dart @@ -7,7 +7,7 @@ import 'package:google_api_availability/google_api_availability.dart'; // import 'package:huawei_hmsavailability/huawei_hmsavailability.dart'; import 'package:huawei_push/huawei_push.dart' as h_push; import 'package:test_sa/controllers/notification/notification_manger.dart'; - +import 'package:test_sa/extensions/string_extensions.dart'; import 'package:test_sa/models/all_requests_and_count_model.dart'; import 'package:test_sa/models/device/asset_transfer.dart'; import 'package:test_sa/models/new_models/gas_refill_model.dart'; @@ -40,18 +40,11 @@ class FirebaseNotificationManger { if (!(await isGoogleServicesAvailable())) { h_push.Push.enableLogger(); final result = await h_push.Push.setAutoInitEnabled(true); - - h_push.Push.onNotificationOpenedApp.listen((message) { - // newMessage(toFirebaseRemoteMessage(message)); - }, onError: (e) => print(e.toString())); - - h_push.Push.onMessageReceivedStream.listen((message) { - // newMessage(toFirebaseRemoteMessage(message)); - }, onError: (e) => print(e.toString())); + h_push.Push.onMessageReceivedStream.listen(_onMessageReceived, onError: _onMessageReceiveError); h_push.Push.getTokenStream.listen((hToken) { // onToken(token); - print("Huawei Token: ${token}"); + // print("Huawei Token: ${hToken}"); token = hToken; }, onError: (e) { print(e.toString()); @@ -75,7 +68,15 @@ class FirebaseNotificationManger { } } print("pushToken:$token"); - // return token; + // return token; + } + + static void _onMessageReceived(h_push.RemoteMessage remoteMessage) { + print("onMessageReceivedStream:${remoteMessage.toMap()}"); + } + + static void _onMessageReceiveError(Object error) { + print("onMessageReceivedStream:${error?.toString()}"); } static Future isGoogleServicesAvailable() async { @@ -119,7 +120,27 @@ class FirebaseNotificationManger { } static initialized(BuildContext context) async { - //await Firebase.initializeApp(); + if (!(await isGoogleServicesAvailable())) { + var initialNotification = await h_push.Push.getInitialNotification(); + if (initialNotification != null) { + Map remoteData = Map.from(initialNotification["extras"] as Map); + handleMessage(context, remoteData); + } + + h_push.Push.onNotificationOpenedApp.listen((message) { + try { + if (message is Map) { + Map remoteData = message; + remoteData = remoteData["extras"]; + handleMessage(context, remoteData); + } + } catch (ex) { + print("parsingError:$ex"); + } + }, onError: (e) => print("onNotificationOpenedApp Error${e.toString()}")); + return; + } + NotificationSettings settings; try { diff --git a/lib/models/user.dart b/lib/models/user.dart index 6c525eb2..367c05ec 100644 --- a/lib/models/user.dart +++ b/lib/models/user.dart @@ -1,3 +1,5 @@ +import 'dart:io'; + import 'package:test_sa/controllers/notification/firebase_notification_manger.dart'; import 'package:test_sa/models/enums/user_types.dart'; @@ -73,11 +75,8 @@ class User { Future> toLoginJson() async { // if (FirebaseNotificationManger.token == null) await FirebaseNotificationManger.getToken(); // print("PuToken:${FirebaseNotificationManger?.token}"); - return { - "username": userName, - "password": password, - "fireBaseToken": FirebaseNotificationManger?.token ?? "", - }; + String notificationType = (Platform.isAndroid && !(await FirebaseNotificationManger.isGoogleServicesAvailable())) ? "HMC" : "FCM"; + return {"username": userName, "password": password, "fireBaseToken": FirebaseNotificationManger?.token ?? "", "notificationType": notificationType}; } UsersTypes get type { diff --git a/pubspec.lock b/pubspec.lock index 82a0a0ba..52fc57b1 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -593,10 +593,10 @@ packages: dependency: "direct main" description: name: huawei_push - sha256: "30b4dcab270d81e2c58624e044707886c487d6cb80287db9be629d894e439e40" + sha256: "94c70987591442e301745e881ee2768ca5349800294bb7bdd46f9f80d1096c53" url: "https://pub.dev" source: hosted - version: "6.12.0+301" + version: "6.12.0+302" image: dependency: transitive description: diff --git a/pubspec.yaml b/pubspec.yaml index 6ac7d40a..56636f6b 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -79,7 +79,7 @@ dependencies: syncfusion_flutter_charts: ^21.2.3 local_auth: ^2.1.6 google_api_availability: ^5.0.0 - huawei_push: ^6.5.0+300 + huawei_push: ^6.12.0+302 dev_dependencies: flutter_test: