Skip to content

Commit

Permalink
Merge pull request #224 from ably/enhancement/ios/notification-in-rem…
Browse files Browse the repository at this point in the history
…ote-message

iOS: add `notification` in RemoteMessage
  • Loading branch information
ben-xD committed Nov 22, 2021
2 parents 7618f0f + 4075a3c commit 2172fc1
Show file tree
Hide file tree
Showing 10 changed files with 44 additions and 26 deletions.
@@ -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';

Expand Down
Expand Up @@ -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';
Expand Down
@@ -1,9 +1,11 @@
import 'dart:io';

import 'package:ably_flutter/ably_flutter.dart' as ably;
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();
Expand All @@ -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),
),
Expand Down Expand Up @@ -45,10 +47,11 @@ class PushNotificationsReceivedSliver extends StatelessWidget {
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
TextRow('Title',
TextRow('Notification Title',
e.notification?.title ?? 'NO TITLE'),
TextRow('Body',
TextRow('Notification Body',
e.notification?.body ?? 'NO BODY'),
TextRow('Data', e.data.toString()),
],
),
))
Expand Down
Expand Up @@ -12,7 +12,7 @@ class PushNotificationsSliver extends StatelessWidget {
final PushNotificationService _pushNotificationService;
final bool isIOSSimulator;

PushNotificationsSliver(this._pushNotificationService,
const PushNotificationsSliver(this._pushNotificationService,
{required this.isIOSSimulator, Key? key})
: super(key: key);

Expand Down
Expand Up @@ -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
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion example/pubspec.lock
Expand Up @@ -7,7 +7,7 @@ packages:
path: ".."
relative: true
source: path
version: "1.2.4"
version: "1.2.5"
args:
dependency: transitive
description:
Expand Down
29 changes: 29 additions & 0 deletions ios/Classes/Notification.swift
@@ -0,0 +1,29 @@
import Foundation

class Notification: NSObject {
@objc
let title: String?;
@objc
let body: String?;

init(title: String?, body: String?) {
self.title = title
self.body = body
}

convenience init?(from userInfo: [AnyHashable : Any]) {
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
}
}
}
3 changes: 2 additions & 1 deletion ios/Classes/PushNotificationEventHandlers.swift
Expand Up @@ -59,7 +59,8 @@ public class PushNotificationEventHandlers: NSObject, UNUserNotificationCenterDe
if (application.applicationState == .background || application.applicationState == .inactive) {
methodName = AblyPlatformMethod_pushOnBackgroundMessage
}
let remoteMessage = RemoteMessage(data: userInfo._bridgeToObjectiveC(), notification: nil);

let remoteMessage = RemoteMessage(data: userInfo._bridgeToObjectiveC(), notification: Notification(from: userInfo));
methodChannel.invokeMethod(methodName, arguments: remoteMessage) { flutterResult in
completionHandler(.newData);
}
Expand Down
12 changes: 0 additions & 12 deletions ios/Classes/RemoteMessage.swift
Expand Up @@ -20,15 +20,3 @@ public class RemoteMessage: NSObject {
return RemoteMessage(data: userInfo._bridgeToObjectiveC(), notification: notification);
}
}

class Notification: NSObject {
@objc
let title: String?;
@objc
let body: String?;

init(title: String, body: String) {
self.title = title;
self.body = body;
}
}

0 comments on commit 2172fc1

Please sign in to comment.