Skip to content

notificationapi-com/notificationapi-flutter-sdk

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

12 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

NotificationAPI Flutter SDK

A Flutter plugin for integrating NotificationAPI push notifications into your mobile app.

πŸš€ Push Notification Support

Cross-platform push notifications with native performance:

  • Android: Full FCM (Firebase Cloud Messaging) support
  • iOS: Direct APN (Apple Push Notifications) integration - no Firebase routing required!

This means you get native iOS push notifications with reduced latency and fewer dependencies.

πŸš€ Quick Start

1. Installation

Add to your pubspec.yaml:

dependencies:
  notificationapi_flutter_sdk: ^1.0.3

2. Setup (One Line!)

import 'package:notificationapi_flutter_sdk/notificationapi_flutter_sdk.dart';

// That's it! This handles initialization, user identification, and permission requests
await NotificationAPI.setup(
  clientId: 'your_client_id_here',
  userId: 'user123',
  autoRequestPermission: true, // automatically request push permissions
  showForegroundNotifications: true, // show native notifications when app is open
);

3. Listen to Notifications (Optional)

// Listen to notifications received while app is open
// Note: These are automatically shown as native notifications by default
NotificationAPI.onMessage.listen((notification) {
  print('Received: ${notification.title}');
  // Optional: Handle notification data or custom actions
});

// Listen to notifications that opened the app
NotificationAPI.onMessageOpenedApp.listen((notification) {
  print('App opened from: ${notification.title}');
  // Handle deep linking or navigation
});

4. Check Status

// Check if everything is ready
if (NotificationAPI.isReady) {
  print('NotificationAPI is ready to receive notifications!');
}

// Get the push token (FCM on Android, APN on iOS)
String? token = await NotificationAPI.getToken();

πŸ“± Complete Example

import 'package:flutter/material.dart';
import 'package:notificationapi_flutter_sdk/notificationapi_flutter_sdk.dart';

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  @override
  void initState() {
    super.initState();
    _setupNotifications();
  }

  Future<void> _setupNotifications() async {
    try {
      await NotificationAPI.setup(
        clientId: 'your_client_id',
        userId: 'user123',
        showForegroundNotifications: true, // Auto-show native notifications
      );

      // Optional: Listen for custom handling
      NotificationAPI.onMessage.listen((notification) {
        print('Foreground notification: ${notification.title}');
        // Notification is automatically displayed as native notification
      });

      NotificationAPI.onMessageOpenedApp.listen((notification) {
        print('App opened from notification: ${notification.title}');
        // Handle navigation or deep linking
        if (notification.deepLink != null) {
          // Navigate to specific screen
        }
      });
    } catch (e) {
      print('Error setting up notifications: $e');
    }
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(title: Text('NotificationAPI Example')),
        body: Center(
          child: Column(
            children: [
              Text('Ready: ${NotificationAPI.isReady}'),
              SizedBox(height: 20),
              ElevatedButton(
                onPressed: () async {
                  bool granted = await NotificationAPI.requestPermission();
                  ScaffoldMessenger.of(context).showSnackBar(
                    SnackBar(content: Text('Permission: ${granted ? "Granted" : "Denied"}')),
                  );
                },
                child: Text('Request Permission'),
              ),
              SizedBox(height: 20),
              ElevatedButton(
                onPressed: () {
                  // Toggle foreground notification display
                  NotificationAPI.setShowForegroundNotifications(false);
                  ScaffoldMessenger.of(context).showSnackBar(
                    SnackBar(content: Text('Foreground notifications disabled')),
                  );
                },
                child: Text('Disable Foreground Notifications'),
              ),
            ],
          ),
        ),
      ),
    );
  }
}

πŸŒ™ Background Notifications (App Closed)

Background notifications are automatically handled by the push package. The SDK uses:

  • FCM for Android background notifications
  • APN for iOS background notifications

No additional setup is required for basic background notification handling.

Handling Notification Taps

When users tap a notification while the app is terminated, it's automatically handled:

class _MyAppState extends State<MyApp> {
  @override
  void initState() {
    super.initState();
    _setupNotifications();
  }

  Future<void> _setupNotifications() async {
    await NotificationAPI.setup(
      clientId: 'your_client_id',
      userId: 'user123',
    );

    // Handle notifications that opened the app
    NotificationAPI.onMessageOpenedApp.listen((notification) {
      _handleNotificationTap(notification);
    });
  }

  void _handleNotificationTap(PushNotification notification) {
    print('Notification tapped: ${notification.title}');

    // Handle deep linking or navigation
    if (notification.deepLink != null) {
      // Navigate to specific screen
      Navigator.pushNamed(context, notification.deepLink!);
    }
  }
}

Platform-Specific Setup

Android

  • Requires Firebase setup for FCM
  • Background notifications work automatically

iOS

  • Uses native APN - no Firebase required!
  • Requires Apple Developer Program membership
  • Enable Push Notifications capability in Xcode
  • Background notifications work automatically

πŸ”” Native Notification Display

Automatic Foreground Notifications

By default, when showForegroundNotifications: true is set in setup(), the SDK automatically displays received notifications as native push notifications even when your app is in the foreground. This provides a consistent user experience.

Notification Behavior

App State Notification Display
Foreground Native notification banner/alert (automatic)
Background System notification (handled by OS)
Terminated System notification (handled by OS)

Customization Options

await NotificationAPI.setup(
  // ... other parameters
  showForegroundNotifications: true,  // Enable native notifications in foreground
);

// You can also toggle this later
NotificationAPI.setShowForegroundNotifications(false); // Disable
NotificationAPI.setShowForegroundNotifications(true);  // Re-enable

πŸ”§ API Reference

NotificationAPI

Method Description Returns
setup() One-call setup with initialization, identification, and optional permission request Future<void>
requestPermission() Request push notification permission Future<bool>
getToken() Get the current push token (FCM on Android, APN on iOS) Future<String?>
onMessage Stream of foreground notifications (auto-displayed as native notifications) Stream<PushNotification>
onMessageOpenedApp Stream of notifications that opened the app Stream<PushNotification>
currentUser Get current user info NotificationUser?
isReady Check if SDK is ready bool
setShowForegroundNotifications() Enable/disable automatic native notification display void

Setup Parameters

await NotificationAPI.setup({
  required String clientId,           // Your NotificationAPI client ID
  required String userId,             // User's unique identifier
  String? hashedUserId,              // Hashed user ID for privacy (optional)
  bool autoRequestPermission = true, // Auto-request push permission (optional)
  bool showForegroundNotifications = true, // Auto-show native notifications (optional)
});

πŸ”’ Privacy & Security

  • User IDs can be hashed for additional privacy
  • All communication uses HTTPS
  • Push tokens are securely stored in NotificationAPI backend

πŸ› οΈ Platform Setup

Android (Firebase Required)

  1. Create a Firebase project at Firebase Console
  2. Add your Android app to the project
  3. Download google-services.json and place it in android/app/
  4. Follow the FlutterFire setup guide

iOS (No Firebase Required!)

  1. Requires Apple Developer Program membership ($99/year)
  2. Enable Push Notifications capability in Xcode
  3. Create APNs key in Apple Developer Console
  4. Configure your server to send notifications directly to APNs

πŸ› Troubleshooting

Common Issues

  1. Notifications not received

    • Android: Verify Firebase setup is correct
    • iOS: Ensure APNs key is configured correctly
    • Check if permission was granted: await NotificationAPI.requestPermission()
    • Ensure your NotificationAPI client ID is correct
  2. Foreground notifications not showing

    • Make sure showForegroundNotifications: true in setup
    • Check that notification permissions are granted
    • Verify app is actually in foreground
  3. Token is null

    • Android: Firebase may not be properly initialized
    • iOS: APNs may not be properly configured
    • Check device/simulator supports push notifications
  4. iOS Specific Issues

    • Ensure you have an Apple Developer Program membership
    • Push Notifications capability must be enabled in Xcode
    • APNs keys must be properly configured on your server
  5. Android Specific Issues

    • Verify google-services.json is in the correct location
    • Check that Firebase project includes your Android app's package name

πŸ“ž Support

πŸ“„ License

This project is licensed under the MIT License - see the LICENSE file for details.

About

Push Notifications for Flutter

Resources

License

Contributing

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published