Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

iOS: add notification in RemoteMessage #224

Merged
merged 14 commits into from Nov 22, 2021
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
@@ -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]) {
if let aps = userInfo["aps"] as? NSDictionary {
ben-xD marked this conversation as resolved.
Show resolved Hide resolved
if let alert = aps["alert"] as? NSDictionary {
ben-xD marked this conversation as resolved.
Show resolved Hide resolved
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;
}
} 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;
}
}