Created
May 25, 2021 11:54
-
-
Save epynic/297e531c3d98f86d3b3c352bad245d4f to your computer and use it in GitHub Desktop.
minimal clevertap flutter example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'dart:convert'; | |
import 'package:firebase_core/firebase_core.dart'; | |
import 'package:firebase_messaging/firebase_messaging.dart'; | |
import 'package:flutter/material.dart'; | |
import 'package:clevertap_plugin/clevertap_plugin.dart'; | |
void main() async { | |
WidgetsFlutterBinding.ensureInitialized(); | |
await Firebase.initializeApp(); | |
runApp(MyApp()); | |
} | |
// just for fix | |
Future<void> _firebaseMessagingBackgroundHandler(RemoteMessage message) async { | |
print(' ---- FirebaseMessaging.onBackgroundMessage =-' + | |
message.data.toString()); | |
await Firebase.initializeApp(); | |
} | |
class MyApp extends StatefulWidget { | |
@override | |
_MyAppState createState() => _MyAppState(); | |
} | |
class _MyAppState extends State<MyApp> { | |
CleverTapPlugin _clevertapPlugin; | |
var inboxInitialized = false; | |
var optOut = false; | |
var offLine = false; | |
var enableDeviceNetworkingInfo = false; | |
FirebaseMessaging _cloudNotification = FirebaseMessaging.instance; | |
@override | |
void initState() { | |
super.initState(); | |
initPlatformState(); | |
activateCleverTapFlutterPluginHandlers(); | |
CleverTapPlugin.setDebugLevel(3); | |
CleverTapPlugin.createNotificationChannel("bynge_local_notification", | |
"bynge_local_notification", "Bynge CleverTap Channnel", 3, true); | |
CleverTapPlugin.initializeInbox(); | |
CleverTapPlugin.registerForPush(); | |
// var profile = { | |
// 'Name': 'Thor', | |
// 'Identity': '100', | |
// 'DOB': '22-04-2000', | |
// 'Email': 'thor@asgard.com', | |
// }; | |
// CleverTapPlugin.profileSet(profile); | |
initCloudMessaging(); | |
} | |
initCloudMessaging() async { | |
String token = await _cloudNotification.getToken(); | |
print('fcm Token : ' + token); | |
RemoteMessage initialMessage = await _cloudNotification.getInitialMessage(); | |
if (initialMessage?.data != null) { | |
print( | |
'---- FCM initialMessage listen -' + initialMessage.data.toString()); | |
} | |
_cloudNotification.getNotificationSettings(); | |
// foreground episode | |
FirebaseMessaging.onMessage.listen((RemoteMessage message) { | |
print('---- FCM onMessage listen -' + message.data.toString()); | |
// if (message.data != null) consumeCFCM(message.data); | |
}); | |
FirebaseMessaging.onMessageOpenedApp.listen((RemoteMessage message) { | |
print('---- FCM onMessageOpenedApp listen -' + message.data.toString()); | |
}); | |
// background | |
FirebaseMessaging.onBackgroundMessage(_firebaseMessagingBackgroundHandler); | |
} | |
/// Platform messages are asynchronous, so we initialize in an async method. | |
Future<void> initPlatformState() async { | |
if (!mounted) return; | |
} | |
void activateCleverTapFlutterPluginHandlers() { | |
_clevertapPlugin = new CleverTapPlugin(); | |
_clevertapPlugin.setCleverTapPushClickedPayloadReceivedHandler( | |
pushClickedPayloadReceived); | |
_clevertapPlugin.setCleverTapInAppNotificationButtonClickedHandler( | |
inAppNotificationButtonClicked); | |
} | |
void inAppNotificationButtonClicked(Map<String, dynamic> map) { | |
this.setState(() { | |
print("inAppNotificationButtonClicked called = ${map.toString()}"); | |
}); | |
} | |
void pushClickedPayloadReceived(Map<String, dynamic> map) { | |
print("pushClickedPayloadReceived called"); | |
print(map); | |
var data = jsonEncode(map); | |
print("on Push Click Payload = " + data.toString()); | |
} | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
home: Scaffold( | |
body: Center( | |
child: Text('CleverTap Minimal'), | |
), | |
), | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment