Flutter
You can add Xtremepush to your Flutter apps quickly by following our native integration for Android and iOS, then adding a method channel to communicate from your Dart code to the respective platform. Follow our guide below to show how this can be done.
Make sure your mobile apps are added to Xtremepush, following the guides for Android and iOS.
The Xtremepush.dart
file and sample integration code can be downloaded here.
Adding the method channel
Add the Xtremepush.dart
file to you main lib directory, beside the main.dart
file.
Android
In your MainActivity.java class, add the Xtremepush method channel by adding the following:
private static final String CHANNEL = "ie.imobile.xtremepush/flutter";
@Override
public void configureFlutterEngine(@NonNull FlutterEngine flutterEngine) {
super.configureFlutterEngine(flutterEngine);
new MethodChannel(flutterEngine.getDartExecutor().getBinaryMessenger(), CHANNEL)
.setMethodCallHandler(
(call, result) -> {
// Note: this method is invoked on the main thread.
switch (call.method) {
case "hitEvent":
final String xpEvent = call.argument("xpEvent");
mPushConnector.hitEvent(xpEvent);
break;
case "hitTag":
final String xpTag = call.argument("xpTag");
mPushConnector.hitTag(xpTag);
break;
case "hitTagWithValue":
final String xpTagw = call.argument("xpTag");
final String xpTagValue = call.argument("xpTagValue");
mPushConnector.hitTag(xpTagw, xpTagValue);
break;
case "setUser":
final String user_id = call.argument("xp_user_id");
mPushConnector.setUser(user_id);
break;
case "setTempUser":
final String temp_user_id = call.argument("xp_temp_user_id");
mPushConnector.setTempUser(temp_user_id);
break;
case "openInbox":
mPushConnector.openInbox(this);
break;
case "setSubscription":
final boolean sub_status = call.argument("subscription_flag");
mPushConnector.setSubscription(sub_status);
break;
case "getInboxList":
final int offset = call.argument("offset");
final int limit = call.argument("limit");
mPushConnector.inboxListWithOffset(this, offset, limit);
break;
case "requestNotificationPermissions":
mPushConnector.requestNotificationPermissions(this);
break;
}
}
);
}
override fun configureFlutterEngine(flutterEngine: FlutterEngine) {
super.configureFlutterEngine(flutterEngine)
MethodChannel(flutterEngine.dartExecutor.binaryMessenger, CHANNEL)
.setMethodCallHandler { call: MethodCall, result: MethodChannel.Result? ->
when (call.method) {
"hitEvent" -> {
val xpEvent = call.argument<String>("xpEvent")
PushConnector.mPushConnector.hitEvent(xpEvent)
}
"hitTag" -> {
val xpTag = call.argument<String>("xpTag")
PushConnector.mPushConnector.hitTag(xpTag)
}
"hitTagWithValue" -> {
val xpTagw = call.argument<String>("xpTag")
val xpTagValue = call.argument<String>("xpTagValue")
PushConnector.mPushConnector.hitTag(xpTagw, xpTagValue)
}
"setUser" -> {
val user_id = call.argument<String>("xp_user_id")
PushConnector.mPushConnector.setUser(user_id)
}
"setTempUser" -> {
val temp_user_id = call.argument<String>("xp_temp_user_id")
PushConnector.mPushConnector.setTempUser(temp_user_id)
}
"openInbox" -> PushConnector.mPushConnector.openInbox(this)
"setSubscription" -> {
val sub_status = call.argument<Boolean>("subscription_flag")!!
PushConnector.mPushConnector.setSubscription(sub_status)
}
}
}
}
companion object {
private const val CHANNEL = "ie.imobile.xtremepush/flutter"
}
iOS
In your AppDelegate's didFinishLaunching
call, add the method channel like below:
let controller : FlutterViewController = window?.rootViewController as! FlutterViewController
let xtremepushChannel = FlutterMethodChannel(name: "ie.imobile.xtremepush/flutter",
binaryMessenger: controller.binaryMessenger)
xtremepushChannel({
(call: FlutterMethodCall, result: @escaping FlutterResult) -> Void in
switch call.method {
case "hitEvent":
let xpEvent = call.arguments["xpEvent"] as! String
XPush.hitEvent(xpEvent)
case "hitTag":
let xpTag = call.arguments["xpTag"] as! String;
XPush.hitTag(xpTag)
case "hitTagWithValue":
let xpTag = call.arguments["xpTag"] as! String;
let xpTagValue = call.arguments["xpTagValue"] as! String;
XPush.hitTag(xpTag, withValue: xpTagValue)
case "setUser":
let userId = call.arguments["xp_user_id"] as! String;
XPush.setUser(userId)
case "setTempUser":
let userId = call.arguments["xp_temp_user_id"] as! String;
XPush.setTempUser(userId)
case "openInbox":
XPush.openInbox()
case "setSubscription":
let subscription_flag = (call.arguments["xp_temp_user_id"] as! NSNumber).boolValue;
XPush.setSubscription(subscription_flag)
}
})
[xtremepushChannel setMethodCallHandler:^(FlutterMethodCall* call, FlutterResult result) {
if ([@"hitEvent" isEqualToString:call.method]) {
NSString *xpEvent = call.arguments[@"xpEvent"];
[XPush hitEvent: xpEvent];
}
if ([@"hitTag" isEqualToString:call.method]) {
NSString *xpTag = call.arguments[@"xpTag"];
[XPush hitTag: xpTag];
}
if ([@"hitTagWithValue" isEqualToString:call.method]) {
NSString *xpTag = call.arguments[@"xpTag"];
NSString *xpTagValue = call.arguments[@"xpTagValue"];
[XPush hitTag: xpTag withValue:xpTagValue];
}
if ([@"setUser" isEqualToString:call.method]) {
NSString *xpUser = call.arguments[@"xp_user_id"];
[XPush setUser: xpUser];
}
if ([@"setTempUser" isEqualToString:call.method]) {
NSString *xpUser = call.arguments[@"xp_temp_user_id"];
[XPush setTempUser: xpUser];
}
if ([@"openInbox" isEqualToString:call.method]) {
[XPush openInbox];
}
if ([@"setSubscription" isEqualToString:call.method]) {
BOOL subscription_flag = [call.arguments[@"subscription_flag"] boolValue];
[XPush setSubscription: subscription_flag];
}
}];
Calling Xtremepush functions from Dart
In your Dart file, import Xtremepush from the Xtremepush.dart file:
import 'Xtremepush.dart';
You can then call the functions using the following:
Xtremepush.hitEvent("string");
Xtremepush.hitTag("string");
Xtremepush.hitTagWithValue("string", "string");
Xtremepush.hitEvent("string");
Xtremepush.setUser("string");
Xtremepush.setTempUser("string");
Xtremepush.openInbox();
Xtremepush.getInboxList(offset, limit);
Xtremepush.setSubscription(true/false);
Make sure you set the user ID
It is recommended that at this stage you use our SDK method to set user IDs by following our dedicated guide to ensure devices can be associated and targeted in your campaigns by your own unique identifier.
Updated 8 months ago