Skip to content

Commit

Permalink
fix Airship Actions running
Browse files Browse the repository at this point in the history
  • Loading branch information
Ulrico972 committed Apr 5, 2024
1 parent 45c071b commit 7cc69e7
Show file tree
Hide file tree
Showing 11 changed files with 69 additions and 53 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -455,9 +455,9 @@ class AirshipModule internal constructor(val context: ReactApplicationContext) :
}

@ReactMethod
override fun actionRun(name: String?, value: Dynamic?, promise: Promise) {
override fun actionRun(action: ReadableMap, promise: Promise) {
promise.resolveDeferred<ActionValue> { callback ->
proxy.actions.runAction(requireNotNull(name), Utils.convertDynamic(value))
proxy.actions.runAction(requireNotNull(action.getString("_name")), Utils.convertDynamic(action.getDynamic("_value")))
.addResultCallback { actionResult ->
if (actionResult != null && actionResult.status == ActionResult.STATUS_COMPLETED) {
callback(actionResult.value, null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -240,8 +240,7 @@ abstract class AirshipSpec internal constructor(context: ReactApplicationContext
@ReactMethod
@com.facebook.proguard.annotations.DoNotStrip
abstract fun actionRun(
name: String?,
value: Dynamic?,
action: ReadableMap,
promise: Promise
)

Expand Down
29 changes: 0 additions & 29 deletions example/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,40 +8,11 @@ import MessageCenterScreen from './screens/MessageCenterScreen';
import MessageScreen from './screens/MessageScreen';
import PreferenceCenterScreen from './screens/PreferenceCenterScreen';
import Airship, { EventType } from '@ua/react-native-airship';
import { CustomEvent } from '@ua/react-native-airship';
import MaterialIcons from 'react-native-vector-icons/MaterialIcons';

const Tab = createBottomTabNavigator();
const MessageCenterStack = createStackNavigator();

Airship.takeOff({
default: {
appKey: "VWDwdOFjRTKLRxCeXTVP6g",
appSecret: "5Ifi5rYgTm2QHey9JkP0WA",
logLevel: "verbose"
},
site: "us", // use "eu" for EU cloud projects
urlAllowList: ["*"],
android: {
notificationConfig: {
icon: "ic_notification",
accentColor: "#00ff00"
}
}
});

var customEvent = new CustomEvent("event_name", 123.12);
customEvent.addProperty("my_custom_property", "some custom value");
customEvent.addProperty("is_neat", true);
customEvent.addProperty("any_json", {
"foo": "bar"
});
Airship.analytics.addCustomEvent(customEvent);


var url: string = "ulrich://some-deep-link"
Airship.actions.run("deep_link_action", url);

Airship.addListener(EventType.NotificationResponse, (event) => {
console.log('NotificationResponse:', JSON.stringify(event));
});
Expand Down
6 changes: 3 additions & 3 deletions ios/AirshipReactNative.swift
Original file line number Diff line number Diff line change
Expand Up @@ -328,10 +328,10 @@ public extension AirshipReactNative {
// Actions
@objc
public extension AirshipReactNative {
func actionsRun(actionName: String, actionValue: Any?) async throws-> Any? {
func actionsRun(action: [String: Any]) async throws-> Any? {
return try await AirshipProxy.shared.action.runAction(
actionName,
value: try AirshipJSON.wrap(actionValue)
action["_name"] as! String,
value: action["_value"] is NSNull ? nil : try AirshipJSON.wrap(action["_value"])
)
}
}
Expand Down
5 changes: 2 additions & 3 deletions ios/RTNAirship.mm
Original file line number Diff line number Diff line change
Expand Up @@ -336,11 +336,10 @@ + (BOOL)requiresMainQueueSetup {
}

RCT_REMAP_METHOD(actionRun,
actionRun:(NSString *)name value:(NSDictionary *)value
actionRun:(NSDictionary *)action
resolve:(RCTPromiseResolveBlock)resolve
reject:(RCTPromiseRejectBlock)reject) {
[AirshipReactNative.shared actionsRunWithActionName:name
actionValue:value
[AirshipReactNative.shared actionsRunWithAction:action
completionHandler:^(id result , NSError *error) {


Expand Down
34 changes: 34 additions & 0 deletions src/Action.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/* Copyright Airship and Contributors */

'use strict';

import { JsonValue } from './types';

/**
* Airship Action Object.
* This is used to encapsulate the Action name and the Action value.
*/
export class Action {
_name: string;
_value?: JsonValue;

/**
* Airship Action constructor.
*
* @param name The action name.
* @param value The action value.
*/
constructor(name: string, value?: JsonValue) {
this._name = name;
this._value = value;
}

/**
* Sets the action value.
*
* @param value The action value.
*/
setValue(value?: JsonValue) {
this._value = value;
}
}
9 changes: 4 additions & 5 deletions src/AirshipActions.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { JsonValue } from './types';
import { Action } from './Action';

/**
* Airship actions.
Expand All @@ -9,15 +10,13 @@ export class AirshipActions {
/**
* Runs an Airship action.
*
* @param name The name of the action.
* @param value The action's value.
* @param action The Airship Action.
* @return A promise that returns the action result if the action
* successfully runs, or the Error if the action was unable to be run.
*/
public run(
actionName: string,
actionValue?: JsonValue
action: Action
): Promise<JsonValue | null | undefined> {
return this.module.actionRun(actionName, actionValue);
return this.module.actionRun(action);
}
}
10 changes: 3 additions & 7 deletions src/AirshipAnalytics.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { Action } from "./Action";
import { CustomEvent } from "./CustomEvent";

/**
Expand Down Expand Up @@ -33,15 +34,10 @@ export class AirshipAnalytics {
* custom event is rejected.
*/
public addCustomEvent(event: CustomEvent): Promise<null | Error> {
const actionArg = {
event_name: event._name,
event_value: event._value,
transaction_id: event._transactionId,
properties: event._properties
}
let action = new Action("add_custom_event_action", event.toJsonValue())

return new Promise((resolve, reject) => {
this.module.actionRun("add_custom_event_action", actionArg).then(() => {
this.module.actionRun(action).then(() => {
resolve(null)
}, (error: Error) => {
reject(error)
Expand Down
18 changes: 18 additions & 0 deletions src/CustomEvent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,22 @@ export class CustomEvent {
addProperty(name: string, value: JsonValue) {
this._properties[name] = value;
}

/**
* Converts a CustomEvent into a JsonValue.
*
* @returns A JsonValue.
*/
toJsonValue(): JsonValue {
let jsonObject: JsonObject = {};
jsonObject.event_name = this._name;
if (this._value) {
jsonObject.event_value = this._value;
}
jsonObject.properties = this._properties;
if (this._transactionId) {
jsonObject.transaction_id = this._transactionId;
}
return jsonObject;
}
}
3 changes: 1 addition & 2 deletions src/NativeRTNAirship.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import type { TurboModule } from 'react-native';
import { TurboModuleRegistry } from 'react-native';
import { JsonValue } from './types';

export interface Spec extends TurboModule {
// Airship
Expand Down Expand Up @@ -69,7 +68,7 @@ export interface Spec extends TurboModule {
analyticsAssociateIdentifier(key: string, identifier?: string): Promise<void>;

// Action
actionRun(name: string, value?: JsonValue): Promise<Object | Error>;
actionRun(action: Object): Promise<Object | Error>;

// Privacy Manager
privacyManagerSetEnabledFeatures(features: string[]): Promise<void>;
Expand Down
1 change: 1 addition & 0 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export { SubscriptionListEditor } from './SubscriptionListEditor';
export { TagGroupEditor } from './TagGroupEditor';
export { ScopedSubscriptionListEditor } from './ScopedSubscriptionListEditor';
export { AttributeEditor } from './AttributeEditor';
export { Action } from './Action';

export * from './types';
export * from './MessageView';
Expand Down

0 comments on commit 7cc69e7

Please sign in to comment.