Skip to content

Commit

Permalink
Opting out form using default handler for push notifications.
Browse files Browse the repository at this point in the history
maratal committed Jan 26, 2025
1 parent c58152e commit 4399f09
Showing 3 changed files with 23 additions and 6 deletions.
7 changes: 7 additions & 0 deletions example/ios/Runner/AppDelegate.swift
Original file line number Diff line number Diff line change
@@ -14,4 +14,11 @@ import Flutter
override func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
NSLog("application:didFailToRegisterForRemoteNotificationsWithError was called with error: %@", error.localizedDescription)
}

/// An example of how you handle push notification if you opt-out from using Ably Pushes by adding `AblyFlutterHandlePushNotifications` equal to `NO` in your project's Info.plist file:
///
/// (This method is deprecated, use the new one from `UNUserNotificationCenter` as recommented by Apple, it's here for the sake of simplest example)
override func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
NSLog("Notification received: \(userInfo)")
}
}
2 changes: 2 additions & 0 deletions example/ios/Runner/Info.plist
Original file line number Diff line number Diff line change
@@ -2,6 +2,8 @@
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>AblyFlutterHandlePushNotifications</key>
<true/>
<key>CFBundleDevelopmentRegion</key>
<string>$(DEVELOPMENT_LANGUAGE)</string>
<key>CFBundleDisplayName</key>
20 changes: 14 additions & 6 deletions ios/Classes/AblyFlutter.m
Original file line number Diff line number Diff line change
@@ -695,7 +695,11 @@ +(void)registerWithRegistrar:(NSObject<FlutterPluginRegistrar>*)registrar {

// initializing method channel for round-trip method calls
FlutterMethodChannel *const methodChannel = [FlutterMethodChannel methodChannelWithName:@"io.ably.flutter.plugin" binaryMessenger:[registrar messenger] codec:methodCodec];
AblyFlutter *const ably = [[AblyFlutter alloc] initWithChannel:methodChannel streamsChannel: streamsChannel registrar:registrar];
NSNumber *handleAPNs = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"AblyFlutterHandlePushNotifications"];
AblyFlutter *const ably = [[AblyFlutter alloc] initWithChannel:methodChannel
streamsChannel:streamsChannel
registrar:registrar
handleAPNs:handleAPNs != nil ? [handleAPNs boolValue] : YES];

// registering method channel with registrar
[registrar addMethodCallDelegate:ably channel:methodChannel];
@@ -708,18 +712,22 @@ +(void)registerWithRegistrar:(NSObject<FlutterPluginRegistrar>*)registrar {

-(instancetype)initWithChannel:(FlutterMethodChannel *const)channel
streamsChannel:(AblyStreamsChannel *const)streamsChannel
registrar:(NSObject<FlutterPluginRegistrar>*)registrar {
registrar:(NSObject<FlutterPluginRegistrar>*)registrar
handleAPNs:(BOOL)handleAPNs {
self = [super init];
if (!self) {
return nil;
}
_instanceStore = [AblyInstanceStore sharedInstance];
_channel = channel;
_streamsChannel = streamsChannel;
UNUserNotificationCenter *const center = UNUserNotificationCenter.currentNotificationCenter;
_pushNotificationEventHandlers = [[PushNotificationEventHandlers alloc] initWithDelegate: center.delegate andMethodChannel: channel];
center.delegate = _pushNotificationEventHandlers;


if (handleAPNs) {
UNUserNotificationCenter *const center = UNUserNotificationCenter.currentNotificationCenter;
_pushNotificationEventHandlers = [[PushNotificationEventHandlers alloc] initWithDelegate: center.delegate andMethodChannel: channel];
center.delegate = _pushNotificationEventHandlers;
}

_handlers = @{
AblyPlatformMethod_getPlatformVersion: _getPlatformVersion,
AblyPlatformMethod_getVersion: _getVersion,

0 comments on commit 4399f09

Please sign in to comment.