rationalowl_flutter 1.0.0
rationalowl_flutter: ^1.0.0 copied to clipboard
Flutter plugin for RationalOwl, a realtime mobile messaging service
RationalOwl Plugin for Flutter #
A Flutter plugin to use the RationalOwl API.
Usage #
To use this plugin, add rationalowl_flutter as a dependency in your pubspec.yaml file.
Getting Started #
- Integrate the Firebase Cloud Messaging (FCM) and Apple Push Notification service (APNs) in your RationalOwl service.
Android #
-
Download the rationalowl-android-1.4.1.aar file.
-
Create the
libsdirectory inandroid/app, then copy therationalowl-android-1.4.1.aarfile.
[android1]
- Set the
dependenciesinandroid/app/build.gradle:
dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')
implementation files('libs/rationalowl-android-1.4.1.aar')
}
- Use Firebase Messaging for background messages.
iOS #
-
Open your project workspace file
ios/Runner.xcworkspacevia Xcode. -
Download the RationalOwl.framework directory.
-
Copy the
RationalOwl.frameworkdirectory to theRunnerproject.
[ios1]
-
Select the
Runnerproject, then select theRunnertarget. -
Open the
Frameworks, Libraries, and Embedded Contentsection, then click on the+button. -
Click the
Add Files...menu item, then choose theRationalOwl.frameworkdirectory.
[ios2]
-
Select the
Signing & Capabilitiestab, then click on the+ Capabilitybutton. -
Add the
Push NotificationsandBackground Modescapabilities. -
In the
Background Modescapability, enable theBackground fetchandRemote notificationsmodes.
[ios3]
-
Select the
Build Phasestab, then open theLink Binary With Librariessection. -
Click on the
+button. -
Choose the
UserNotifications.framework, then clickAdd.
[ios4]
- Open the
Embed Frameworkssection, then select theCopy only when installingcheckbox.
[ios5]
-
Click on the
+button on the left bottom. -
Choose the
Notification Service Extensiontemplate, then clickNext.
[ios6]
- Add the
Product Name, then clickFinish.
[ios7]
Sample Usage #
const iOSAppGroup = 'group.com.rationalowl.flutterexample';
Future<void> _initialize() async {
if (Platform.isAndroid) {
await Firebase.initializeApp(options: DefaultFirebaseOptions.currentPlatform);
FirebaseMessaging.onBackgroundMessage(handleMessage);
}
await initializeNotification();
final MinervaManager minMgr = MinervaManager.getInstance();
if (Platform.isIOS) {
await minMgr.setAppGroup(iOSAppGroup);
}
await minMgr.setMsgListener(RoMessageListener());
}
void main() async {
WidgetsFlutterBinding.ensureInitialized();
await _initialize();
runApp(const Application());
}
class Application extends StatefulWidget {
const Application({super.key});
@override
State<StatefulWidget> createState() => _ApplicationState();
}
class _ApplicationState extends State<Application> with WidgetsBindingObserver {
late final MinervaAppLifecycleObserver _observer;
@override
void initState() {
super.initState();
if (Platform.isIOS) {
_observer = MinervaAppLifecycleObserver();
WidgetsBinding.instance.addObserver(_observer);
} else {
FirebaseMessaging.onMessage.listen(handleMessage);
FirebaseMessaging.instance.onTokenRefresh.listen(handleTokenRefresh);
}
}
@override
void dispose() {
if (Platform.isIOS) {
WidgetsBinding.instance.removeObserver(_observer);
}
super.dispose();
}
@override
Widget build(BuildContext context) {
return const MaterialApp(
home: Scaffold(
body: SafeArea(
child: MainPage(),
),
),
);
}
}
In your Dart code, implement the MessageListener to receive foreground messages in Android and iOS.
class RoMessageListener implements MessageListener {
@override
void onDownstreamMsgReceived(List<Map<String, dynamic>> msgList) {}
@override
void onP2PMsgReceived(List<Map<String, dynamic>> msgList) {
if (msgList.isNotEmpty) {
final latestMessage = Map<String, dynamic>.from(msgList[0]['data']);
showNotification(latestMessage);
}
}
@override
void onPushMsgReceived(List<Map<String, dynamic>> msgList) {
if (msgList.isNotEmpty) {
final latestMessage = Map<String, dynamic>.from(msgList[0]['data']);
showNotification(latestMessage);
}
}
@override
void onSendUpstreamMsgResult(int resultCode, String? resultMsg, String? msgId) {}
@override
void onSendP2PMsgResult(int resultCode, String? resultMsg, String? msgId) {}
}
In your Dart code, implement the handlers to receive the token and background messages in Android.
Future<void> handleTokenRefresh(String token) async {
final MinervaManager minMgr = MinervaManager.getInstance();
minMgr.setDeviceToken(token);
}
@pragma('vm:entry-point')
Future<void> handleMessage(RemoteMessage message) async {
final Map<String, dynamic> data = message.data;
final MinervaManager minMgr = MinervaManager.getInstance();
minMgr.enableNotificationTracking(data: data);
if (!data.containsKey('silent')) {
showNotification(data);
}
}
In your Swift code ios/{Product Name}/NotificationService.swift, implement the UNNotificationServiceExtension to receive background messages in iOS.
class NotificationService: UNNotificationServiceExtension {
private static let appGroup = "group.com.rationalowl.flutterexample"
var contentHandler: ((UNNotificationContent) -> Void)?
var bestAttemptContent: UNMutableNotificationContent?
override func didReceive(_ request: UNNotificationRequest,
withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void) {
self.contentHandler = contentHandler
bestAttemptContent = (request.content.mutableCopy() as? UNMutableNotificationContent)
if let bestAttemptContent = bestAttemptContent {
let userInfo = bestAttemptContent.userInfo
let minMgr = MinervaManager.getInstance()!
minMgr.enableNotificationTracking(userInfo, appGroup: Self.appGroup)
if userInfo["notiTitle"] != nil {
bestAttemptContent.title = userInfo["notiTitle"] as! String
}
if userInfo["notiBody"] != nil {
bestAttemptContent.body = userInfo["notiBody"] as! String
}
contentHandler(bestAttemptContent)
}
}
override func serviceExtensionTimeWillExpire() {
if let contentHandler = contentHandler, let bestAttemptContent = bestAttemptContent {
contentHandler(bestAttemptContent)
}
}
}
See the example directory for a complete sample app.