flyy_flutter_plugin 2.0.0
flyy_flutter_plugin: ^2.0.0 copied to clipboard
Flutter wrapper around our Android and iOS mobile SDKs
example/lib/main.dart
import 'dart:async';
import 'dart:io';
import 'package:firebase_core/firebase_core.dart';
import 'package:firebase_messaging/firebase_messaging.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flyy_flutter_plugin/flyy_flutter_plugin.dart';
//comes here when app is in background
//handle notification as per the platform
Future<void> _messageHandler(RemoteMessage remoteMessage) async {
print('background message ${remoteMessage.notification!.body}');
if (Platform.isAndroid) {
//for android
handleAndroidNotification(remoteMessage);
} else if (Platform.isIOS) {
//for ios
handleiOSNotification(remoteMessage);
}
}
//handle flyy android notifications
handleAndroidNotification(RemoteMessage remoteMessage) {
print('background message ${remoteMessage.notification!.body}');
if (remoteMessage.data.containsKey("notification_source") &&
remoteMessage.data["notification_source"] != null &&
remoteMessage.data["notification_source"] == "flyy_sdk") {
FlyyFlutterPlugin.handleNotification(remoteMessage.data);
}
}
//handle flyy ios notifications
handleiOSNotification(RemoteMessage remoteMessage) {
print('background message ${remoteMessage.notification!.body}');
if (remoteMessage.data.containsKey("notification_source") &&
remoteMessage.data["notification_source"] != null &&
remoteMessage.data["notification_source"] == "flyy_sdk") {
FlyyFlutterPlugin.handleBackgroundNotification(remoteMessage.data);
}
}
Future<void> main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp();
FirebaseMessaging.onBackgroundMessage(_messageHandler);
runApp(MyApp());
}
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
late FirebaseMessaging firebaseMessaging;
late FlyyFlutterPlugin flyy;
@override
void initState() {
super.initState();
flyy = FlyyFlutterPlugin();
flyy.on(FlyyFlutterPlugin.FLYY_ON_SDK_CLOSED_LISTENER, onFlyySdkClosed);
flyy.on(
FlyyFlutterPlugin.FLYY_INIT_WITH_REFERRAL_CALLBACK, onReferralSuccess);
FlyyFlutterPlugin.setPackageName("<package_name_same_as_flyy_dashboard>");
initFlyySdk();
setupFirebase();
}
//do your work here when flyy sdk is closed
void onFlyySdkClosed(String response) {
if (response != null) {
getWalletBalance();
}
}
void onReferralSuccess(String referralData) {
if (referralData != null) {
FlyyFlutterPlugin.verifyReferralCode(referralData);
}
}
getWalletBalance() async {
try {
Map<String, dynamic> mapShareData =
await FlyyFlutterPlugin.getWalletBalance("<your_currency>");
//To get result
print(mapShareData["balance"]);
print(mapShareData["total_credit"]);
print(mapShareData["total_debit"]);
} on PlatformException catch (e) {
print(e.message);
}
}
@override
void dispose() {
super.dispose();
flyy.clearEventListeners();
}
setupFirebase() async {
// await Firebase.initializeApp();
// Code added to display user authorization for notifications
FirebaseMessaging messaging = FirebaseMessaging.instance;
NotificationSettings settings = await messaging.requestPermission(
alert: true,
announcement: false,
badge: true,
carPlay: false,
criticalAlert: false,
provisional: false,
sound: true,
);
if (settings.authorizationStatus == AuthorizationStatus.authorized) {
print('User granted permission');
} else if (settings.authorizationStatus ==
AuthorizationStatus.provisional) {
print('User granted provisional permission');
} else {
print('User declined or has not accepted permission');
}
firebaseMessaging = FirebaseMessaging.instance;
firebaseMessaging.getToken().then((value) {
print("FCM TOKEN :- $value");
//send fcm token to server only for ios
FlyyFlutterPlugin.sendFCMTokenToServer(value!);
});
//comes here when app is in foreground
FirebaseMessaging.onMessage.listen((RemoteMessage remoteMessage) {
print("message received");
print(remoteMessage.data);
if (Platform.isAndroid) {
handleAndroidNotification(remoteMessage);
} else if (Platform.isIOS) {
handleiOSNotification(remoteMessage);
}
});
//comes here when clicked from notification bar
FirebaseMessaging.onMessageOpenedApp.listen((remoteMessage) {
print('Message clicked!');
if (Platform.isAndroid) {
//do nothing for android
} else if (Platform.isIOS) {
handleiOSNotification(remoteMessage);
}
});
}
//handle flyy android notifications
handleAndroidNotification(RemoteMessage remoteMessage) {
print('background message ${remoteMessage.notification!.body}');
if (remoteMessage.data.containsKey("notification_source") &&
remoteMessage.data["notification_source"] != null &&
remoteMessage.data["notification_source"] == "flyy_sdk") {
FlyyFlutterPlugin.handleNotification(remoteMessage.data);
}
}
//handle flyy ios notifications
handleiOSNotification(RemoteMessage remoteMessage) {
print('background message ${remoteMessage.notification!.body}');
if (remoteMessage.data.containsKey("notification_source") &&
remoteMessage.data["notification_source"] != null &&
remoteMessage.data["notification_source"] == "flyy_sdk") {
FlyyFlutterPlugin.handleForegroundNotification(remoteMessage.data);
}
}
// Platform messages are asynchronous, so we initialize in an async method.
initFlyySdk() async {
FlyyFlutterPlugin.initFlyySDK("<partner_id>", FlyyFlutterPlugin.PRODUCTION);
FlyyFlutterPlugin.setThemeColor("#cc1d27", "#cc1d27");
FlyyFlutterPlugin.setFlyyUser("<user_id>");
FlyyFlutterPlugin.setFlyyUserName("<user_name>");
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('Plugin example app'),
),
body: Center(
child: Container(
child: Column(
children: [
GestureDetector(
onTap: () {
FlyyFlutterPlugin.openFlyyOffersPage();
// FlyyFlutterPlugin.openFlyyRewardsPage();
// FlyyFlutterPlugin.openFlyyWalletPage();
// FlyyFlutterPlugin.openFlyyGiftCardsPage();
// FlyyFlutterPlugin.openFlyyReferralsPage();
// FlyyFlutterPlugin.openFlyyQuizPage(0);
// FlyyFlutterPlugin.openFlyyQuizHistoryPage();
// FlyyFlutterPlugin.openFlyyQuizListPage();
// FlyyFlutterPlugin.openFlyyInviteDetailsPage(0);
// FlyyFlutterPlugin.openFlyyInviteAndEarnPage(0);
// FlyyFlutterPlugin.openFlyyCustomInviteAndEarnPage(0, "");
// FlyyFlutterPlugin.openFlyyChallengeDetailsPage(0);
// FlyyFlutterPlugin.openFlyyBonanzaPage();
},
child: Padding(
padding: const EdgeInsets.all(20.0),
child: Center(
child: Text('Start Offers Page'),
),
),
),
GestureDetector(
onTap: () {
// FlyyFlutterPlugin.listenFlyySDKClosed();
FlyyFlutterPlugin.openFlyyRewardsPage();
},
child: Padding(
padding: const EdgeInsets.all(20.0),
child: Center(
child: Text('Start Rewards PAge'),
),
),
),
GestureDetector(
onTap: () {
FlyyFlutterPlugin.openFlyyWalletPage();
// FlyyFlutterPlugin.openFlyyReferralsPage();
},
child: Padding(
padding: const EdgeInsets.all(20.0),
child: Center(
child: Text('Start Wallet Page'),
),
),
),
],
),
),
),
),
);
}
}