Skip to content

Commit

Permalink
Merge branch 'feature/custom-app-delegate'
Browse files Browse the repository at this point in the history
  • Loading branch information
mikemilla committed May 15, 2023
2 parents 5efcaaa + ae70c83 commit 24c13e9
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 51 deletions.
2 changes: 1 addition & 1 deletion example/ios/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -120,4 +120,4 @@ SPEC CHECKSUMS:

PODFILE CHECKSUM: 26b6dfe6bd7b68e4ed6d22322d27708fcee945b0

COCOAPODS: 1.11.3
COCOAPODS: 1.12.0
12 changes: 3 additions & 9 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -53,10 +52,7 @@ StreamController<dynamic> pushClicked = StreamController<dynamic>();
class _MyAppState extends State<MyApp> {
bool _isLoading = true;
String? _currentUserId;
final List<CourierProvider> _providers = [
CourierProvider.apns,
CourierProvider.fcm
];
final List<CourierProvider> _providers = [CourierProvider.apns, CourierProvider.fcm];

@override
void initState() {
Expand Down Expand Up @@ -102,12 +98,10 @@ class _MyAppState extends State<MyApp> {
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
Expand Down
2 changes: 1 addition & 1 deletion example/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ packages:
path: ".."
relative: true
source: path
version: "1.0.2"
version: "1.0.3"
cupertino_icons:
dependency: "direct main"
description:
Expand Down
63 changes: 30 additions & 33 deletions lib/courier_flutter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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();
Expand All @@ -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;
Expand All @@ -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<iOSNotificationPresentationOption> _iOSForegroundNotificationPresentationOptions = iOSNotificationPresentationOption.values;

List<iOSNotificationPresentationOption>
_iOSForegroundNotificationPresentationOptions =
iOSNotificationPresentationOption.values;
List<iOSNotificationPresentationOption>
get iOSForegroundNotificationPresentationOptions =>
_iOSForegroundNotificationPresentationOptions;
set iOSForegroundNotificationPresentationOptions(
List<iOSNotificationPresentationOption> options) {
CourierFlutterEventsPlatform.instance
.iOSForegroundPresentationOptions(options);
get iOSForegroundNotificationPresentationOptions => _iOSForegroundNotificationPresentationOptions;
set iOSForegroundNotificationPresentationOptions(List<iOSNotificationPresentationOption> options) {
CourierFlutterEventsPlatform.instance.iOSForegroundPresentationOptions(options);
_iOSForegroundNotificationPresentationOptions = options;
}

Expand All @@ -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<String?> get apnsToken =>
CourierFlutterCorePlatform.instance.apnsToken();
Future<String?> 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<String?> get fcmToken =>
CourierFlutterCorePlatform.instance.fcmToken();
Future<String?> get fcmToken => CourierFlutterCorePlatform.instance.fcmToken();

/// Sets the current FCM token in Courier Token Management
/// Mostly used for handling the iOS Firebase SDK
Expand Down Expand Up @@ -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<NotificationPermissionStatus> 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<NotificationPermissionStatus> 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<String> sendPush(
{required String authKey,
required String userId,
required String title,
required String body,
required List<CourierProvider> providers}) {
return CourierFlutterCorePlatform.instance
.sendPush(authKey, userId, title, body, providers);
Future<String> sendPush({required String authKey, required String userId, required String title, required String body, required List<CourierProvider> 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);
}
}

}
35 changes: 29 additions & 6 deletions lib/courier_flutter_events_method_channel.dart
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -14,17 +15,35 @@ class EventsChannelCourierFlutter extends CourierFlutterEventsPlatform {

@override
Future<String> requestNotificationPermission() async {
return await channel.invokeMethod('requestNotificationPermission');

try {
return await channel.invokeMethod('requestNotificationPermission');
} catch (error) {
return 'unknown';
}

}

@override
Future<String> 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
Expand All @@ -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 [];
}

}

Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -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:
Expand Down

0 comments on commit 24c13e9

Please sign in to comment.