smartpush 1.8.7
smartpush: ^1.8.7 copied to clipboard
A Flutter Plugin to use Pluggable's SDK for AI optimized delivery of push notifications in Flutter.
Pluggable's Smartpush Plugin for Flutter apps #
A Flutter plugin for Flutter apps.
Pre-conditions #
Before using this plugin you should guarantee that your app already integrates with Firebase Cloud Messaging (FCM). The steps are quite easy to follow.
- First install FlutterFire, a tool which automatically configures your app to use firebase;
- Then, install firebase_messaging, a cross-platform messaging solution;
- Then, to start using the Cloud Messaging package within your project follow these steps;
- Integrating the Cloud Messaging plugin on iOS requires additional setup before your devices receive messages. The full steps are available here, but the following prerequisites are required to be able to enable messaging:
- You must have an active Apple Developer Account;
- You must have a physical iOS device to receive messages.
- Also for iOS:
- If you are using swift, in your
AppDelegate.swiftmake sure you have added the first code; - If you're using flutter with objective-c, add the second code to your
appdelegate.mfile.
- If you are using swift, in your
import Firebase
FirebaseApp.configure() //add this before the code below
GeneratedPluginRegistrant.register(with: self)
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[FIRApp configure]; //add this at the top
// ...
}
NOTE: FCM via APNs does not work on iOS Simulators. To receive messages & notifications a real device is required. The same is recommended for Android.
Smartpush Usage #
After configuring your app to integrate with FCM, you are ready to use this plugin to properly engage with your users. This library exposes two methods which can be imported like this in your main.dart:
import 'package:smartpush/smartpush.dart';
pluggableExecute #
This method is used to execute the SDK AI models to deliver push notifications at the best moment to each particular user. This method, which returns the UUID of the push (which you can use to associate sensor data to your users) should be called in the functions you created to receive background and foreground push notifications (typically, Future<void> _firebaseMessagingBackgroundHandler(RemoteMessage message) async and void _firebaseMessagingForegroundHandler(RemoteMessage message) async), as follows:
final _smartpushPlugin = Smartpush();
final pushUUID = await _smartpushPlugin.pluggableExecute(notificationChannelId, notificationChannelName, notificationData);
// Save the pushUUID and userID to the database
// ...
Example:
/// Receive push while in the background
@pragma('vm:entry-point')
Future<void> _firebaseMessagingBackgroundHandler(RemoteMessage message) async {
// If you're going to use other Firebase services in the background, such as Firestore,
// make sure you call `initializeApp` before using other Firebase services.
await Firebase.initializeApp(
options: DefaultFirebaseOptions.currentPlatform,
);
print("Handling a background message: ${message.messageId}");
print('Message data: ${message.data}');
String notificationChannelId = "aNotificationChannelId";
String notificationChannelName = "aNotificationChannelName";
Map<String, String> notificationData = message.data.map((key, value) => MapEntry(key, value.toString()));
final pushUUID = await Smartpush().pluggableExecute(notificationChannelId, notificationChannelName, notificationData);
// Save the pushUUID and userID to the database
// ...
}
/// Receive push while in the foreground
void _firebaseMessagingForegroundHandler(RemoteMessage message) async {
print("Handling a foreground message: ${message.messageId}");
print('Message data: ${message.data}');
if (message.notification != null) {
print('Message also contained a notification: ${message.notification}');
}
String notificationChannelId = "aNotificationChannelId";
String notificationChannelName = "aNotificationChannelName";
Map<String, String> notificationData = message.data.map((key, value) => MapEntry(key, value.toString()));
final pushUUID = await Smartpush().pluggableExecute(notificationChannelId, notificationChannelName, notificationData);
// Save the pushUUID and userID to the database
// ...
}
pluggableStoreFeedback #
This method is used to create engagement metrics (used as soon as a notification is opened). This method should be called within a function added to FirebaseMessaging.onMessageOpenedApp.listen, as follows:
final _smartpushPlugin = Smartpush();
await _smartpushPlugin.pluggableStoreFeedback();
Example:
void _handlePushClick(RemoteMessage message) async {
print('A new onMessageOpenedApp event was published!');
print('RemoteMessage: $message');
await Smartpush().pluggableStoreFeedback();
}
Future<void> setupInteractedMessage() async {
// Get any messages which caused the application to open from
// a terminated state.
RemoteMessage? initialMessage = await FirebaseMessaging.instance.getInitialMessage();
// If the message is not null
if (initialMessage != null) {
_handlePushClick(initialMessage);
}
// Also handle any interaction when the app is in the background via a
// Stream listener
FirebaseMessaging.onMessageOpenedApp.listen(_handlePushClick);
}
getToken and subscribeToTopic #
You can get a specific token to identify each single device and/or you can subscribe each device to specific topics, as follows:
Future<void> setupToken() async {
// Get the token each time the application loads
String? token = await FirebaseMessaging.instance.getToken();
// Save the initial token to the database
// ...
print('Device token is as follows: $token');
// Subscribe to topic
await FirebaseMessaging.instance.subscribeToTopic('a_topic');
}
Request Push Permission #
You should also request for user permission to deliver push notifications:
Future requestPermission() async {
FirebaseMessaging messaging = FirebaseMessaging.instance;
// Ask for push permissions
NotificationSettings settings = await messaging.requestPermission(
alert: true,
announcement: false,
badge: true,
carPlay: false,
criticalAlert: false,
provisional: true,
sound: true,
);
print('User granted permission: ${settings.authorizationStatus}');
// Presentation for foreground push
await messaging.setForegroundNotificationPresentationOptions(
alert: true,
badge: true,
sound: true,
);
}
More info #
For more info visit https://pluggableai.xyz/ or give us feedback to [email protected].