one
Version:
One is a React framework for web, iOS, and Android apps.
384 lines (381 loc) • 13.3 kB
JavaScript
import { Platform } from "react-native";
import { NitroModules } from "react-native-nitro-modules";
import { rethrowNativeError } from "../nativeError.native.js";
import { ForegroundHandler } from "./handlerState.native.js";
import { DEFAULT_ACTION_IDENTIFIER, assertCategoryActions, assertCategoryIdentifier, assertCategoryOptions, fromNativeAuthorizationStatus, fromNativeImportance, toNativeImportance } from "./types.native.js";
var hybrid;
function native() {
hybrid !== null && hybrid !== void 0 ? hybrid : hybrid = NitroModules.createHybridObject("OneNotifications");
return hybrid;
}
function mapPermissionResponse(response) {
return {
status: response.status,
granted: response.granted,
canAskAgain: response.canAskAgain,
...response.ios ? { ios: { status: fromNativeAuthorizationStatus(response.ios.status) } } : {}
};
}
async function getPermissions() {
return mapPermissionResponse(await native().getPermissions().catch(rethrowNativeError));
}
async function requestPermissions() {
var options = arguments.length > 0 && arguments[0] !== void 0 ? arguments[0] : {};
return mapPermissionResponse(await native().requestPermissions(options).catch(rethrowNativeError));
}
async function getBadgeCount() {
return native().getBadgeCount().catch(rethrowNativeError);
}
async function setBadgeCount(count) {
return native().setBadgeCount(count).catch(rethrowNativeError);
}
function mapChannel(channel) {
return {
id: channel.id,
name: channel.name,
importance: fromNativeImportance(channel.importance),
description: channel.description,
sound: channel.sound,
vibrationPattern: channel.vibrationPattern,
showBadge: channel.showBadge
};
}
async function setChannel(channelId, channel) {
var importance = toNativeImportance(channel.importance);
if (Platform.OS !== "android") return null;
var created = await native().setNotificationChannel(channelId, {
...channel,
importance
}).catch(rethrowNativeError);
return created ? mapChannel(created) : null;
}
async function getChannel(channelId) {
if (Platform.OS !== "android") return null;
var found = await native().getNotificationChannel(channelId).catch(rethrowNativeError);
return found ? mapChannel(found) : null;
}
async function getChannels() {
if (Platform.OS !== "android") return [];
var channels = await native().getNotificationChannels().catch(rethrowNativeError);
return channels.map(mapChannel);
}
async function deleteChannel(channelId) {
if (Platform.OS !== "android") return;
await native().deleteNotificationChannel(channelId).catch(rethrowNativeError);
}
function mapTrigger(trigger) {
switch (trigger.type) {
case "timeInterval":
var _trigger_seconds, _trigger_repeats;
return {
type: "timeInterval",
seconds: (_trigger_seconds = trigger.seconds) !== null && _trigger_seconds !== void 0 ? _trigger_seconds : 0,
repeats: (_trigger_repeats = trigger.repeats) !== null && _trigger_repeats !== void 0 ? _trigger_repeats : false
};
case "date":
var _trigger_date;
return {
type: "date",
date: (_trigger_date = trigger.date) !== null && _trigger_date !== void 0 ? _trigger_date : 0
};
case "push": return { type: "push" };
default: return { type: "unknown" };
}
}
function mapContent(content) {
var _content_title, _content_subtitle, _content_body, _content_badge, _content_categoryIdentifier;
return {
title: (_content_title = content.title) !== null && _content_title !== void 0 ? _content_title : null,
subtitle: (_content_subtitle = content.subtitle) !== null && _content_subtitle !== void 0 ? _content_subtitle : null,
body: (_content_body = content.body) !== null && _content_body !== void 0 ? _content_body : null,
data: content.data,
sound: content.sound,
badge: (_content_badge = content.badge) !== null && _content_badge !== void 0 ? _content_badge : null,
categoryIdentifier: (_content_categoryIdentifier = content.categoryIdentifier) !== null && _content_categoryIdentifier !== void 0 ? _content_categoryIdentifier : null
};
}
function mapRequest(request) {
return {
identifier: request.identifier,
content: mapContent(request.content),
trigger: mapTrigger(request.trigger)
};
}
function mapNotification(notification) {
return {
request: mapRequest(notification.request),
date: notification.date
};
}
function mapResponse(response) {
var mapped = {
notification: mapNotification(response.notification),
actionIdentifier: response.actionIdentifier
};
if (response.userText !== void 0) {
mapped.userText = response.userText;
}
return mapped;
}
function toNativeAction(action) {
var _ref, _ref1, _ref2;
var _action_options, _action_options1, _action_options2;
var native2 = {
identifier: action.identifier,
buttonTitle: action.buttonTitle,
authenticationRequired: (_ref = (_action_options = action.options) === null || _action_options === void 0 ? void 0 : _action_options.isAuthenticationRequired) !== null && _ref !== void 0 ? _ref : false,
destructive: (_ref1 = (_action_options1 = action.options) === null || _action_options1 === void 0 ? void 0 : _action_options1.isDestructive) !== null && _ref1 !== void 0 ? _ref1 : false,
foreground: (_ref2 = (_action_options2 = action.options) === null || _action_options2 === void 0 ? void 0 : _action_options2.opensAppToForeground) !== null && _ref2 !== void 0 ? _ref2 : true
};
if (action.textInput !== void 0) {
native2.textInputPlaceholder = action.textInput.placeholder;
if (action.textInput.submitButtonTitle !== void 0) {
native2.textInputSubmitTitle = action.textInput.submitButtonTitle;
}
}
return native2;
}
function fromNativeAction(action) {
var mapped = {
identifier: action.identifier,
buttonTitle: action.buttonTitle,
options: {
isAuthenticationRequired: action.authenticationRequired,
isDestructive: action.destructive,
opensAppToForeground: action.foreground
}
};
if (action.textInputPlaceholder !== void 0) {
mapped.textInput = { placeholder: action.textInputPlaceholder };
if (action.textInputSubmitTitle !== void 0) {
mapped.textInput.submitButtonTitle = action.textInputSubmitTitle;
}
}
return mapped;
}
function mapCategory(category) {
var mapped = {
identifier: category.identifier,
actions: category.actions.map(fromNativeAction)
};
if (category.options !== void 0) {
var options = {};
if (category.options.allowInCarPlay !== void 0) {
options.allowInCarPlay = category.options.allowInCarPlay;
}
if (category.options.categorySummaryFormat !== void 0) {
options.categorySummaryFormat = category.options.categorySummaryFormat;
}
if (category.options.customDismissAction !== void 0) {
options.customDismissAction = category.options.customDismissAction;
}
if (category.options.intentIdentifiers !== void 0) {
options.intentIdentifiers = [...category.options.intentIdentifiers];
}
if (category.options.previewPlaceholder !== void 0) {
options.previewPlaceholder = category.options.previewPlaceholder;
}
if (category.options.showTitle !== void 0) {
options.showTitle = category.options.showTitle;
}
if (category.options.showSubtitle !== void 0) {
options.showSubtitle = category.options.showSubtitle;
}
mapped.options = options;
}
return mapped;
}
function setCategory(identifier, actions, options) {
assertCategoryIdentifier(identifier, "setCategory");
assertCategoryActions(actions, "setCategory");
assertCategoryOptions(options, "setCategory");
var category = {
identifier,
actions: actions.map(toNativeAction)
};
if (options !== void 0) {
var { allowAnnouncement: _ignored, ...nativeOptions } = options;
category.options = { ...nativeOptions };
}
return native().setNotificationCategory(category).catch(rethrowNativeError).then(mapCategory);
}
function getCategories() {
return native().getNotificationCategories().catch(rethrowNativeError).then(function(categories) {
return categories.map(mapCategory);
});
}
function deleteCategory(identifier) {
assertCategoryIdentifier(identifier, "deleteCategory");
return native().deleteNotificationCategory(identifier).catch(rethrowNativeError);
}
function toValue(value) {
if (value === null || typeof value === "string" || typeof value === "number" || typeof value === "boolean") {
return value;
}
if (Array.isArray(value)) return value.map(function(item) {
var _toValue;
return (_toValue = toValue(item)) !== null && _toValue !== void 0 ? _toValue : null;
});
if (typeof value === "object") return toAnyMap(Object.entries(value));
return void 0;
}
function toAnyMap(entries) {
var map = {};
var _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = void 0;
try {
for (var _iterator = entries[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
var [key, item] = _step.value;
var value = toValue(item);
if (value !== void 0) map[key] = value;
}
} catch (err) {
_didIteratorError = true;
_iteratorError = err;
} finally {
try {
if (!_iteratorNormalCompletion && _iterator.return != null) {
_iterator.return();
}
} finally {
if (_didIteratorError) {
throw _iteratorError;
}
}
}
return map;
}
var receivedListeners = /* @__PURE__ */ new Set();
var responseListeners = /* @__PURE__ */ new Set();
var pushTokenListeners = /* @__PURE__ */ new Set();
var sharedRunner = null;
function events(module) {
if (!sharedRunner) {
var runner = new ForegroundHandler(function(requestId, behavior) {
return module.presentNotification(requestId, behavior);
});
module.setListeners(function(requestId, nativeNotification) {
var notification = mapNotification(nativeNotification);
if (!runner.receive(requestId, notification)) return;
receivedListeners.forEach(function(listener) {
return listener(notification);
});
}, function(nativeResponse) {
var response = mapResponse(nativeResponse);
responseListeners.forEach(function(listener) {
return listener(response);
});
}, function(token) {
if (token.type !== "ios" && token.type !== "android") return;
var mapped = {
type: token.type,
data: token.data
};
pushTokenListeners.forEach(function(listener) {
return listener(mapped);
});
});
sharedRunner = runner;
}
return sharedRunner;
}
function subscribe(listeners, listener) {
events(native());
listeners.add(listener);
return { remove: function remove() {
listeners.delete(listener);
} };
}
function addReceivedListener(listener) {
return subscribe(receivedListeners, listener);
}
function addResponseReceivedListener(listener) {
return subscribe(responseListeners, listener);
}
async function getDevicePushToken() {
var token = await native().getDevicePushToken().catch(rethrowNativeError);
var type = token && (token.type === "ios" || token.type === "android") ? token.type : null;
if (!token || type === null || typeof token.data !== "string" || !token.data.length) {
throw new Error("Notifications.getDevicePushToken did not return a push token");
}
return {
type,
data: token.data
};
}
function addPushTokenListener(listener) {
return subscribe(pushTokenListeners, listener);
}
function setHandler(handler) {
events(native()).setHandler(handler);
}
function getLastResponse() {
var response = native().getLastNotificationResponse();
return response ? mapResponse(response) : null;
}
function clearLastResponse() {
native().clearLastNotificationResponse();
}
async function schedule(request) {
var { content, trigger } = request;
return native().scheduleNotification({
identifier: request.identifier,
content: {
...content,
data: content.data ? toAnyMap(Object.entries(content.data)) : void 0
},
trigger: trigger == null ? void 0 : trigger.type === "date" ? {
...trigger,
date: trigger.date instanceof Date ? trigger.date.getTime() : trigger.date
} : trigger
}).catch(rethrowNativeError);
}
async function cancelScheduled(identifier) {
await native().cancelScheduledNotification(identifier).catch(rethrowNativeError);
}
async function cancelAllScheduled() {
await native().cancelAllScheduledNotifications().catch(rethrowNativeError);
}
async function getAllScheduled() {
var scheduled = await native().getAllScheduledNotifications().catch(rethrowNativeError);
return scheduled.map(mapRequest);
}
async function getPresented() {
var presented = await native().getPresentedNotifications().catch(rethrowNativeError);
return presented.map(mapNotification);
}
async function dismiss(identifier) {
await native().dismissNotification(identifier).catch(rethrowNativeError);
}
async function dismissAll() {
await native().dismissAllNotifications().catch(rethrowNativeError);
}
var Notifications = Object.freeze({
getPermissions,
requestPermissions,
getBadgeCount,
setBadgeCount,
setCategory,
getCategories,
deleteCategory,
setChannel,
getChannel,
getChannels,
deleteChannel,
addReceivedListener,
addResponseReceivedListener,
getDevicePushToken,
addPushTokenListener,
setHandler,
getLastResponse,
clearLastResponse,
schedule,
cancelScheduled,
cancelAllScheduled,
getAllScheduled,
getPresented,
dismiss,
dismissAll,
DEFAULT_ACTION_IDENTIFIER
});
export { Notifications };
//# sourceMappingURL=index.native.js.map