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

feat: fetch currently stored device token using JS method #216

Merged
merged 12 commits into from
Nov 1, 2023
9 changes: 9 additions & 0 deletions Apps/APN/src/screens/Settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import { useCustomerIoSdkContext } from '../state/customerIoSdkState';
import { useUserStateContext } from '../state/userState';
import { resetRoute } from '../utils/navigation';
import Prompts from '../utils/prompts';
import { CustomerIO } from 'customerio-reactnative';

const Settings = ({ navigation, route }) => {
const { params } = route;
Expand Down Expand Up @@ -85,6 +86,14 @@ const Settings = ({ navigation, route }) => {
);

useEffect(() => {
CustomerIO.pushMessaging()
.getRegisteredDeviceToken()
.then((token) => {
setDeviceToken(token);
})
.catch((error) => {
console.log(error);
});
setTrackUrl(initialConfig.trackingUrl);
setSiteId(initialSiteId ?? initialConfig.siteId);
setApiKey(initialApiKey ?? initialConfig.apiKey);
Expand Down
9 changes: 9 additions & 0 deletions Apps/FCM/src/screens/Settings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import { useCustomerIoSdkContext } from '../state/customerIoSdkState';
import { useUserStateContext } from '../state/userState';
import { resetRoute } from '../utils/navigation';
import Prompts from '../utils/prompts';
import { CustomerIO } from 'customerio-reactnative';

interface SettingsProps {
navigation: NavigationProp<ParamListBase>;
Expand Down Expand Up @@ -97,6 +98,14 @@ const Settings: React.FC<SettingsProps> = ({ navigation, route }) => {
}
};

CustomerIO.pushMessaging()
.getRegisteredDeviceToken()
.then((token) => {
setDeviceToken(token);
})
.catch((error) => {
console.log(error);
});
setValueIfPresent(initialConfig?.trackingUrl, setTrackUrl);
setValueIfPresent(initialSiteId ?? initialConfig?.siteId, setSiteId);
setValueIfPresent(initialApiKey ?? initialConfig?.apiKey, setApiKey);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,27 @@ class RNCIOPushMessaging(
}
}

/**
* Get the registered device token for the app.
* @returns Promise with device token as a string, or error if no token is
* registered or the method fails to fetch token.
*/
@ReactMethod
fun getRegisteredDeviceToken(promise: Promise) {
try {
// Get the device token from SDK
val deviceToken: String? = CustomerIO.instance().registeredDeviceToken

if (deviceToken != null) {
promise.resolve(deviceToken)
} else {
promise.reject("device_token_not_found", "The device token is not available.")
}
Comment on lines +128 to +132
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would have preferred to have the Promise resolve null instead of reject when the native device token is null.

A device not yet having a device token is not an error, IMO.

} catch (e: Exception) {
promise.reject("error_getting_device_token", "Error fetching registered device token.", e)
}
}

/**
* Checks current permission of push notification permission
*/
Expand Down
4 changes: 4 additions & 0 deletions ios/CustomerioPushMessaging.m
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,8 @@ @interface RCT_EXTERN_MODULE(CustomerioPushMessaging, NSObject)

RCT_EXTERN_METHOD(trackNotificationReceived : (nonnull NSDictionary *) payload])

RCT_EXTERN_METHOD(getRegisteredDeviceToken: (RCTPromiseResolveBlock) resolver
rejecter:(RCTPromiseRejectBlock)rejecter)


@end
11 changes: 11 additions & 0 deletions ios/CustomerioPushMessaging.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import Foundation
import CioInternalCommon
import CioTracking
import CioMessagingPush


Expand All @@ -23,6 +24,16 @@ class CustomerioPushMessaging: NSObject {
trackPushMetrics(payload: payload, event: .delivered)
}

@objc(getRegisteredDeviceToken:rejecter:)
func getRegisteredDeviceToken(resolver resolve: @escaping(RCTPromiseResolveBlock), rejecter reject: @escaping(RCTPromiseRejectBlock)) -> Void {

guard let token = CustomerIO.shared.registeredDeviceToken else {
reject(CustomerioConstants.cioTag, CustomerioConstants.showDeviceTokenFailureError, nil)
return
}
resolve(token)
}

private func trackPushMetrics(payload: NSDictionary, event : Metric) {
guard let deliveryId = payload[CustomerioConstants.CioDeliveryId] as? String, let deviceToken = payload[CustomerioConstants.CioDeliveryToken] as? String else
{return}
Expand Down
2 changes: 1 addition & 1 deletion ios/constants/CustomerioConstants.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ struct CustomerioConstants {

static let cioTag = "[CIO]"
static let showPromptFailureError = "Error requesting push notification permission."

static let showDeviceTokenFailureError = "Error fetching registered device token."
static let platformiOS = "ios"
static let sound = "sound"
static let badge = "badge"
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"react-native": "src/index",
"source": "src/index",
"expoVersion": "",
"cioNativeiOSSdkVersion": "= 2.8.5",
"cioNativeiOSSdkVersion": "= 2.9.0",
"files": [
"src",
"lib",
Expand Down
9 changes: 9 additions & 0 deletions src/CustomerIOPushMessaging.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,17 @@
PushMessagingNative.trackNotificationReceived(payload);
}

/**
* Get the registered device token for the app.
* @returns Promise with device token as a string, or error if no token is
* registered or the method fails to fetch token.
*/
getRegisteredDeviceToken(): Promise<string> {
return PushMessagingNative.getRegisteredDeviceToken();
}

isAndroid(): boolean {
return Platform.OS == 'android';

Check warning on line 104 in src/CustomerIOPushMessaging.tsx

View workflow job for this annotation

GitHub Actions / eslint results

src/CustomerIOPushMessaging.tsx#L104

[eqeqeq] Expected '===' and instead saw '=='.
}
}

Expand Down
Loading