diff --git a/example/ios/Podfile.lock b/example/ios/Podfile.lock index 52a6db2..1b46724 100644 --- a/example/ios/Podfile.lock +++ b/example/ios/Podfile.lock @@ -120,4 +120,4 @@ SPEC CHECKSUMS: PODFILE CHECKSUM: 26b6dfe6bd7b68e4ed6d22322d27708fcee945b0 -COCOAPODS: 1.11.3 +COCOAPODS: 1.12.0 diff --git a/example/lib/main.dart b/example/lib/main.dart index eb94bdd..701472d 100644 --- a/example/lib/main.dart +++ b/example/lib/main.dart @@ -3,7 +3,6 @@ import 'package:courier_flutter/ios_foreground_notification_presentation_options import 'package:courier_flutter_sample/env.dart'; import 'package:firebase_core/firebase_core.dart'; import 'package:firebase_messaging/firebase_messaging.dart'; -import 'package:flutter/foundation.dart'; import 'package:flutter/material.dart'; import 'dart:async'; @@ -53,10 +52,7 @@ StreamController pushClicked = StreamController(); class _MyAppState extends State { bool _isLoading = true; String? _currentUserId; - final List _providers = [ - CourierProvider.apns, - CourierProvider.fcm - ]; + final List _providers = [CourierProvider.apns, CourierProvider.fcm]; @override void initState() { @@ -102,12 +98,10 @@ class _MyAppState extends State { final userId = await Courier.shared.userId; print(userId); - final fetchStatus = - await Courier.shared.getNotificationPermissionStatus(); + final fetchStatus = await Courier.shared.getNotificationPermissionStatus(); print(fetchStatus); - final requestStatus = - await Courier.shared.requestNotificationPermission(); + final requestStatus = await Courier.shared.requestNotificationPermission(); print(requestStatus); // Set the current FCM token diff --git a/example/pubspec.lock b/example/pubspec.lock index 2492bfe..2834a6f 100644 --- a/example/pubspec.lock +++ b/example/pubspec.lock @@ -42,7 +42,7 @@ packages: path: ".." relative: true source: path - version: "1.0.2" + version: "1.0.3" cupertino_icons: dependency: "direct main" description: diff --git a/lib/courier_flutter.dart b/lib/courier_flutter.dart index 8275591..3a084bb 100644 --- a/lib/courier_flutter.dart +++ b/lib/courier_flutter.dart @@ -13,27 +13,27 @@ class Courier { isDebugging = kDebugMode; // Set the default iOS presentation options - iOSForegroundNotificationPresentationOptions = - _iOSForegroundNotificationPresentationOptions; + iOSForegroundNotificationPresentationOptions = _iOSForegroundNotificationPresentationOptions; // Register listeners for when the native system receives messages CourierFlutterEventsPlatform.instance.registerMessagingListeners( - onPushNotificationDelivered: (message) => - _onPushNotificationDelivered?.call(message), - onPushNotificationClicked: (message) => - _onPushNotificationClicked?.call(message), - onLogPosted: (log) => - {/* Empty for now. Flutter will automatically print to console */}, + onPushNotificationDelivered: (message) => _onPushNotificationDelivered?.call(message), + onPushNotificationClicked: (message) => _onPushNotificationClicked?.call(message), + onLogPosted: (log) => { + /* Empty for now. Flutter will automatically print to console */ + }, ); } // Singleton static Courier? _instance; + static Courier get shared => _instance ??= Courier._(); /// Called if set and a push notification is delivered while the app /// Is in the foreground on iOS and "background" / foreground on Android Function(dynamic message)? _onPushNotificationDelivered; + set onPushNotificationDelivered(Function(dynamic message)? listener) { _onPushNotificationDelivered = listener; } @@ -42,6 +42,7 @@ class Courier { /// Will automatically get called the first time your app starts /// and the user clicked on a push notification to launch your app Function(dynamic message)? _onPushNotificationClicked; + set onPushNotificationClicked(Function(dynamic message)? listener) { _onPushNotificationClicked = listener; CourierFlutterEventsPlatform.instance.getClickedNotification(); @@ -50,7 +51,9 @@ class Courier { /// Allows you to show or hide Courier Native SDK debugging logs /// You likely want this to match your development environment debugging mode bool _isDebugging = kDebugMode; + bool get isDebugging => _isDebugging; + set isDebugging(bool isDebugging) { CourierFlutterCorePlatform.instance.isDebugging(isDebugging); _isDebugging = isDebugging; @@ -60,16 +63,12 @@ class Courier { /// showing a push notification when it is received while the app is in the foreground. /// This will not have an affect on any other platform /// If you do not not want a system push to appear, pass [] + List _iOSForegroundNotificationPresentationOptions = iOSNotificationPresentationOption.values; + List - _iOSForegroundNotificationPresentationOptions = - iOSNotificationPresentationOption.values; - List - get iOSForegroundNotificationPresentationOptions => - _iOSForegroundNotificationPresentationOptions; - set iOSForegroundNotificationPresentationOptions( - List options) { - CourierFlutterEventsPlatform.instance - .iOSForegroundPresentationOptions(options); + get iOSForegroundNotificationPresentationOptions => _iOSForegroundNotificationPresentationOptions; + set iOSForegroundNotificationPresentationOptions(List options) { + CourierFlutterEventsPlatform.instance.iOSForegroundPresentationOptions(options); _iOSForegroundNotificationPresentationOptions = options; } @@ -79,14 +78,12 @@ class Courier { /// Returns the currently stored apns token in the native SDK /// If you sign out, this value may still be set so that you can /// pass it to the next signed in userId - Future get apnsToken => - CourierFlutterCorePlatform.instance.apnsToken(); + Future get apnsToken => CourierFlutterCorePlatform.instance.apnsToken(); /// Returns the currently stored fcm token in the native SDK /// If you sign out, this value may still be set so that you can /// pass it to the next signed in userId - Future get fcmToken => - CourierFlutterCorePlatform.instance.fcmToken(); + Future get fcmToken => CourierFlutterCorePlatform.instance.fcmToken(); /// Sets the current FCM token in Courier Token Management /// Mostly used for handling the iOS Firebase SDK @@ -114,29 +111,29 @@ class Courier { /// You should call this where it makes the most sense for the user experience you are building /// Android does NOT support this feature yet due to Android AppCompatActivity limitations Future requestNotificationPermission() async { - final status = await CourierFlutterEventsPlatform.instance - .requestNotificationPermission(); + final status = await CourierFlutterEventsPlatform.instance.requestNotificationPermission(); return status.permissionStatus; } /// Returns the current push notification permission status /// Does not present a popup dialog to your user Future getNotificationPermissionStatus() async { - final status = await CourierFlutterEventsPlatform.instance - .getNotificationPermissionStatus(); + final status = await CourierFlutterEventsPlatform.instance.getNotificationPermissionStatus(); return status.permissionStatus; } /// Sends a push notification to the provider your would like /// This is used to test your integration /// For more info: https://www.courier.com/docs/reference/send/message/ - Future sendPush( - {required String authKey, - required String userId, - required String title, - required String body, - required List providers}) { - return CourierFlutterCorePlatform.instance - .sendPush(authKey, userId, title, body, providers); + Future sendPush({required String authKey, required String userId, required String title, required String body, required List providers}) { + return CourierFlutterCorePlatform.instance.sendPush(authKey, userId, title, body, providers); } + + /// Show a log to the console + static void log(String message) { + if (Courier.shared._isDebugging) { + print(message); + } + } + } diff --git a/lib/courier_flutter_events_method_channel.dart b/lib/courier_flutter_events_method_channel.dart index 360f33b..61adc06 100644 --- a/lib/courier_flutter_events_method_channel.dart +++ b/lib/courier_flutter_events_method_channel.dart @@ -1,5 +1,6 @@ import 'dart:io'; +import 'package:courier_flutter/courier_flutter.dart'; import 'package:courier_flutter/courier_flutter_events_platform_interface.dart'; import 'package:flutter/foundation.dart'; import 'package:flutter/services.dart'; @@ -14,17 +15,35 @@ class EventsChannelCourierFlutter extends CourierFlutterEventsPlatform { @override Future requestNotificationPermission() async { - return await channel.invokeMethod('requestNotificationPermission'); + + try { + return await channel.invokeMethod('requestNotificationPermission'); + } catch (error) { + return 'unknown'; + } + } @override Future getNotificationPermissionStatus() async { - return await channel.invokeMethod('getNotificationPermissionStatus'); + + try { + return await channel.invokeMethod('getNotificationPermissionStatus'); + } catch (error) { + return 'unknown'; + } + } @override Future getClickedNotification() async { - return await channel.invokeMethod('getClickedNotification'); + + try { + return await channel.invokeMethod('getClickedNotification'); + } catch (error) { + return; + } + } @override @@ -33,9 +52,13 @@ class EventsChannelCourierFlutter extends CourierFlutterEventsPlatform { // Skip other platforms. Do not show error if (!Platform.isIOS) return; - return await channel.invokeMethod('iOSForegroundPresentationOptions', { - 'options': options.map((option) => option.value).toList(), - }); + try { + return await channel.invokeMethod('iOSForegroundPresentationOptions', { + 'options': options.map((option) => option.value).toList(), + }); + } catch (error) { + return []; + } } diff --git a/pubspec.yaml b/pubspec.yaml index cf34edb..811bfc5 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,6 +1,6 @@ name: courier_flutter description: The best way to add push notifications to your Flutter app! -version: 1.0.3 +version: 1.0.4 homepage: https://courier.com environment: