smartpush 0.0.5 copy "smartpush: ^0.0.5" to clipboard
smartpush: ^0.0.5 copied to clipboard

PlatformAndroidiOS
outdated

A Flutter Plugin to use Pluggable's SDK for AI optimized delivery of push notifications in Flutter.

Pluggable's Smartpush 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.

  1. First install FlutterFire, a tool which automatically configures your app to use firebase;
  2. Then, install firebase_messaging, a cross-platform messaging solution;
  3. Then, to start using the Cloud Messaging package within your project follow these steps;
  4. 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.
  5. Also for iOS:
    • If you are using swift, in your AppDelegate.swift make sure you have added the first code;
    • If you're using flutter with objective-c, add the second code to your appdelegate.m file.
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 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();

await _smartpushPlugin.pluggableExecute(notificationChannelId, notificationChannelName, notificationActivity, notificationData, notificationIcon);

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";
	String notificationActivity = "com.example.SearchActivity";
	int notificationIcon = -99;
	Map<String, String> notificationData = message.data.map((key, value) => MapEntry(key, value.toString()));

	await Smartpush().pluggableExecute(notificationChannelId, notificationChannelName, notificationActivity, notificationData, notificationIcon);
}

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');
}

More info #

For more info visit https://pluggableai.xyz/ or give us feedback to [email protected].

1
likes
140
points
13
downloads

Publisher

verified publisherpluggableai.pt

Weekly Downloads

A Flutter Plugin to use Pluggable's SDK for AI optimized delivery of push notifications in Flutter.

Homepage

Documentation

API reference

License

BSD-3-Clause (license)

Dependencies

flutter, plugin_platform_interface

More

Packages that depend on smartpush

Packages that implement smartpush