From 051b31eb7c70fddeb5dc1a5f3eba16ec5c28e723 Mon Sep 17 00:00:00 2001 From: Ben Butterworth <24711048+ben-xD@users.noreply.github.com> Date: Mon, 15 Nov 2021 11:40:19 +0000 Subject: [PATCH 01/10] Create a notification instance from the `userInfo` --- .../push_notifications_received_sliver.dart | 12 ++++++--- example/pubspec.lock | 2 +- .../PushNotificationEventHandlers.swift | 26 ++++++++++++++++++- ios/Classes/RemoteMessage.swift | 9 +++++-- 4 files changed, 41 insertions(+), 8 deletions(-) diff --git a/example/lib/ui/push_notifications/push_notifications_received_sliver.dart b/example/lib/ui/push_notifications/push_notifications_received_sliver.dart index 5be9eb4c9..73b914dc9 100644 --- a/example/lib/ui/push_notifications/push_notifications_received_sliver.dart +++ b/example/lib/ui/push_notifications/push_notifications_received_sliver.dart @@ -1,3 +1,5 @@ +import 'dart:io'; + import 'package:ably_flutter/ably_flutter.dart' as ably; import 'package:flutter/material.dart'; import 'package:flutter/widgets.dart'; @@ -16,8 +18,8 @@ class PushNotificationsReceivedSliver extends StatelessWidget { children: [ Row( mainAxisAlignment: MainAxisAlignment.spaceBetween, - children: [ - const Text( + children: const [ + Text( 'Received messages', style: TextStyle(fontSize: 16, fontWeight: FontWeight.bold), ), @@ -45,10 +47,12 @@ class PushNotificationsReceivedSliver extends StatelessWidget { child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ - TextWithLabel('Title', + TextWithLabel('Notification Title', e.notification?.title ?? 'NO TITLE'), - TextWithLabel('Body', + TextWithLabel('Notification Body', e.notification?.body ?? 'NO BODY'), + TextWithLabel('Data', + e.data.toString()), ], ), )) diff --git a/example/pubspec.lock b/example/pubspec.lock index f70d57c6f..fccae6f45 100644 --- a/example/pubspec.lock +++ b/example/pubspec.lock @@ -7,7 +7,7 @@ packages: path: ".." relative: true source: path - version: "1.2.4" + version: "1.2.5" args: dependency: transitive description: diff --git a/ios/Classes/PushNotificationEventHandlers.swift b/ios/Classes/PushNotificationEventHandlers.swift index 08bc4183d..3863355f9 100644 --- a/ios/Classes/PushNotificationEventHandlers.swift +++ b/ios/Classes/PushNotificationEventHandlers.swift @@ -59,9 +59,33 @@ public class PushNotificationEventHandlers: NSObject, UNUserNotificationCenterDe if (application.applicationState == .background || application.applicationState == .inactive) { methodName = AblyPlatformMethod_pushOnBackgroundMessage } - let remoteMessage = RemoteMessage(data: userInfo._bridgeToObjectiveC(), notification: nil); + + let notification: Notification? = createNotification(from: userInfo); + let remoteMessage = RemoteMessage(data: userInfo._bridgeToObjectiveC(), notification: notification); methodChannel.invokeMethod(methodName, arguments: remoteMessage) { flutterResult in completionHandler(.newData); } } + + private func createNotification(from userInfo: [AnyHashable : Any]) -> Notification? { + let notification = Notification(); + + if let aps = userInfo["aps"] as? NSDictionary { + if let alert = aps["alert"] as? NSDictionary { + if let title = alert["title"] as? String { + notification.title = title + } + if let body = alert["body"] as? String { + notification.body = body + } + } + } + + if (notification.title != nil || notification.body == nil) { + // Return an instance of notification only if it has some data. + return notification; + } + + return nil; + } } diff --git a/ios/Classes/RemoteMessage.swift b/ios/Classes/RemoteMessage.swift index 59f7ed3f6..1014f4b03 100644 --- a/ios/Classes/RemoteMessage.swift +++ b/ios/Classes/RemoteMessage.swift @@ -23,12 +23,17 @@ public class RemoteMessage: NSObject { class Notification: NSObject { @objc - let title: String?; + var title: String?; @objc - let body: String?; + var body: String?; init(title: String, body: String) { self.title = title; self.body = body; } + + override init() { + self.title = nil; + self.body = nil; + } } From 8d45e6b4ee8260964d25b6cb4fe72c88403d046f Mon Sep 17 00:00:00 2001 From: Ben Butterworth <24711048+ben-xD@users.noreply.github.com> Date: Mon, 15 Nov 2021 11:50:44 +0000 Subject: [PATCH 02/10] Remove outdated comment --- .../push_realtime_client_received_sliver.dart | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/example/lib/ui/push_notifications/push_realtime_client_received_sliver.dart b/example/lib/ui/push_notifications/push_realtime_client_received_sliver.dart index 099a6bc90..d77bcaedf 100644 --- a/example/lib/ui/push_notifications/push_realtime_client_received_sliver.dart +++ b/example/lib/ui/push_notifications/push_realtime_client_received_sliver.dart @@ -19,10 +19,7 @@ class PushRealtimeClientReceivedSliver extends StatelessWidget { style: TextStyle(fontSize: 16, fontWeight: FontWeight.bold), ), const Text('To validate messages were sent, you can subscribe to ' - 'the channel and view the device logs. Data messages ' - 'are not currently available through Ably-flutter. ' - 'You should implement the relevant delegate methods on iOS' - ' and extend FirebaseMessagingService on Android.'), + 'the channel and view the device logs.'), BoolStreamButton( stream: _pushNotificationService.hasPushChannelStream, onPressed: _pushNotificationService From 2e44d5156c4622c389a269d1f48b40dcaf7a1901 Mon Sep 17 00:00:00 2001 From: Ben Butterworth <24711048+ben-xD@users.noreply.github.com> Date: Mon, 15 Nov 2021 11:58:36 +0000 Subject: [PATCH 03/10] Format files --- .../push_notifications/push_notifications_received_sliver.dart | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/example/lib/ui/push_notifications/push_notifications_received_sliver.dart b/example/lib/ui/push_notifications/push_notifications_received_sliver.dart index 73b914dc9..c1135aaac 100644 --- a/example/lib/ui/push_notifications/push_notifications_received_sliver.dart +++ b/example/lib/ui/push_notifications/push_notifications_received_sliver.dart @@ -51,8 +51,7 @@ class PushNotificationsReceivedSliver extends StatelessWidget { e.notification?.title ?? 'NO TITLE'), TextWithLabel('Notification Body', e.notification?.body ?? 'NO BODY'), - TextWithLabel('Data', - e.data.toString()), + TextWithLabel('Data', e.data.toString()), ], ), )) From c24553f6442fca09a6c20dc65d18228962e7ec47 Mon Sep 17 00:00:00 2001 From: Ben Butterworth <24711048+ben-xD@users.noreply.github.com> Date: Wed, 17 Nov 2021 17:37:36 +0000 Subject: [PATCH 04/10] Fix file name and usage after class rename --- .../push_notifications_device_information.dart | 2 +- .../push_notifications_ios_permissions_sliver.dart | 2 +- .../push_notifications_received_sliver.dart | 8 ++++---- example/lib/ui/{text_with_label.dart => text_row.dart} | 0 4 files changed, 6 insertions(+), 6 deletions(-) rename example/lib/ui/{text_with_label.dart => text_row.dart} (100%) diff --git a/example/lib/ui/push_notifications/push_notifications_device_information.dart b/example/lib/ui/push_notifications/push_notifications_device_information.dart index ce98b4627..64321d07d 100644 --- a/example/lib/ui/push_notifications/push_notifications_device_information.dart +++ b/example/lib/ui/push_notifications/push_notifications_device_information.dart @@ -1,6 +1,6 @@ import 'package:ably_flutter/ably_flutter.dart' as ably; import 'package:ably_flutter_example/push_notifications/push_notification_service.dart'; -import 'package:ably_flutter_example/ui/text_with_label.dart'; +import 'package:ably_flutter_example/ui/text_row.dart'; import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; diff --git a/example/lib/ui/push_notifications/push_notifications_ios_permissions_sliver.dart b/example/lib/ui/push_notifications/push_notifications_ios_permissions_sliver.dart index 3b345c7c1..9ea92a14c 100644 --- a/example/lib/ui/push_notifications/push_notifications_ios_permissions_sliver.dart +++ b/example/lib/ui/push_notifications/push_notifications_ios_permissions_sliver.dart @@ -3,7 +3,7 @@ import 'dart:io'; import 'package:ably_flutter/ably_flutter.dart' as ably; import 'package:ably_flutter_example/push_notifications/push_notification_service.dart'; import 'package:ably_flutter_example/ui/bool_stream_button.dart'; -import 'package:ably_flutter_example/ui/text_with_label.dart'; +import 'package:ably_flutter_example/ui/text_row.dart'; import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; import 'package:fluttertoast/fluttertoast.dart'; diff --git a/example/lib/ui/push_notifications/push_notifications_received_sliver.dart b/example/lib/ui/push_notifications/push_notifications_received_sliver.dart index c1135aaac..42950cffe 100644 --- a/example/lib/ui/push_notifications/push_notifications_received_sliver.dart +++ b/example/lib/ui/push_notifications/push_notifications_received_sliver.dart @@ -5,7 +5,7 @@ import 'package:flutter/material.dart'; import 'package:flutter/widgets.dart'; import '../../push_notifications/push_notification_handlers.dart'; -import '../text_with_label.dart'; +import '../text_row.dart'; class PushNotificationsReceivedSliver extends StatelessWidget { const PushNotificationsReceivedSliver(); @@ -47,11 +47,11 @@ class PushNotificationsReceivedSliver extends StatelessWidget { child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ - TextWithLabel('Notification Title', + TextRow('Notification Title', e.notification?.title ?? 'NO TITLE'), - TextWithLabel('Notification Body', + TextRow('Notification Body', e.notification?.body ?? 'NO BODY'), - TextWithLabel('Data', e.data.toString()), + TextRow('Data', e.data.toString()), ], ), )) diff --git a/example/lib/ui/text_with_label.dart b/example/lib/ui/text_row.dart similarity index 100% rename from example/lib/ui/text_with_label.dart rename to example/lib/ui/text_row.dart From 11684e921431be565458e5a3c6e2062925876152 Mon Sep 17 00:00:00 2001 From: Ben Butterworth <24711048+ben-xD@users.noreply.github.com> Date: Thu, 18 Nov 2021 08:13:27 +0000 Subject: [PATCH 05/10] Move Notification into new file, and add convenience initializer --- ios/Classes/Notification.swift | 30 +++++++++++++++++++ .../PushNotificationEventHandlers.swift | 25 +--------------- ios/Classes/RemoteMessage.swift | 17 ----------- 3 files changed, 31 insertions(+), 41 deletions(-) create mode 100644 ios/Classes/Notification.swift diff --git a/ios/Classes/Notification.swift b/ios/Classes/Notification.swift new file mode 100644 index 000000000..791f71f67 --- /dev/null +++ b/ios/Classes/Notification.swift @@ -0,0 +1,30 @@ +import Foundation + +class Notification: NSObject { + @objc + var title: String?; + @objc + var body: String?; + + init(title: String, body: String) { + self.title = title; + self.body = body; + } + + override init() { + self.title = nil; + self.body = nil; + } + + convenience init?(from userInfo: [AnyHashable : Any]) { + self.init() + if let aps = userInfo["aps"] as? NSDictionary { + if let alert = aps["alert"] as? NSDictionary { + title = alert["title"] as? String + body = alert["body"] as? String + } else { + return nil + } + } + } +} diff --git a/ios/Classes/PushNotificationEventHandlers.swift b/ios/Classes/PushNotificationEventHandlers.swift index 3863355f9..6d85bebd1 100644 --- a/ios/Classes/PushNotificationEventHandlers.swift +++ b/ios/Classes/PushNotificationEventHandlers.swift @@ -60,32 +60,9 @@ public class PushNotificationEventHandlers: NSObject, UNUserNotificationCenterDe methodName = AblyPlatformMethod_pushOnBackgroundMessage } - let notification: Notification? = createNotification(from: userInfo); - let remoteMessage = RemoteMessage(data: userInfo._bridgeToObjectiveC(), notification: notification); + let remoteMessage = RemoteMessage(data: userInfo._bridgeToObjectiveC(), notification: Notification(from: userInfo)); methodChannel.invokeMethod(methodName, arguments: remoteMessage) { flutterResult in completionHandler(.newData); } } - - private func createNotification(from userInfo: [AnyHashable : Any]) -> Notification? { - let notification = Notification(); - - if let aps = userInfo["aps"] as? NSDictionary { - if let alert = aps["alert"] as? NSDictionary { - if let title = alert["title"] as? String { - notification.title = title - } - if let body = alert["body"] as? String { - notification.body = body - } - } - } - - if (notification.title != nil || notification.body == nil) { - // Return an instance of notification only if it has some data. - return notification; - } - - return nil; - } } diff --git a/ios/Classes/RemoteMessage.swift b/ios/Classes/RemoteMessage.swift index 1014f4b03..7a9e78b29 100644 --- a/ios/Classes/RemoteMessage.swift +++ b/ios/Classes/RemoteMessage.swift @@ -20,20 +20,3 @@ public class RemoteMessage: NSObject { return RemoteMessage(data: userInfo._bridgeToObjectiveC(), notification: notification); } } - -class Notification: NSObject { - @objc - var title: String?; - @objc - var body: String?; - - init(title: String, body: String) { - self.title = title; - self.body = body; - } - - override init() { - self.title = nil; - self.body = nil; - } -} From 61bbbe22601998faf08b070a88ec92ba78376d41 Mon Sep 17 00:00:00 2001 From: Ben Butterworth <24711048+ben-xD@users.noreply.github.com> Date: Thu, 18 Nov 2021 09:44:04 +0000 Subject: [PATCH 06/10] Handle `aps["alert"]` being a String instead of a NSDictionary --- ios/Classes/Notification.swift | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/ios/Classes/Notification.swift b/ios/Classes/Notification.swift index 791f71f67..7286feacd 100644 --- a/ios/Classes/Notification.swift +++ b/ios/Classes/Notification.swift @@ -6,25 +6,26 @@ class Notification: NSObject { @objc var body: String?; - init(title: String, body: String) { - self.title = title; - self.body = body; - } - - override init() { - self.title = nil; - self.body = nil; + init(title: String?, body: String?) { + self.title = title + self.body = body } convenience init?(from userInfo: [AnyHashable : Any]) { - self.init() if let aps = userInfo["aps"] as? NSDictionary { if let alert = aps["alert"] as? NSDictionary { - title = alert["title"] as? String - body = alert["body"] as? String + let title = alert["title"] as? String + let body = alert["body"] as? String + self.init(title: title, body: body) + } else if let title = aps["alert"] as? String { + self.init(title: title, body: nil) } else { - return nil + self.init(title: nil, body: nil) + return nil; } + } else { + self.init(title: nil, body: nil) + return nil; } } } From 74b183d95cf031eced2393f988a11385755a909e Mon Sep 17 00:00:00 2001 From: Ben Butterworth <24711048+ben-xD@users.noreply.github.com> Date: Thu, 18 Nov 2021 09:48:28 +0000 Subject: [PATCH 07/10] Use let instead of var --- ios/Classes/Notification.swift | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ios/Classes/Notification.swift b/ios/Classes/Notification.swift index 7286feacd..07ac6a1d0 100644 --- a/ios/Classes/Notification.swift +++ b/ios/Classes/Notification.swift @@ -2,9 +2,9 @@ import Foundation class Notification: NSObject { @objc - var title: String?; + let title: String?; @objc - var body: String?; + let body: String?; init(title: String?, body: String?) { self.title = title From ee2d7748c9bdd4a8877790ce4bfe7a98bc4eb817 Mon Sep 17 00:00:00 2001 From: Ben Butterworth <24711048+ben-xD@users.noreply.github.com> Date: Thu, 18 Nov 2021 09:59:15 +0000 Subject: [PATCH 08/10] Remove unnecessary `self.init(title: nil, body: nil)` --- ios/Classes/Notification.swift | 2 -- 1 file changed, 2 deletions(-) diff --git a/ios/Classes/Notification.swift b/ios/Classes/Notification.swift index 07ac6a1d0..5d5e5da61 100644 --- a/ios/Classes/Notification.swift +++ b/ios/Classes/Notification.swift @@ -20,11 +20,9 @@ class Notification: NSObject { } else if let title = aps["alert"] as? String { self.init(title: title, body: nil) } else { - self.init(title: nil, body: nil) return nil; } } else { - self.init(title: nil, body: nil) return nil; } } From a2be8e867f9876982706efd9fb76b508dd6bcf18 Mon Sep 17 00:00:00 2001 From: Ben Butterworth <24711048+ben-xD@users.noreply.github.com> Date: Thu, 18 Nov 2021 10:03:46 +0000 Subject: [PATCH 09/10] Apply suggestions from code review MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Use `[AnyHashable: Any]` instead of `NSDictionary` Co-authored-by: Łukasz Szyszkowski <47628502+lukasz-szyszkowski@users.noreply.github.com> --- ios/Classes/Notification.swift | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ios/Classes/Notification.swift b/ios/Classes/Notification.swift index 5d5e5da61..527c7b2c5 100644 --- a/ios/Classes/Notification.swift +++ b/ios/Classes/Notification.swift @@ -12,8 +12,8 @@ class Notification: NSObject { } convenience init?(from userInfo: [AnyHashable : Any]) { - if let aps = userInfo["aps"] as? NSDictionary { - if let alert = aps["alert"] as? NSDictionary { + if let aps = userInfo["aps"] as? [AnyHashable: Any] { + if let alert = aps["alert"] as? [AnyHashable: Any] { let title = alert["title"] as? String let body = alert["body"] as? String self.init(title: title, body: body) From 20d99a27ff421faabb5240cd1908c1d8a6d8b612 Mon Sep 17 00:00:00 2001 From: Ben Butterworth <24711048+ben-xD@users.noreply.github.com> Date: Thu, 18 Nov 2021 11:10:13 +0000 Subject: [PATCH 10/10] Use guard statement MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Łukasz Szyszkowski <47628502+lukasz-szyszkowski@users.noreply.github.com> --- ios/Classes/Notification.swift | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/ios/Classes/Notification.swift b/ios/Classes/Notification.swift index 527c7b2c5..6faa334a0 100644 --- a/ios/Classes/Notification.swift +++ b/ios/Classes/Notification.swift @@ -12,18 +12,18 @@ class Notification: NSObject { } convenience init?(from userInfo: [AnyHashable : Any]) { - if let aps = userInfo["aps"] as? [AnyHashable: Any] { - if let alert = aps["alert"] as? [AnyHashable: Any] { - let title = alert["title"] as? String - let body = alert["body"] as? String - self.init(title: title, body: body) - } else if let title = aps["alert"] as? String { - self.init(title: title, body: nil) - } else { - return nil; - } + guard let aps = userInfo["aps"] as? [AnyHashable: Any] else { + return nil + } + + if let alert = aps["alert"] as? [AnyHashable: Any] { + let title = alert["title"] as? String + let body = alert["body"] as? String + self.init(title: title, body: body) + } else if let title = aps["alert"] as? String { + self.init(title: title, body: nil) } else { - return nil; + return nil } } }