pushly_pushsdk 1.2.0 copy "pushly_pushsdk: ^1.2.0" to clipboard
pushly_pushsdk: ^1.2.0 copied to clipboard

Flutter implementation of the Pushly PushSDK

example/lib/main.dart

import 'package:flutter/material.dart';
import 'dart:async';
import 'dart:math';
import 'package:shared_preferences/shared_preferences.dart';
import 'dart:developer' as dt;

import 'package:pushly_pushsdk/pushsdk.dart';

void main() {
  WidgetsFlutterBinding.ensureInitialized();
  runApp(const MyApp());
}

String generateRandomString(int len) {
  final random = Random();
  const availableChars =
      'AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz1234567890';
  final randomString = List.generate(
          len, (index) => availableChars[random.nextInt(availableChars.length)])
      .join();
  return randomString;
}

class MyApp extends StatefulWidget {
  const MyApp({super.key});

  @override
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  PNApplicationConfig? _config;
  PNSubscriberStatus? _status;
  bool _deleted = false;
  UserProfile? _userProfile;
  final appMessageInputController = TextEditingController();

  @override
  void initState() {
    super.initState();
    initializeSdk();
  }

  @override
  void dispose() {
    appMessageInputController.dispose();
    super.dispose();
  }

  Future<void> initializeSdk() async {
    if (!mounted) return;

    final prefs = await SharedPreferences.getInstance();
    String? externalId = prefs.getString("externalId");

    if (externalId == null) {
      externalId = generateRandomString(8);
      await prefs.setString("externalId", externalId);
    }

    await trackLifecycleCallbacks();
    await trackNotificationLifecycleCallbacks();
    await trackPermissionCallbacks();
    await trackAppMessageCallbacks();

    await PushSDK.setLogLevel(PNLogLevel.verbose);
    await PushSDK.setConfiguration("dGcVsIMi7EBZsHY8vWcQ2b9EZWSn0n1Jg6E0");
    // await PushSDK.setConfiguration("UjdYMswog2YLVmc9xs9O3GhdwglKnzIYb7hE");
    await PushSDK.UserProfile.setExternalId(externalId);

    getUserProfile();
  }

  Future<void> getUserProfile() {
    return PushSDK.UserProfile.get().then((value) => setState(() {
          _userProfile = value;
          _status = value.subscriberStatus;
          _deleted = value.isDeleted ?? false;
        }));
  }

  void showNativePermissionPrompt() async {
    await PushSDK.PushNotifications.showPermissionPrompt(completion: (granted, status, error) {
      if (granted == true) {
        getUserProfile();
      }
    });
  }

  Future<void> trackAppMessageCallbacks() {
    return PushSDK.registerAppMessageLifecycleCallbacks(
      onPushSDKWillPresentAppMessage: (appMessage) {
        print("Presenting AppMessage: ${appMessage.id}");
        return false;
      },
      onPushSDKDidReceiveAppMessageInteraction: (interaction, appMessage) {
        print(
            "Received AppMessage interaction: ${appMessage.id} ${interaction.id.value}");
        return false;
      },
      onPushSDKDidFailToProcessMessage: (message, appMessage) {
        print("Failed to process AppMessage: ${appMessage.id}");
      },
    );
  }

  Future<void> trackPermissionCallbacks() {
    return PushSDK.registerPermissionLifecycleCallbacks(
        onPushSDKDidReceivePermissionResponse: (permissionResponse) {
      dt.log(permissionResponse.value.toString());
    }, onPushSDKDidReceivePermissionStatusChange: (permissionResponse) {
      dt.log(permissionResponse.value);
    }, onPushSDKDidRegisterForRemoteNotificationsWithDeviceToken:
            (deviceToken) {
      dt.log(deviceToken);
    }, onPushSDKDidFailToRegisterForRemoteNotificationsWithError: (error) {
      dt.log(error.toString());
    });
  }

  Future<void> trackLifecycleCallbacks() {
    return PushSDK.registerPushSDKLifecycleCallbacks(
      onPushSDKDidFinishLoading: (configuration, subscriberStatus) {
        dt.log(
            "App callback onPushSDKDidFinishLoading #### Configuration: ${configuration.toString()}, status: $subscriberStatus");
        setState(() {
          _config = configuration;
          _status = subscriberStatus;
        });
      },
      onPushSDKDidExitWithSubscriberStatus: (subscriberStatus, deleted) {
        dt.log(
            "onPushSDKDidExitWithSubscriberStatus #### deleted: $deleted, status: $subscriberStatus");
        setState(() {
          _deleted = deleted;
          _status = subscriberStatus;
        });
      },
    );
  }

  Future<void> trackNotificationLifecycleCallbacks() {
    return PushSDK.registerNotificationLifecycleCallbacks(
      onPushSDKDidReceiveNotification: (notification) {
        dt.log(
            "Flutter app: Received app notification = ${notification.toString()}");
        if (notification is PNAndroidNotification) {
          // process android specific notification fields
        } else if (notification is PNiOSNotification) {
          // process iOS specific notification fields
        }
      },
      onPushSDKDidReceiveNotificationDestination: (destination, interaction) {
        dt.log(
            "Flutter app: Received Android notification destination = $destination for notification interaction: ${interaction.toString()}");
        return false;
      },
    );
  }

  void toggleUdr() {
    if (_userProfile?.isDeleted == false) {
      PushSDK.UserProfile.requestUserDeletion().then((_) => getUserProfile());
    }
  }

  void testPushNotifications() async {
    var isPaused = await PushSDK.PushNotifications.isPaused();
    var isEligibleToPrompt = await PushSDK.PushNotifications.isEligibleToPrompt();
    var isSubscribed = await PushSDK.PushNotifications.isSubscribed();

    print("isPaused: $isPaused isEligibleToPrompt: $isEligibleToPrompt isSubscribed: $isSubscribed");

    if (isPaused == true) {
      await PushSDK.PushNotifications.resume();
    } else {
      await PushSDK.PushNotifications.pause();
    }
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('Plugin example app'),
        ),
        body: Center(
            child: Column(children: [
          Text('Status:\n${_status?.name}\n', textAlign: TextAlign.center),
          Text('Deleted:\n$_deleted\n', textAlign: TextAlign.center),
          Text('UserProfile#externalId:\n${_userProfile?.externalId}\n',
              textAlign: TextAlign.center),
          Text('UserProfile#anonymousId:\n${_userProfile?.anonymousId}\n',
              textAlign: TextAlign.center),
          TextButton(
            style: TextButton.styleFrom(
              backgroundColor: Colors.blue,
              foregroundColor: Colors.white,
            ),
            onPressed: () {
              showNativePermissionPrompt();
            },
            child: const Text('Prompt notification permission'),
          ),
          TextButton(
            style: TextButton.styleFrom(
              backgroundColor: Colors.blue,
              foregroundColor: Colors.white,
            ),
            onPressed: () {
              toggleUdr();
            },
            child: const Text('Toggle UDR'),
          ),
          Padding(
            padding: const EdgeInsets.symmetric(horizontal: 8, vertical: 16),
            child: TextField(
              controller: appMessageInputController,
              decoration: const InputDecoration(
                border: OutlineInputBorder(),
                hintText: 'Enter App Message Id',
                  ),
                )
              ),
              TextButton(
                style: TextButton.styleFrom(
                  backgroundColor: Colors.blue,
                  foregroundColor: Colors.white,
                ),
                onPressed: () {
                  toggleUdr();
                }, 
                child: const Text('Toggle UDR'),
              ),
              TextButton(
                style: TextButton.styleFrom(
                  backgroundColor: Colors.blue,
                  foregroundColor: Colors.white,
                ),
                onPressed: () {
                  testPushNotifications();
                }, 
                child: const Text('PushNotifications test'),
              ),
            ]
          )
        ),
      ),
    );
  }
}
0
likes
0
points
2
downloads

Publisher

verified publisherpushly.com

Weekly Downloads

Flutter implementation of the Pushly PushSDK

Homepage

License

unknown (license)

Dependencies

flutter, plugin_platform_interface

More

Packages that depend on pushly_pushsdk

Packages that implement pushly_pushsdk