rationalowl_flutter 1.0.1
rationalowl_flutter: ^1.0.1 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
libs
directory inandroid/app
, then copy therationalowl-android-1.4.1.aar
file.
- Set the
dependencies
inandroid/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.xcworkspace
via Xcode. -
Download the RationalOwl.framework directory.
-
Drag and drop the
RationalOwl.framework
directory to theRunner
project. -
Select the
Copy items if needed
checkbox, then clickFinish
.
-
Select the
Runner
project, then select theRunner
target. -
Open the
Frameworks, Libraries, and Embedded Content
section, then click on the+
button. -
Click the
Add Files...
menu item, then choose theRationalOwl.framework
directory.
-
Select the
Signing & Capabilities
tab, then click on the+ Capability
button. -
Add the
Push Notifications
andBackground Modes
capabilities. -
In the
Background Modes
capability, enable theBackground fetch
andRemote notifications
modes.
-
Select the
Build Phases
tab, then open theLink Binary With Libraries
section. -
Click on the
+
button. -
Choose the
UserNotifications.framework
, then clickAdd
.
- Open the
Embed Frameworks
section, then select theCopy only when installing
checkbox.
-
Click on the
+
button on the left bottom. -
Choose the
Notification Service Extension
template, then clickNext
.
- Add the
Product Name
, then clickFinish
.
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.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.