Skip to content

Commit

Permalink
chore(ios): add initialization default labels option, extend example …
Browse files Browse the repository at this point in the history
…with new options
  • Loading branch information
worotyns committed Jun 26, 2023
1 parent 45aa833 commit b085a4f
Show file tree
Hide file tree
Showing 12 changed files with 79 additions and 28 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,7 @@ Initial release of library with Readme instructions
Now we use:
- 0.0.31 for Android
- 0.0.9 for iOS.

## 0.0.6
Now we use native SDK of Core 0.0.10 for iOS.
Add option to pass ios default labels
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ class NotificationService: PpgCoreNotificationServiceExtension {
target 'NSE' do
use_frameworks!
use_modular_headers!
pod 'PpgCoreSDK', '~> 0.0.9'
pod 'PpgCoreSDK', '~> 0.0.10'
end
```
7. In `Info.plist` add folowing to enable deep linking in flutter
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ import io.flutter.plugin.common.PluginRegistry
enum class MethodIdentifier {
initialize,
registerForNotifications,
onToken;
onToken,
onExternalData;

companion object {
fun create(name: String): MethodIdentifier {
return values().find { it.name.equals(name, ignoreCase = true) }
Expand Down
4 changes: 2 additions & 2 deletions example/ios/Podfile
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,13 @@ target 'Runner' do
use_frameworks!
use_modular_headers!
flutter_install_all_ios_pods File.dirname(File.realpath(__FILE__))
pod 'PpgCoreSDK', '~> 0.0.9'
pod 'PpgCoreSDK', '~> 0.0.10'
end

target 'NSE' do
platform :ios, '14.0'
use_frameworks!
pod 'PpgCoreSDK', '~> 0.0.9'
pod 'PpgCoreSDK', '~> 0.0.10'
end

post_install do |installer|
Expand Down
10 changes: 5 additions & 5 deletions example/ios/Podfile.lock
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
PODS:
- Flutter (1.0.0)
- ppg_core (0.0.5):
- ppg_core (0.0.6):
- Flutter
- PpgCoreSDK
- PpgCoreSDK (0.0.9)
- PpgCoreSDK (0.0.10)

DEPENDENCIES:
- Flutter (from `Flutter`)
- ppg_core (from `.symlinks/plugins/ppg_core/ios`)
- PpgCoreSDK (~> 0.0.9)
- PpgCoreSDK (~> 0.0.10)

SPEC REPOS:
trunk:
Expand All @@ -23,8 +23,8 @@ EXTERNAL SOURCES:
SPEC CHECKSUMS:
Flutter: f04841e97a9d0b0a8025694d0796dd46242b2854
ppg_core: da70a4ee9033090b03bb4e1701570885217f9887
PpgCoreSDK: a19d177da5b9df3281f5d86b16b10c57c7183f04
PpgCoreSDK: 76c44cf7ddd3afbce74183ed44e9232557011c24

PODFILE CHECKSUM: 1291197bf36a59ad881d7f79e8d59e0512234022
PODFILE CHECKSUM: e1be5b949b64c416452dadd97d6220b7024dcb2f

COCOAPODS: 1.12.1
14 changes: 9 additions & 5 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,15 @@ class _MyAppState extends State<MyApp> {
// Platform messages are asynchronous, so we initialize in an async method.
Future<void> initializePpgCore() async {
// TBD Logic
_ppgCorePlugin.initialize(onToken: (tokenJSON) {
// Upload this token to your server backend - you need this to use our API to send push notifications to this user
// This is a JSON formatted string contains all necessery informations to our backend.
log(tokenJSON);
});
_ppgCorePlugin.initialize(
// Only for iOS - default labels if custom channels is not defined
iosLabels: ["Click me", "Show me"],
onToken: (String tokenJSON) {
// Upload this token to your server backend - you need this to use our API to send push notifications to this user
// This is a JSON formatted string contains all necessery informations to our backend.
log(tokenJSON);
}
);

if (!mounted) return;

Expand Down
2 changes: 1 addition & 1 deletion example/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ packages:
path: ".."
relative: true
source: path
version: "0.0.5"
version: "0.0.6"
sky_engine:
dependency: transitive
description: flutter
Expand Down
23 changes: 18 additions & 5 deletions ios/Classes/PpgCorePlugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ public class PpgCorePlugin: NSObject, FlutterPlugin, FlutterApplicationLifeCycle
case registerForNotifications
case onMessage
case onToken
case onExternalData
}

private var channel: FlutterMethodChannel?
Expand All @@ -19,7 +20,7 @@ public class PpgCorePlugin: NSObject, FlutterPlugin, FlutterApplicationLifeCycle

public static func register(with registrar: FlutterPluginRegistrar) {
let channel = FlutterMethodChannel(name: "com.pushpushgo/core", binaryMessenger: registrar.messenger())
let instance = PpgCorePlugin()
let instance: PpgCorePlugin = PpgCorePlugin()

PpgCorePlugin.instance = instance

Expand All @@ -38,18 +39,30 @@ public class PpgCorePlugin: NSObject, FlutterPlugin, FlutterApplicationLifeCycle
let method = MethodIdentifier(rawValue: call.method)
switch method {
case .initialize:
return onInitialize(callback: result)
return onInitialize(callback: result, arguments: call.arguments)
case .registerForNotifications:
return onRegisterForNotifications(callback: result)
default:
return result(FlutterMethodNotImplemented)
}
}

private func onInitialize(callback: @escaping FlutterResult) {
private func onExternalData(data: String) -> Void {
channel?.invokeMethod(MethodIdentifier.onExternalData.rawValue, arguments: data)
}

private func onInitialize(callback: @escaping FlutterResult, arguments: Any?) {
// TODO: Pass dynamic data with "action labels" / define multiple channels
UNUserNotificationCenter.current().delegate = PpgCorePlugin.instance
ppgCoreClient.initialize(actionLabels: ["Open", "Check more"])

var defaultActions: [String] = ["Open", "Check more"]
if let argsOpts: [String : Any] = arguments as? [String: Any],
let labelOpts: [String] = argsOpts["iosLabels"] as? [String] {
defaultActions = labelOpts
}

ppgCoreClient.initialize(actionLabels: defaultActions, onExternalData: self.onExternalData)

return callback("success")
}

Expand All @@ -67,7 +80,7 @@ public class PpgCorePlugin: NSObject, FlutterPlugin, FlutterApplicationLifeCycle

public func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
let subscriberJSON = Subscription(token: deviceToken).toJSONString();
// PpgCoreLogger.info(subscriberJSON)
PpgCoreLogger.info(subscriberJSON)
channel?.invokeMethod(MethodIdentifier.onToken.rawValue, arguments: subscriberJSON)
}

Expand Down
2 changes: 1 addition & 1 deletion ios/ppg_core.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#
Pod::Spec.new do |s|
s.name = 'ppg_core'
s.version = '0.0.5'
s.version = '0.0.6'
s.summary = 'PushPushGo Core SDK'
s.description = <<-DESC
PushPushGo Core SDK for Flutter (Dart)
Expand Down
3 changes: 3 additions & 0 deletions lib/common_channel.dart
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ enum ChannelMethod {
registerForNotifications,
unregisterFromNotifications,
getToken,
onExternalData,
onLaunch,
onMessage,
onResume,
Expand All @@ -51,6 +52,8 @@ extension ChannelMethodExtensions on ChannelMethod {
return 'onResume';
case ChannelMethod.onToken:
return 'onToken';
case ChannelMethod.onExternalData:
return 'onExternalData';
default:
return '';
}
Expand Down
37 changes: 31 additions & 6 deletions lib/ppg_core.dart
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import 'dart:async';

import 'package:ppg_core/common_channel.dart';
import 'package:flutter/services.dart';

typedef MessageHandler = Function(Map<String, dynamic> message);
typedef ExternalDataHandler = Function(String data);
typedef SubscriptionHandler = Function(String serializedJSON);
typedef DefaultLabels = List<String>;
typedef PpgCoreOptions = Map<String, dynamic>;

enum RegisterStatus {
granted,
Expand All @@ -14,22 +16,41 @@ enum RegisterStatus {

class PpgCore {
String? lastSubscriptionJSON;

MessageHandler _onMessage = (_) {};

// ignore: prefer_function_declarations_over_variables
final MessageHandler _onMessage = (_) {};
// ignore: prefer_function_declarations_over_variables
final ExternalDataHandler _onExternalData = (_) {};

SubscriptionHandler _onToken = (_) {
throw UnsupportedError("onToken handler must be declared");
};

Future<void> initialize({
required SubscriptionHandler onToken,
MessageHandler? onMessage,
// MessageHandler? onMessage,
// ExternalDataHandler? onExternalData,
List<String>? iosLabels,
}) {
_onMessage = onMessage ?? (_) {};
// _onMessage = onMessage ?? (_) {};
// _onExternalData = onExternalData ?? (_) {};
_onToken = onToken;

final List<String> defaultIosLabels = ["Open", "Show more"];
final List<String> assignedLabels = iosLabels ?? [];

String firstLabel = assignedLabels.isNotEmpty ? assignedLabels[0] : defaultIosLabels[0];
String secondLabel = assignedLabels.length > 1 ? assignedLabels[1] : defaultIosLabels[1];

CommonChannel.setMethodCallHandler(_handleChannelMethodCallback);

final PpgCoreOptions opts = {
'iosLabels': [firstLabel, secondLabel]
};

return CommonChannel.invokeMethod<void>(
method: ChannelMethod.initialize,
arguments: opts
).catchError(
(error) {
if (error is! TimeoutException) throw error;
Expand Down Expand Up @@ -63,12 +84,16 @@ class PpgCore {
String method = call.method;

dynamic arguments = call.arguments;

if (method == ChannelMethod.onMessage.name) {
return _onMessage(arguments ?? "{}"); //.cast<String, dynamic>()
} else if (method == ChannelMethod.onToken.name) {
lastSubscriptionJSON = arguments;
return _onToken(lastSubscriptionJSON ?? "{}");
} else if (method == ChannelMethod.onExternalData.name) {
return _onExternalData(arguments ?? "");
}
throw UnsupportedError("Unrecognized JSON message");

throw UnsupportedError("Unrecognized message");
}
}
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: ppg_core
description: PushPushGo Core SDK for Flutter (Dart)
version: 0.0.5
version: 0.0.6
repository: https://github.com/ppgco/ppg-core-flutter-sdk

environment:
Expand Down

0 comments on commit b085a4f

Please sign in to comment.