Skip to content

Commit 7cc69e7

Browse files
committed
fix Airship Actions running
1 parent 45c071b commit 7cc69e7

File tree

11 files changed

+69
-53
lines changed

11 files changed

+69
-53
lines changed

android/src/main/java/com/urbanairship/reactnative/AirshipModule.kt

+2-2
Original file line numberDiff line numberDiff line change
@@ -455,9 +455,9 @@ class AirshipModule internal constructor(val context: ReactApplicationContext) :
455455
}
456456

457457
@ReactMethod
458-
override fun actionRun(name: String?, value: Dynamic?, promise: Promise) {
458+
override fun actionRun(action: ReadableMap, promise: Promise) {
459459
promise.resolveDeferred<ActionValue> { callback ->
460-
proxy.actions.runAction(requireNotNull(name), Utils.convertDynamic(value))
460+
proxy.actions.runAction(requireNotNull(action.getString("_name")), Utils.convertDynamic(action.getDynamic("_value")))
461461
.addResultCallback { actionResult ->
462462
if (actionResult != null && actionResult.status == ActionResult.STATUS_COMPLETED) {
463463
callback(actionResult.value, null)

android/src/oldarch/java/com/urbanairship/reactnative/AirshipSpec.kt

+1-2
Original file line numberDiff line numberDiff line change
@@ -240,8 +240,7 @@ abstract class AirshipSpec internal constructor(context: ReactApplicationContext
240240
@ReactMethod
241241
@com.facebook.proguard.annotations.DoNotStrip
242242
abstract fun actionRun(
243-
name: String?,
244-
value: Dynamic?,
243+
action: ReadableMap,
245244
promise: Promise
246245
)
247246

example/src/App.tsx

-29
Original file line numberDiff line numberDiff line change
@@ -8,40 +8,11 @@ import MessageCenterScreen from './screens/MessageCenterScreen';
88
import MessageScreen from './screens/MessageScreen';
99
import PreferenceCenterScreen from './screens/PreferenceCenterScreen';
1010
import Airship, { EventType } from '@ua/react-native-airship';
11-
import { CustomEvent } from '@ua/react-native-airship';
1211
import MaterialIcons from 'react-native-vector-icons/MaterialIcons';
1312

1413
const Tab = createBottomTabNavigator();
1514
const MessageCenterStack = createStackNavigator();
1615

17-
Airship.takeOff({
18-
default: {
19-
appKey: "VWDwdOFjRTKLRxCeXTVP6g",
20-
appSecret: "5Ifi5rYgTm2QHey9JkP0WA",
21-
logLevel: "verbose"
22-
},
23-
site: "us", // use "eu" for EU cloud projects
24-
urlAllowList: ["*"],
25-
android: {
26-
notificationConfig: {
27-
icon: "ic_notification",
28-
accentColor: "#00ff00"
29-
}
30-
}
31-
});
32-
33-
var customEvent = new CustomEvent("event_name", 123.12);
34-
customEvent.addProperty("my_custom_property", "some custom value");
35-
customEvent.addProperty("is_neat", true);
36-
customEvent.addProperty("any_json", {
37-
"foo": "bar"
38-
});
39-
Airship.analytics.addCustomEvent(customEvent);
40-
41-
42-
var url: string = "ulrich://some-deep-link"
43-
Airship.actions.run("deep_link_action", url);
44-
4516
Airship.addListener(EventType.NotificationResponse, (event) => {
4617
console.log('NotificationResponse:', JSON.stringify(event));
4718
});

ios/AirshipReactNative.swift

+3-3
Original file line numberDiff line numberDiff line change
@@ -328,10 +328,10 @@ public extension AirshipReactNative {
328328
// Actions
329329
@objc
330330
public extension AirshipReactNative {
331-
func actionsRun(actionName: String, actionValue: Any?) async throws-> Any? {
331+
func actionsRun(action: [String: Any]) async throws-> Any? {
332332
return try await AirshipProxy.shared.action.runAction(
333-
actionName,
334-
value: try AirshipJSON.wrap(actionValue)
333+
action["_name"] as! String,
334+
value: action["_value"] is NSNull ? nil : try AirshipJSON.wrap(action["_value"])
335335
)
336336
}
337337
}

ios/RTNAirship.mm

+2-3
Original file line numberDiff line numberDiff line change
@@ -336,11 +336,10 @@ + (BOOL)requiresMainQueueSetup {
336336
}
337337

338338
RCT_REMAP_METHOD(actionRun,
339-
actionRun:(NSString *)name value:(NSDictionary *)value
339+
actionRun:(NSDictionary *)action
340340
resolve:(RCTPromiseResolveBlock)resolve
341341
reject:(RCTPromiseRejectBlock)reject) {
342-
[AirshipReactNative.shared actionsRunWithActionName:name
343-
actionValue:value
342+
[AirshipReactNative.shared actionsRunWithAction:action
344343
completionHandler:^(id result , NSError *error) {
345344

346345

src/Action.ts

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/* Copyright Airship and Contributors */
2+
3+
'use strict';
4+
5+
import { JsonValue } from './types';
6+
7+
/**
8+
* Airship Action Object.
9+
* This is used to encapsulate the Action name and the Action value.
10+
*/
11+
export class Action {
12+
_name: string;
13+
_value?: JsonValue;
14+
15+
/**
16+
* Airship Action constructor.
17+
*
18+
* @param name The action name.
19+
* @param value The action value.
20+
*/
21+
constructor(name: string, value?: JsonValue) {
22+
this._name = name;
23+
this._value = value;
24+
}
25+
26+
/**
27+
* Sets the action value.
28+
*
29+
* @param value The action value.
30+
*/
31+
setValue(value?: JsonValue) {
32+
this._value = value;
33+
}
34+
}

src/AirshipActions.ts

+4-5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { JsonValue } from './types';
2+
import { Action } from './Action';
23

34
/**
45
* Airship actions.
@@ -9,15 +10,13 @@ export class AirshipActions {
910
/**
1011
* Runs an Airship action.
1112
*
12-
* @param name The name of the action.
13-
* @param value The action's value.
13+
* @param action The Airship Action.
1414
* @return A promise that returns the action result if the action
1515
* successfully runs, or the Error if the action was unable to be run.
1616
*/
1717
public run(
18-
actionName: string,
19-
actionValue?: JsonValue
18+
action: Action
2019
): Promise<JsonValue | null | undefined> {
21-
return this.module.actionRun(actionName, actionValue);
20+
return this.module.actionRun(action);
2221
}
2322
}

src/AirshipAnalytics.ts

+3-7
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { Action } from "./Action";
12
import { CustomEvent } from "./CustomEvent";
23

34
/**
@@ -33,15 +34,10 @@ export class AirshipAnalytics {
3334
* custom event is rejected.
3435
*/
3536
public addCustomEvent(event: CustomEvent): Promise<null | Error> {
36-
const actionArg = {
37-
event_name: event._name,
38-
event_value: event._value,
39-
transaction_id: event._transactionId,
40-
properties: event._properties
41-
}
37+
let action = new Action("add_custom_event_action", event.toJsonValue())
4238

4339
return new Promise((resolve, reject) => {
44-
this.module.actionRun("add_custom_event_action", actionArg).then(() => {
40+
this.module.actionRun(action).then(() => {
4541
resolve(null)
4642
}, (error: Error) => {
4743
reject(error)

src/CustomEvent.ts

+18
Original file line numberDiff line numberDiff line change
@@ -48,4 +48,22 @@ export class CustomEvent {
4848
addProperty(name: string, value: JsonValue) {
4949
this._properties[name] = value;
5050
}
51+
52+
/**
53+
* Converts a CustomEvent into a JsonValue.
54+
*
55+
* @returns A JsonValue.
56+
*/
57+
toJsonValue(): JsonValue {
58+
let jsonObject: JsonObject = {};
59+
jsonObject.event_name = this._name;
60+
if (this._value) {
61+
jsonObject.event_value = this._value;
62+
}
63+
jsonObject.properties = this._properties;
64+
if (this._transactionId) {
65+
jsonObject.transaction_id = this._transactionId;
66+
}
67+
return jsonObject;
68+
}
5169
}

src/NativeRTNAirship.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import type { TurboModule } from 'react-native';
22
import { TurboModuleRegistry } from 'react-native';
3-
import { JsonValue } from './types';
43

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

7170
// Action
72-
actionRun(name: string, value?: JsonValue): Promise<Object | Error>;
71+
actionRun(action: Object): Promise<Object | Error>;
7372

7473
// Privacy Manager
7574
privacyManagerSetEnabledFeatures(features: string[]): Promise<void>;

src/index.tsx

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ export { SubscriptionListEditor } from './SubscriptionListEditor';
1919
export { TagGroupEditor } from './TagGroupEditor';
2020
export { ScopedSubscriptionListEditor } from './ScopedSubscriptionListEditor';
2121
export { AttributeEditor } from './AttributeEditor';
22+
export { Action } from './Action';
2223

2324
export * from './types';
2425
export * from './MessageView';

0 commit comments

Comments
 (0)