From b3f6533224dbad8bf78f8093e518ad8a6878219e Mon Sep 17 00:00:00 2001 From: KambleSonam Date: Thu, 25 Apr 2024 13:56:48 +0530 Subject: [PATCH 1/7] Adding js package --- example/pubspec.yaml | 2 +- lib/clevertap_plugin_web.dart | 48 ++++++++++++----------- lib/src/clevertap_plugin_web_binding.dart | 26 ++++++------ pubspec.yaml | 1 + 4 files changed, 40 insertions(+), 37 deletions(-) diff --git a/example/pubspec.yaml b/example/pubspec.yaml index 5eaa3bb..b71dd89 100644 --- a/example/pubspec.yaml +++ b/example/pubspec.yaml @@ -14,7 +14,7 @@ description: A CleverTap Flutter Example project. version: 1.0.0+1 environment: - sdk: '>=3.2.0-0 <4.0.0' + sdk: '>=2.12.0 <4.0.0' dependencies: flutter_styled_toast: ^2.0.1 diff --git a/lib/clevertap_plugin_web.dart b/lib/clevertap_plugin_web.dart index bdab6ee..9f84d9a 100644 --- a/lib/clevertap_plugin_web.dart +++ b/lib/clevertap_plugin_web.dart @@ -1,11 +1,11 @@ import 'dart:async'; -import 'dart:js_interop'; -import 'dart:js_util'; -import 'dart:html' as html; import 'package:clevertap_plugin/src/clevertap_plugin_web_binding.dart'; import 'package:flutter/services.dart'; import 'package:flutter_web_plugins/flutter_web_plugins.dart'; +import 'package:js/js_util.dart' as js_util; +import 'package:js/js.dart'; +import 'dart:html' as html; /// A web implementation of the CleverTapPlugin plugin. class CleverTapPlugin { @@ -147,47 +147,47 @@ class CleverTapPlugin { void _toggleInbox(MethodCall call) { Map args = call.arguments as Map; - toggleInbox(jsify({'rect': args['rect']})); + toggleInbox(js_util.jsify({'rect': args['rect']})); } /// Pushes a basic event void _recordEvent(MethodCall call) { Map args = call.arguments as Map; String eventName = args['eventName'] as String; - Object eventData = args['eventData'] ?? {}; - event_push(eventName, jsify(eventData)); + Object? eventData = args['eventData']; + event_push(eventName, js_util.jsify(eventData ?? {})); } /// OnUserLogin request void _onUserLogin(MethodCall call) { Map args = call.arguments as Map; - onUserLogin_push(jsify({"Site": args['profile']})); + onUserLogin_push(js_util.jsify({"Site": args['profile']})); } /// enable web push void _enableWebPush(MethodCall call) { Map args = call.arguments as Map; - notifications_push(jsify(args)); + notifications_push(js_util.jsify(args)); } /// Profile push request void _profileSet(MethodCall call) { Map args = call.arguments as Map; - onUserLogin_push(jsify({"Site": args['profile']})); + onUserLogin_push(js_util.jsify({"Site": args['profile']})); } /// Set Optout flag void _setOptOut(MethodCall call) { Map args = call.arguments as Map; bool value = args['value'] as bool; - privacy_push(jsify({"optOut": value})); + privacy_push(js_util.jsify({"optOut": value})); } /// Set useIP flag void _setUseIP(MethodCall call) { Map args = call.arguments as Map; bool value = args['value'] as bool; - privacy_push(jsify({"useIP": value})); + privacy_push(js_util.jsify({"useIP": value})); } /// Set Log Level @@ -290,7 +290,8 @@ class CleverTapPlugin { Map args = call.arguments as Map; String msgId = args['msgId'] as String; String pivotId = args['pivotId'] as String; - renderNotificationViewed(jsify({"msgId": msgId, "pivotId": pivotId})); + renderNotificationViewed( + js_util.jsify({"msgId": msgId, "pivotId": pivotId})); } /// Method for notification clicked @@ -298,7 +299,8 @@ class CleverTapPlugin { Map args = call.arguments as Map; String msgId = args['msgId'] as String; String pivotId = args['pivotId'] as String; - renderNotificationClicked(jsify({"msgId": msgId, "pivotId": pivotId})); + renderNotificationClicked( + js_util.jsify({"msgId": msgId, "pivotId": pivotId})); } /// Get total inbox message count @@ -313,19 +315,19 @@ class CleverTapPlugin { /// Get All Inbox Messages List _getAllInboxMessages(MethodCall call) { - return List.from((getAllInboxMessages().dartify() as Map).values); + return List.from((js_util.dartify(getAllInboxMessages()) as Map).values); } /// Get All Inbox Unread Messages List _getUnreadInboxMessages(MethodCall call) { - return List.from((getUnreadInboxMessages().dartify() as Map).values); + return List.from((js_util.dartify(getUnreadInboxMessages()) as Map).values); } /// Get Inbox Message for the given message-id - Map _getInboxMessageForId(MethodCall call) { + Object _getInboxMessageForId(MethodCall call) { Map args = call.arguments as Map; String messageId = args['messageId'] as String; - return getInboxMessageForId(messageId).dartify() as Map; + return (js_util.dartify(getInboxMessageForId(messageId)) as Map); } /// Delete Message for the given message-id @@ -356,7 +358,7 @@ class CleverTapPlugin { void _defineVariables(MethodCall call) { Map args = call.arguments as Map; Object variables = args['variables'] as Object; - defineVariables(jsify(variables)); + defineVariables(js_util.jsify(variables)); } /// Sync Variables @@ -378,21 +380,21 @@ class CleverTapPlugin { name, allowInterop((object) => { _nativeToDartMethodChannel?.invokeMethod( - 'onValueChanged', dartify(object)) + 'onValueChanged', js_util.dartify(object)) })); } void _onVariablesChanged(MethodCall call) { onVariablesChanged(allowInterop((object) => { _nativeToDartMethodChannel?.invokeMethod( - 'onVariablesChanged', dartify(object)) + 'onVariablesChanged', js_util.dartify(object)) })); } Future> _getVariables(MethodCall call) async { var completer = Completer>(); getVariables(allowInterop((object) => - completer.complete(dartify(object) as Map))); + completer.complete(js_util.dartify(object) as Map))); return completer.future; } @@ -400,8 +402,8 @@ class CleverTapPlugin { Map args = call.arguments as Map; String name = args['name'] as String; var completer = Completer(); - getVariable( - name, allowInterop((object) => completer.complete(dartify(object)))); + getVariable(name, + allowInterop((object) => completer.complete(js_util.dartify(object)))); return completer.future; } } diff --git a/lib/src/clevertap_plugin_web_binding.dart b/lib/src/clevertap_plugin_web_binding.dart index 17d001d..93565f5 100644 --- a/lib/src/clevertap_plugin_web_binding.dart +++ b/lib/src/clevertap_plugin_web_binding.dart @@ -1,7 +1,7 @@ @JS("clevertap") library clevertap; -import 'dart:js_interop'; +import 'package:js/js.dart'; @JS('init') external void init( @@ -14,22 +14,22 @@ external void setLibrary( ); @JS('toggleInbox') -external void toggleInbox(JSObject object); +external void toggleInbox(Object object); @JS('event.push') -external void event_push(String event, JSObject? object); +external void event_push(String event, Object? object); @JS('onUserLogin.push') -external void onUserLogin_push(JSObject object); +external void onUserLogin_push(Object object); @JS('notifications.push') -external void notifications_push(JSObject object); +external void notifications_push(Object object); @JS('profile.push') -external void profile_push(JSObject object); +external void profile_push(Object object); @JS('privacy.push') -external void privacy_push(JSObject object); +external void privacy_push(Object object); @JS('setLogLevel') external void setLogLevel(int value); @@ -71,10 +71,10 @@ external void handleDecrementValue(String key, num value); external void getLocation(double latitude, double longitude); @JS('renderNotificationViewed') -external void renderNotificationViewed(JSObject object); +external void renderNotificationViewed(Object object); @JS('renderNotificationViewed') -external void renderNotificationClicked(JSObject object); +external void renderNotificationClicked(Object object); @JS('getInboxMessageCount') external int getInboxMessageCount(); @@ -83,13 +83,13 @@ external int getInboxMessageCount(); external int getInboxMessageUnreadCount(); @JS('getAllInboxMessages') -external JSAny getAllInboxMessages(); +external Map getAllInboxMessages(); @JS('getUnreadInboxMessages') -external JSAny getUnreadInboxMessages(); +external Map getUnreadInboxMessages(); @JS('getInboxMessageForId') -external JSAny getInboxMessageForId(String messageId); +external Object getInboxMessageForId(String messageId); @JS('deleteInboxMessage') external void deleteInboxMessage(String messageId); @@ -104,7 +104,7 @@ external void markReadAllInboxMessage(); external void markReadInboxMessagesForIds(List messageIds); @JS('defineVariables') -external void defineVariables(JSObject object); +external void defineVariables(Object object); @JS('syncVariables') external void syncVariables(); diff --git a/pubspec.yaml b/pubspec.yaml index 260195e..fd889d9 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -12,6 +12,7 @@ dependencies: sdk: flutter flutter_web_plugins: sdk: flutter + js: ">=0.6.5" dev_dependencies: flutter_test: From c388ad1035c552a2de71501ab9661b37d2c6db6b Mon Sep 17 00:00:00 2001 From: KambleSonam Date: Mon, 6 May 2024 18:00:32 +0530 Subject: [PATCH 2/7] Added recordChargedEvent method for web --- example/lib/main.dart | 6 +----- lib/clevertap_plugin_web.dart | 10 ++++++++++ 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/example/lib/main.dart b/example/lib/main.dart index fd3de61..4120988 100644 --- a/example/lib/main.dart +++ b/example/lib/main.dart @@ -1731,11 +1731,7 @@ class _MyAppState extends State { 'total': '200', 'payment': 'cash' }; - if (kIsWeb) { - CleverTapPlugin.recordEvent("Charged", chargeDetails); - } else { - CleverTapPlugin.recordChargedEvent(chargeDetails, items); - } + CleverTapPlugin.recordChargedEvent(chargeDetails, items); showToast("Raised event - Charged"); } diff --git a/lib/clevertap_plugin_web.dart b/lib/clevertap_plugin_web.dart index 9f84d9a..fef1122 100644 --- a/lib/clevertap_plugin_web.dart +++ b/lib/clevertap_plugin_web.dart @@ -48,6 +48,8 @@ class CleverTapPlugin { return _toggleInbox(call); case 'recordEvent': return _recordEvent(call); + case 'recordChargedEvent': + return _recordChargedEvent(call); case 'onUserLogin': return _onUserLogin(call); case 'profileSet': @@ -158,6 +160,14 @@ class CleverTapPlugin { event_push(eventName, js_util.jsify(eventData ?? {})); } + /// Pushed a Charged event + void _recordChargedEvent(MethodCall call) { + Map args = call.arguments as Map; + Map chargeDetails = args['chargeDetails'] as Map; + chargeDetails["Items"] = args['items'] as List; + event_push("Charged", js_util.jsify(chargeDetails)); + } + /// OnUserLogin request void _onUserLogin(MethodCall call) { Map args = call.arguments as Map; From 9ba23e63b9d8fa8fc76cc0d14cf9464cb83762e1 Mon Sep 17 00:00:00 2001 From: KambleSonam Date: Tue, 7 May 2024 13:34:25 +0530 Subject: [PATCH 3/7] js package version update --- pubspec.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pubspec.yaml b/pubspec.yaml index fd889d9..345087a 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -12,7 +12,7 @@ dependencies: sdk: flutter flutter_web_plugins: sdk: flutter - js: ">=0.6.5" + js: ">=0.6.4" dev_dependencies: flutter_test: From 66369f42bdddb7cd8bdcaa4e4b8920de121b1f32 Mon Sep 17 00:00:00 2001 From: KambleSonam Date: Tue, 7 May 2024 13:42:03 +0530 Subject: [PATCH 4/7] js package version update --- pubspec.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pubspec.yaml b/pubspec.yaml index 345087a..fd889d9 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -12,7 +12,7 @@ dependencies: sdk: flutter flutter_web_plugins: sdk: flutter - js: ">=0.6.4" + js: ">=0.6.5" dev_dependencies: flutter_test: From c958d6c559dedf0d7fffe94ba0cdc6894c17cbd3 Mon Sep 17 00:00:00 2001 From: KambleSonam Date: Thu, 9 May 2024 12:17:50 +0530 Subject: [PATCH 5/7] Changelog and version update --- CHANGELOG.md | 10 ++++++++++ README.md | 2 +- android/build.gradle | 2 +- ios/clevertap_plugin.podspec | 2 +- lib/clevertap_plugin.dart | 2 +- pubspec.yaml | 2 +- 6 files changed, 15 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 97beacf..b85339e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,15 @@ ## CHANGE LOG +### Version 2.3.2 *(9th May 2024)* +------------------------------------------- +**What's new** +* **[Web Platform]** + * Added the method 'recordChargedEvent' for web + +**Bug Fixes** +* **[Web Platform]** + * Added [JS package](https://pub.dev/packages/js) dependency to handle latest versions. + ### Version 2.3.1 *(19th April 2024)* ------------------------------------------- **Bug Fixes** diff --git a/README.md b/README.md index ce6325c..c999b2f 100644 --- a/README.md +++ b/README.md @@ -21,7 +21,7 @@ To get started, sign up [here](https://clevertap.com/live-product-demo/). ```yaml dependencies: -clevertap_plugin: 2.3.1 +clevertap_plugin: 2.3.2 ``` - Run `flutter packages get` to install the SDK diff --git a/android/build.gradle b/android/build.gradle index 99b0a3a..3291c63 100644 --- a/android/build.gradle +++ b/android/build.gradle @@ -3,7 +3,7 @@ plugins { } group 'com.clevertap.clevertap_plugin' -version '2.3.1' +version '2.3.2' rootProject.allprojects { repositories { diff --git a/ios/clevertap_plugin.podspec b/ios/clevertap_plugin.podspec index 45d4324..1f73b09 100644 --- a/ios/clevertap_plugin.podspec +++ b/ios/clevertap_plugin.podspec @@ -3,7 +3,7 @@ # Pod::Spec.new do |s| s.name = 'clevertap_plugin' - s.version = '2.3.1' + s.version = '2.3.2' s.summary = 'CleverTap Flutter plugin.' s.description = 'The CleverTap iOS SDK for App Analytics and Engagement.' s.homepage = 'https://github.com/CleverTap/clevertap-ios-sdk' diff --git a/lib/clevertap_plugin.dart b/lib/clevertap_plugin.dart index b66b8e8..ff069af 100644 --- a/lib/clevertap_plugin.dart +++ b/lib/clevertap_plugin.dart @@ -60,7 +60,7 @@ class CleverTapPlugin { static const libName = 'Flutter'; static const libVersion = - 20301; // If the current version is X.X.X then pass as X0X0X + 20302; // If the current version is X.X.X then pass as X0X0X CleverTapPlugin._internal() { /// Set the CleverTap Flutter library name and the current version for version tracking diff --git a/pubspec.yaml b/pubspec.yaml index fd889d9..290f338 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,6 +1,6 @@ name: clevertap_plugin description: The CleverTap Flutter SDK for Mobile Customer Engagement,Analytics and Retention solutions. -version: 2.3.1 +version: 2.3.2 homepage: https://github.com/CleverTap/clevertap-flutter environment: From de0ac8a7904af78d6905d169a2152a8a340982f8 Mon Sep 17 00:00:00 2001 From: Anush-Shand <127097095+Anush-Shand@users.noreply.github.com> Date: Fri, 10 May 2024 12:27:55 +0530 Subject: [PATCH 6/7] Task/sdk 3803/callbacks not working with workmanager (#235) * task(SDK-3803) - Fixes callbackissue with work manager - Maintains a set to dispatch native to dart callbacks - Adds required logs * task(SDK-3803) - Integrates workmanager and firebasemessaging into the sample app to ease testing for future as well * task(SDK-3723) - Updates CHANGELOG.md * task(SDK-3723) - Updates CHANGELOG.md * task(SDK-3803) - Changes name of field * task(SDK-3803) - Updates release related files --------- Co-authored-by: KambleSonam <98796470+KambleSonam@users.noreply.github.com> --- CHANGELOG.md | 5 +- README.md | 2 +- android/build.gradle | 2 +- .../clevertap_plugin/CleverTapPlugin.java | 85 +++++++++++-------- example/android/app/build.gradle | 10 +-- example/lib/main.dart | 45 +++++++++- example/pubspec.yaml | 3 + ios/clevertap_plugin.podspec | 2 +- lib/clevertap_plugin.dart | 2 +- pubspec.yaml | 2 +- 10 files changed, 109 insertions(+), 49 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b85339e..eb0f47a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,6 @@ ## CHANGE LOG -### Version 2.3.2 *(9th May 2024)* +### Version 2.4.0 *(10th May 2024)* ------------------------------------------- **What's new** * **[Web Platform]** @@ -10,6 +10,9 @@ * **[Web Platform]** * Added [JS package](https://pub.dev/packages/js) dependency to handle latest versions. +* **[Android Platform]** + * Fixes [#114](https://github.com/CleverTap/clevertap-flutter/issues/114) - an issue related to callbacks from native to dart when a 3rd party plugin like `flutter_workmanager` is used. + ### Version 2.3.1 *(19th April 2024)* ------------------------------------------- **Bug Fixes** diff --git a/README.md b/README.md index c999b2f..175ac17 100644 --- a/README.md +++ b/README.md @@ -21,7 +21,7 @@ To get started, sign up [here](https://clevertap.com/live-product-demo/). ```yaml dependencies: -clevertap_plugin: 2.3.2 +clevertap_plugin: 2.4.0 ``` - Run `flutter packages get` to install the SDK diff --git a/android/build.gradle b/android/build.gradle index 3291c63..aa395dc 100644 --- a/android/build.gradle +++ b/android/build.gradle @@ -3,7 +3,7 @@ plugins { } group 'com.clevertap.clevertap_plugin' -version '2.3.2' +version '2.4.0' rootProject.allprojects { repositories { diff --git a/android/src/main/java/com/clevertap/clevertap_plugin/CleverTapPlugin.java b/android/src/main/java/com/clevertap/clevertap_plugin/CleverTapPlugin.java index a37c254..ba94915 100644 --- a/android/src/main/java/com/clevertap/clevertap_plugin/CleverTapPlugin.java +++ b/android/src/main/java/com/clevertap/clevertap_plugin/CleverTapPlugin.java @@ -46,6 +46,8 @@ import com.clevertap.clevertap_plugin.CleverTapTypeUtils.LongUtil; import com.clevertap.clevertap_plugin.isolate.IsolateHandlePreferences; +import java.util.HashSet; +import java.util.Set; import org.json.JSONException; import org.json.JSONObject; @@ -93,13 +95,14 @@ public class CleverTapPlugin implements ActivityAware, private MethodChannel dartToNativeMethodChannel; - private static MethodChannel nativeToDartMethodChannel; + private MethodChannel lastNativeToDartMethodChannel; private CleverTapAPI cleverTapAPI; private Context context; public static Map variables = new HashMap<>(); + public static Set nativeToDartMethodChannelSet = new HashSet<>(); /** * Plugin registration. @@ -153,6 +156,7 @@ public void onAttachedToActivity(@NonNull ActivityPluginBinding binding) { @Override public void onAttachedToEngine(@NonNull FlutterPluginBinding binding) { + Log.d(TAG,"onAttachedToEngine"); setupPlugin(binding.getApplicationContext(), binding.getBinaryMessenger(), null); } @@ -168,8 +172,10 @@ public void onDetachedFromActivityForConfigChanges() { @Override public void onDetachedFromEngine(@NonNull FlutterPluginBinding binding) { + Log.d(TAG,"onDetachedFromEngine"); + nativeToDartMethodChannelSet.remove(this.lastNativeToDartMethodChannel); + this.lastNativeToDartMethodChannel = null; dartToNativeMethodChannel = null; - nativeToDartMethodChannel = null; context = null; } @@ -1395,49 +1401,55 @@ private void initializeInbox(Result result) { } private void invokeMethodOnUiThread(final String methodName, final String cleverTapID) { - final MethodChannel channel = nativeToDartMethodChannel; - if (channel == null) { - Log.d(TAG, "methodChannel in invokeMethodOnUiThread(String) is null"); - return; - } - runOnMainThread(() -> { - if (!cleverTapID.isEmpty()) { - channel.invokeMethod(methodName, cleverTapID); - } else { - channel.invokeMethod(methodName, null); + Log.d(TAG, "methodChannelSet in invokeMethodOnUiThread(String) is of size " + nativeToDartMethodChannelSet.size()); + + for(MethodChannel channel : nativeToDartMethodChannelSet) { + if (channel != null) { + Log.d(TAG, "methodChannelSet in invokeMethodOnUiThread(String) " + channel); + runOnMainThread(() -> { + if (!cleverTapID.isEmpty()) { + channel.invokeMethod(methodName, cleverTapID); + } else { + channel.invokeMethod(methodName, null); + } + }); } - }); + } } @SuppressWarnings("SameParameterValue") private void invokeMethodOnUiThread(final String methodName, final boolean params) { - final MethodChannel channel = nativeToDartMethodChannel; - if (channel == null) { - Log.d(TAG, "params in invokeMethodOnUiThread(boolean) is null"); - return; + Log.d(TAG, "methodChannelSet in invokeMethodOnUiThread(boolean) is of size" + nativeToDartMethodChannelSet.size()); + + for(MethodChannel channel : nativeToDartMethodChannelSet) { + if (channel != null) { + Log.d(TAG, "methodChannelSet in invokeMethodOnUiThread(boolean) " + channel); + runOnMainThread(() -> channel.invokeMethod(methodName, params)); + } } - runOnMainThread(() -> { - channel.invokeMethod(methodName, params); - }); } private void invokeMethodOnUiThread(final String methodName, final Map map) { - final MethodChannel channel = nativeToDartMethodChannel; - if (channel == null) { - Log.d(TAG, "methodChannel in invokeMethodOnUiThread(Map) is null"); - return; + Log.d(TAG, "methodChannelSet in invokeMethodOnUiThread(Map) is of size " + nativeToDartMethodChannelSet.size()); + + for(MethodChannel channel : nativeToDartMethodChannelSet) { + if (channel != null) { + Log.d(TAG, "methodChannel in invokeMethodOnUiThread(Map) " + channel); + runOnMainThread(() -> channel.invokeMethod(methodName, map)); + } } - runOnMainThread(() -> channel.invokeMethod(methodName, map)); } @SuppressWarnings("SameParameterValue") private void invokeMethodOnUiThread(final String methodName, final ArrayList list) { - final MethodChannel channel = nativeToDartMethodChannel; - if (channel == null) { - Log.d(TAG, "methodChannel in invokeMethodOnUiThread(ArrayList) is null"); - return; + Log.d(TAG, "methodChannelSet in invokeMethodOnUiThread(ArrayList) is of size " + nativeToDartMethodChannelSet.size()); + + for(MethodChannel channel : nativeToDartMethodChannelSet) { + if (channel != null) { + Log.d(TAG, "methodChannel in invokeMethodOnUiThread(ArrayList)" + channel); + runOnMainThread(() -> channel.invokeMethod(methodName, list)); + } } - runOnMainThread(() -> channel.invokeMethod(methodName, list)); } private boolean isCleverTapNotNull(CleverTapAPI cleverTapAPI) { @@ -1880,13 +1892,12 @@ private MethodChannel getMethodChannel(String channelName, BinaryMessenger messe private void setupPlugin(Context context, BinaryMessenger messenger, Registrar registrar) { this.dartToNativeMethodChannel = getMethodChannel("clevertap_plugin/dart_to_native", messenger, registrar); - if (nativeToDartMethodChannel == null) { - // set nativeToDartMethodChannel channel once and it has to be static field - // as per https://github.com/firebase/flutterfire/issues/9689 because multiple - // instances of the CleverTap plugin can be created in case onBackgroundMessage handler - // of FCM plugin. - nativeToDartMethodChannel = getMethodChannel("clevertap_plugin/native_to_dart", messenger, registrar); - } + + // lastNativeToDartMethodChannel is added to a set and not kept as a static field to ensure callbacks work when a background isolate is spawned + // Background Isolates are spawned by several libraries like flutter_workmanager and flutter_firebasemessaging + lastNativeToDartMethodChannel = getMethodChannel("clevertap_plugin/native_to_dart", messenger, registrar); + nativeToDartMethodChannelSet.add(lastNativeToDartMethodChannel); + this.dartToNativeMethodChannel.setMethodCallHandler(this); this.context = context.getApplicationContext(); this.cleverTapAPI = CleverTapAPI.getDefaultInstance(this.context); diff --git a/example/android/app/build.gradle b/example/android/app/build.gradle index cf0e6d8..fb59186 100644 --- a/example/android/app/build.gradle +++ b/example/android/app/build.gradle @@ -55,13 +55,13 @@ dependencies { //implementation fileTree('libs') implementation "com.clevertap.android:push-templates:1.2.3" implementation 'com.google.firebase:firebase-messaging:23.4.1' - implementation 'androidx.core:core:1.3.0' - implementation 'androidx.fragment:fragment:1.3.6' + implementation 'androidx.core:core:1.13.1' + implementation 'androidx.fragment:fragment:1.7.0' //MANDATORY for App Inbox - implementation 'androidx.appcompat:appcompat:1.3.1' - implementation 'androidx.recyclerview:recyclerview:1.2.1' + implementation 'androidx.appcompat:appcompat:1.6.1' + implementation 'androidx.recyclerview:recyclerview:1.3.2' implementation 'androidx.viewpager:viewpager:1.0.0' - implementation 'com.google.android.material:material:1.4.0' + implementation 'com.google.android.material:material:1.11.0' implementation 'com.github.bumptech.glide:glide:4.12.0' //Optional ExoPlayer Libraries for Audio/Video Inbox Messages. Audio/Video messages will be dropped without these dependencies diff --git a/example/lib/main.dart b/example/lib/main.dart index 4120988..03b885b 100644 --- a/example/lib/main.dart +++ b/example/lib/main.dart @@ -1,13 +1,16 @@ import 'dart:async'; import 'dart:convert'; -import 'dart:io' show Platform; +import 'dart:io' show Platform, sleep; import 'package:example/notification_button.dart'; +import 'package:firebase_core/firebase_core.dart'; import 'package:flutter/foundation.dart' show kIsWeb; import 'package:clevertap_plugin/clevertap_plugin.dart'; import 'package:example/deeplink_page.dart'; import 'package:flutter/material.dart'; import 'package:flutter_styled_toast/flutter_styled_toast.dart'; +import 'package:workmanager/workmanager.dart'; +import 'package:firebase_messaging/firebase_messaging.dart'; @pragma('vm:entry-point') void onKilledStateNotificationClickedHandler(Map map) async { @@ -15,8 +18,47 @@ void onKilledStateNotificationClickedHandler(Map map) async { print("Notification Payload received: " + map.toString()); } +@pragma('vm:entry-point') // Mandatory if the App is obfuscated or using Flutter 3.1+ +void callbackDispatcher() { + // This is a dummy work manager to test usecases with background isolates + + Workmanager().executeTask((task, inputData) { + print("Native started background task: $task"); + sleep(Duration(seconds: 30)); + print("Native called background task: $task"); //simpleTask will be emitted here. + return Future.value(true); + }); +} + +Future _firebaseBackgroundMessageHandler(RemoteMessage message) async { + // This is a dummy firebase integration to test usecases with background isolates + await Firebase.initializeApp(); + print("_firebaseBackgroundMessageHandler Background"); + // CleverTapPlugin.createNotification(jsonEncode(message.data)); +} + +/// Handles foreground messages of FCM +void _firebaseForegroundMessageHandler(RemoteMessage remoteMessage) { + print('_firebaseForegroundMessageHandler called'); + // CleverTapPlugin.createNotification(jsonEncode(remoteMessage.data)); +} + + void main() async { WidgetsFlutterBinding.ensureInitialized(); + Workmanager().initialize( + callbackDispatcher, // The top level function, aka callbackDispatcher + isInDebugMode: true // If enabled it will post a notification whenever the task is running. Handy for debugging tasks + ); + Workmanager().registerOneOffTask( + "periodic-task-identifier", + "simplePeriodicTask" + ); + + await Firebase.initializeApp(); + FirebaseMessaging.onMessage.listen(_firebaseForegroundMessageHandler); + FirebaseMessaging.onBackgroundMessage(_firebaseBackgroundMessageHandler); + CleverTapPlugin.onKilledStateNotificationClicked( onKilledStateNotificationClickedHandler); runApp(MaterialApp( @@ -53,6 +95,7 @@ class _MyAppState extends State { @override void initState() { + print("initState"); super.initState(); initPlatformState(); activateCleverTapFlutterPluginHandlers(); diff --git a/example/pubspec.yaml b/example/pubspec.yaml index b71dd89..e37bad8 100644 --- a/example/pubspec.yaml +++ b/example/pubspec.yaml @@ -24,6 +24,9 @@ dependencies: # The following adds the Cupertino Icons font to your application. # Use with the CupertinoIcons class for iOS style icons. cupertino_icons: ^1.0.3 + workmanager: ^0.5.2 + firebase_messaging: ^14.9.2 + firebase_core: ^2.31.0 dev_dependencies: flutter_test: diff --git a/ios/clevertap_plugin.podspec b/ios/clevertap_plugin.podspec index 1f73b09..f346743 100644 --- a/ios/clevertap_plugin.podspec +++ b/ios/clevertap_plugin.podspec @@ -3,7 +3,7 @@ # Pod::Spec.new do |s| s.name = 'clevertap_plugin' - s.version = '2.3.2' + s.version = '2.4.0' s.summary = 'CleverTap Flutter plugin.' s.description = 'The CleverTap iOS SDK for App Analytics and Engagement.' s.homepage = 'https://github.com/CleverTap/clevertap-ios-sdk' diff --git a/lib/clevertap_plugin.dart b/lib/clevertap_plugin.dart index ff069af..62e349a 100644 --- a/lib/clevertap_plugin.dart +++ b/lib/clevertap_plugin.dart @@ -60,7 +60,7 @@ class CleverTapPlugin { static const libName = 'Flutter'; static const libVersion = - 20302; // If the current version is X.X.X then pass as X0X0X + 20400; // If the current version is X.X.X then pass as X0X0X CleverTapPlugin._internal() { /// Set the CleverTap Flutter library name and the current version for version tracking diff --git a/pubspec.yaml b/pubspec.yaml index 290f338..9f1e343 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,6 +1,6 @@ name: clevertap_plugin description: The CleverTap Flutter SDK for Mobile Customer Engagement,Analytics and Retention solutions. -version: 2.3.2 +version: 2.4.0 homepage: https://github.com/CleverTap/clevertap-flutter environment: From 0272ad6719bc1d3ed33fca1f13713c8e663b2d70 Mon Sep 17 00:00:00 2001 From: Anush-Shand <127097095+Anush-Shand@users.noreply.github.com> Date: Fri, 10 May 2024 12:57:41 +0530 Subject: [PATCH 7/7] task(SDK-3803) - Changes access modifier of set (#238) --- .../java/com/clevertap/clevertap_plugin/CleverTapPlugin.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/android/src/main/java/com/clevertap/clevertap_plugin/CleverTapPlugin.java b/android/src/main/java/com/clevertap/clevertap_plugin/CleverTapPlugin.java index ba94915..4262298 100644 --- a/android/src/main/java/com/clevertap/clevertap_plugin/CleverTapPlugin.java +++ b/android/src/main/java/com/clevertap/clevertap_plugin/CleverTapPlugin.java @@ -102,7 +102,7 @@ public class CleverTapPlugin implements ActivityAware, private Context context; public static Map variables = new HashMap<>(); - public static Set nativeToDartMethodChannelSet = new HashSet<>(); + private static final Set nativeToDartMethodChannelSet = new HashSet<>(); /** * Plugin registration.