Skip to content

Commit

Permalink
chore: fetch registered device token (#171)
Browse files Browse the repository at this point in the history
  • Loading branch information
mrehan27 authored Nov 14, 2024
1 parent c1ec3de commit 8197413
Show file tree
Hide file tree
Showing 10 changed files with 86 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ internal object Keys {
const val TRACK_METRIC = "trackMetric"
const val ON_MESSAGE_RECEIVED = "onMessageReceived"
const val DISMISS_MESSAGE = "dismissMessage"
const val GET_REGISTERED_DEVICE_TOKEN = "getRegisteredDeviceToken"
}

object Tracking {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import io.customer.customer_io.constant.Keys
import io.customer.customer_io.getAsTypeOrNull
import io.customer.customer_io.invokeNative
import io.customer.messagingpush.CustomerIOFirebaseMessagingService
import io.customer.sdk.CustomerIO
import io.customer.sdk.core.di.SDKComponent
import io.customer.sdk.core.util.Logger
import io.flutter.embedding.engine.plugins.FlutterPlugin
Expand All @@ -28,6 +29,11 @@ internal class CustomerIOPushMessaging(

override fun onMethodCall(call: MethodCall, result: MethodChannel.Result) {
when (call.method) {
Keys.Methods.GET_REGISTERED_DEVICE_TOKEN -> {
call.invokeNative(result) {
return@invokeNative getRegisteredDeviceToken()
}
}
Keys.Methods.ON_MESSAGE_RECEIVED -> {
call.invokeNative(result) { args ->
return@invokeNative onMessageReceived(
Expand All @@ -43,6 +49,10 @@ internal class CustomerIOPushMessaging(
}
}

private fun getRegisteredDeviceToken(): String? {
return CustomerIO.instance().registeredDeviceToken
}

/**
* Handles push notification received. This is helpful in processing push notifications
* received outside the CIO SDK.
Expand Down
4 changes: 0 additions & 4 deletions apps/amiapp_flutter/lib/src/customer_io.dart
Original file line number Diff line number Diff line change
Expand Up @@ -127,10 +127,6 @@ extension AmiAppSDKExtensions on CustomerIOSDK {
return null;
}
}

Future<String?> getDeviceToken() async {
return null;
}
}

/// Customer.io SDK extensions to save/retrieve configurations to/from preferences.
Expand Down
9 changes: 6 additions & 3 deletions apps/amiapp_flutter/lib/src/screens/settings.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import 'package:customer_io/customer_io.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:go_router/go_router.dart';
Expand Down Expand Up @@ -48,7 +49,7 @@ class _SettingsScreenState extends State<SettingsScreen> {

@override
void initState() {
widget._customerIOSDK.getDeviceToken().then((value) =>
CustomerIO.instance.pushMessaging.getRegisteredDeviceToken().then((value) =>
setState(() => _deviceTokenValueController.text = value ?? ''));

final cioConfig = widget._customerIOSDK.sdkConfig;
Expand Down Expand Up @@ -201,7 +202,8 @@ class _SettingsScreenState extends State<SettingsScreen> {
semanticsLabel: 'API Host Input',
hintText: 'cdp.customer.io/v1',
valueController: _apiHostValueController,
validator: (value) => value?.isEmptyOrValidUrl() != false
validator: (value) => value?.isEmptyOrValidUrl() !=
false
? null
: 'Please enter url e.g. cdp.customer.io/v1 (without https)',
),
Expand All @@ -211,7 +213,8 @@ class _SettingsScreenState extends State<SettingsScreen> {
semanticsLabel: 'CDN Host Input',
hintText: 'cdp.customer.io/v1',
valueController: _cdnHostValueController,
validator: (value) => value?.isEmptyOrValidUrl() != false
validator: (value) => value?.isEmptyOrValidUrl() !=
false
? null
: 'Please enter url e.g. cdp.customer.io/v1 (without https)',
),
Expand Down
1 change: 1 addition & 0 deletions ios/Classes/Keys.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ struct Keys {
static let registerDeviceToken = "registerDeviceToken"
static let trackMetric = "trackMetric"
static let dismissMessage = "dismissMessage"
static let getRegisteredDeviceToken = "getRegisteredDeviceToken"
}

struct Tracking {
Expand Down
45 changes: 45 additions & 0 deletions ios/Classes/MessagingPush/CustomerIOMessagingPush.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import CioDataPipelines
import Flutter
import Foundation

public class CustomerIOMessagingPush: NSObject, FlutterPlugin {
private let channelName: String = "customer_io_messaging_push"

public static func register(with registrar: FlutterPluginRegistrar) {
}

private var methodChannel: FlutterMethodChannel?

init(with registrar: FlutterPluginRegistrar) {
super.init()

methodChannel = FlutterMethodChannel(
name: channelName, binaryMessenger: registrar.messenger())
guard let methodChannel = methodChannel else {
print("\(channelName) methodChannel is nil")
return
}

registrar.addMethodCallDelegate(self, channel: methodChannel)
}

deinit {
detachFromEngine()
}

public func handle(_ call: FlutterMethodCall, result: @escaping FlutterResult) {
// Handle method calls for this method channel
switch call.method {
case Keys.Methods.getRegisteredDeviceToken:
result(CustomerIO.shared.registeredDeviceToken)

default:
result(FlutterMethodNotImplemented)
}
}

func detachFromEngine() {
methodChannel?.setMethodCallHandler(nil)
methodChannel = nil
}
}
2 changes: 2 additions & 0 deletions ios/Classes/SwiftCustomerIoPlugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ public class SwiftCustomerIoPlugin: NSObject, FlutterPlugin {

private var methodChannel: FlutterMethodChannel!
private var inAppMessagingChannelHandler: CusomterIOInAppMessaging!
private var messagingPushChannelHandler: CustomerIOMessagingPush!
private let logger: CioInternalCommon.Logger = DIGraphShared.shared.logger

public static func register(with registrar: FlutterPluginRegistrar) {
Expand All @@ -16,6 +17,7 @@ public class SwiftCustomerIoPlugin: NSObject, FlutterPlugin {
registrar.addMethodCallDelegate(instance, channel: instance.methodChannel)

instance.inAppMessagingChannelHandler = CusomterIOInAppMessaging(with: registrar)
instance.messagingPushChannelHandler = CustomerIOMessagingPush(with: registrar)
}

deinit {
Expand Down
1 change: 1 addition & 0 deletions lib/customer_io_const.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class MethodConsts {
static const String registerDeviceToken = "registerDeviceToken";
static const String onMessageReceived = "onMessageReceived";
static const String dismissMessage = "dismissMessage";
static const String getRegisteredDeviceToken = "getRegisteredDeviceToken";
}

class TrackingConsts {
Expand Down
13 changes: 13 additions & 0 deletions lib/messaging_push/method_channel.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,19 @@ class CustomerIOMessagingPushMethodChannel
@visibleForTesting
final methodChannel = const MethodChannel('customer_io_messaging_push');

@override
Future<String?> getRegisteredDeviceToken() {
try {
return methodChannel
.invokeMethod(MethodConsts.getRegisteredDeviceToken)
.then((result) => result as String?);
} on PlatformException catch (exception) {
handleException(exception);
return Future.error(
exception.message ?? "Error fetching registered device token");
}
}

@override
Future<bool> onMessageReceived(Map<String, dynamic> message,
{bool handleNotificationTrigger = true}) {
Expand Down
7 changes: 7 additions & 0 deletions lib/messaging_push/platform_interface.dart
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,13 @@ abstract class CustomerIOMessagingPushPlatform extends PlatformInterface {
_instance = instance;
}

/// Method to get the device token registered with the Customer.io SDK.
/// Returns a [Future] that resolves to the device token registered with
/// Customer.io SDK.
Future<String?> getRegisteredDeviceToken() {
throw UnimplementedError('getRegisteredDeviceToken() has not been implemented.');
}

/// Processes push notification received outside the CIO SDK. The method
/// displays notification on device and tracks CIO metrics for push
/// notification.
Expand Down

0 comments on commit 8197413

Please sign in to comment.