UNPKG

react-native-onesignal

Version:
508 lines (507 loc) 17.1 kB
import { NativeModules as x, Platform as P, NativeEventEmitter as se } from "react-native"; const N = "OneSignal-notificationWillDisplayInForeground", M = "OneSignal-notificationClicked", j = "OneSignal-inAppMessageClicked", q = "OneSignal-inAppMessageWillDisplay", R = "OneSignal-inAppMessageDidDisplay", G = "OneSignal-inAppMessageWillDismiss", W = "OneSignal-inAppMessageDidDismiss", k = "OneSignal-permissionChanged", F = "OneSignal-subscriptionChanged", V = "OneSignal-userStateChanged", ae = x.OneSignal; class ue { constructor(r) { this.body = r.body, this.sound = r.sound, this.title = r.title, this.launchURL = r.launchURL, this.rawPayload = r.rawPayload, this.actionButtons = r.actionButtons, this.additionalData = r.additionalData, this.notificationId = r.notificationId, de() ? (this.groupKey = r.groupKey, this.ledColor = r.ledColor, this.priority = r.priority, this.smallIcon = r.smallIcon, this.largeIcon = r.largeIcon, this.bigPicture = r.bigPicture, this.collapseId = r.collapseId, this.groupMessage = r.groupMessage, this.fromProjectNumber = r.fromProjectNumber, this.smallIconAccentColor = r.smallIconAccentColor, this.lockScreenVisibility = r.lockScreenVisibility, this.androidNotificationId = r.androidNotificationId) : le() && (this.badge = r.badge, this.category = r.category, this.threadId = r.threadId, this.subtitle = r.subtitle, this.templateId = r.templateId, this.attachments = r.attachments, this.templateName = r.templateName, this.mutableContent = r.mutableContent, this.badgeIncrement = r.badgeIncrement, this.contentAvailable = r.contentAvailable, this.relevanceScore = r.relevanceScore, this.interruptionLevel = r.interruptionLevel); } display() { ae.displayNotification(this.notificationId); } } const de = (n) => P.OS === "android", le = (n) => P.OS === "ios", ce = x.OneSignal; class fe { constructor(r) { this.notification = new ue(r); } preventDefault() { ce.preventDefault(this.notification.notificationId); } getNotification() { return this.notification; } } const B = [ k, F, V, N, M, j, q, G, W, R ]; class ge { constructor(r) { this.RNOneSignal = r, this.oneSignalEventEmitter = new se(r), this.eventListenerArrayMap = /* @__PURE__ */ new Map(), this.listeners = {}, this.setupListeners(); } setupListeners() { if (this.RNOneSignal != null) for (let r = 0; r < B.length; r++) { let h = B[r]; this.listeners[h] = this.generateEventListener(h); } } /** * Adds the event handler to the corresponding handler array on the JS side of the bridge * @param {string} eventName * @param {function} handler * @returns void */ addEventListener(r, h) { let m = this.eventListenerArrayMap.get(r); m && m.length > 0 ? m.push(h) : this.eventListenerArrayMap.set(r, [h]); } /** * clears the event handler(s) for the event name * @param {string} eventName * @param {function} handler * @returns void */ removeEventListener(r, h) { const m = this.eventListenerArrayMap.get(r); if (!m) return; const y = m.indexOf(h); y !== -1 && m.splice(y, 1), m.length === 0 && this.eventListenerArrayMap.delete(r); } // returns an event listener with the js to native mapping generateEventListener(r) { const h = (m) => { let y = this.eventListenerArrayMap.get(r); if (y) if (r === N) y.forEach((E) => { E( new fe(m) ); }); else if (r === k) { const E = m; y.forEach((t) => { t(E.permission); }); } else y.forEach((E) => { E(m); }); }; return this.oneSignalEventEmitter.addListener( r, h ); } } function me(n) { return n && n.__esModule && Object.prototype.hasOwnProperty.call(n, "default") ? n.default : n; } var C, K; function he() { if (K) return C; K = 1; var n = function(r, h, m, y, E, t, p, v) { if (process.env.NODE_ENV !== "production" && h === void 0) throw new Error("invariant requires an error message argument"); if (!r) { var a; if (h === void 0) a = new Error( "Minified exception occurred; use the non-minified dev environment for the full error message and additional helpful warnings." ); else { var f = [m, y, E, t, p, v], I = 0; a = new Error( h.replace(/%s/g, function() { return f[I++]; }) ), a.name = "Invariant Violation"; } throw a.framesToPop = 1, a; } }; return C = n, C; } var pe = he(); const ve = /* @__PURE__ */ me(pe); function b(n) { ve(typeof n == "function", "Must provide a valid callback"); } function i(n) { return n == null ? (console.error( "Could not load RNOneSignal native module. Make sure native dependencies are properly linked." ), !1) : !0; } var Se = /* @__PURE__ */ ((n) => (n[n.NotDetermined = 0] = "NotDetermined", n[n.Denied = 1] = "Denied", n[n.Authorized = 2] = "Authorized", n[n.Provisional = 3] = "Provisional", n[n.Ephemeral = 4] = "Ephemeral", n))(Se || {}); const e = x.OneSignal, l = new ge(e); var ye = /* @__PURE__ */ ((n) => (n[n.None = 0] = "None", n[n.Fatal = 1] = "Fatal", n[n.Error = 2] = "Error", n[n.Warn = 3] = "Warn", n[n.Info = 4] = "Info", n[n.Debug = 5] = "Debug", n[n.Verbose = 6] = "Verbose", n))(ye || {}); let z = !1, O = { id: "", token: "", optedIn: !1 }; async function Ie() { _.Notifications.addEventListener( "permissionChange", (n) => { z = n; } ), z = await e.hasNotificationPermission(); } async function Ae() { _.User.pushSubscription.addEventListener( "change", (n) => { O = n.current; } ), O.id = await e.getPushSubscriptionId(), O.token = await e.getPushSubscriptionToken(), O.optedIn = await e.getOptedIn(); } var _; ((n) => { function r(t) { i(e) && (e.initialize(t), Ie(), Ae()); } n.initialize = r; function h(t) { i(e) && e.login(t); } n.login = h; function m() { i(e) && e.logout(); } n.logout = m; function y(t) { i(e) && e.setPrivacyConsentRequired(t); } n.setConsentRequired = y; function E(t) { i(e) && e.setPrivacyConsentGiven(t); } n.setConsentGiven = E, ((t) => { function p(a) { i(e) && e.setLogLevel(a); } t.setLogLevel = p; function v(a) { i(e) && e.setAlertLevel(a); } t.setAlertLevel = v; })(n.Debug || (n.Debug = {})), ((t) => { function p(g, S, L = () => { }) { i(e) && P.OS === "ios" && e.enterLiveActivity(g, S, L); } t.enter = p; function v(g, S = () => { }) { i(e) && P.OS === "ios" && e.exitLiveActivity(g, S); } t.exit = v; function a(g, S) { i(e) && P.OS === "ios" && e.setPushToStartToken(g, S); } t.setPushToStartToken = a; function f(g) { i(e) && P.OS === "ios" && e.removePushToStartToken(g); } t.removePushToStartToken = f; function I(g) { i(e) && P.OS === "ios" && e.setupDefaultLiveActivity(g); } t.setupDefault = I; function T(g, S, L) { i(e) && P.OS === "ios" && e.startDefaultLiveActivity(g, S, L); } t.startDefault = T; })(n.LiveActivities || (n.LiveActivities = {})), ((t) => { ((o) => { function c(oe, w) { i(e) && (b(w), e.addPushSubscriptionObserver(), l.addEventListener(F, w)); } o.addEventListener = c; function D(oe, w) { i(e) && l.removeEventListener(F, w); } o.removeEventListener = D; function X() { return i(e) ? (console.warn( "OneSignal: This method has been deprecated. Use getIdAsync instead for getting push subscription id." ), O.id ? O.id : "") : ""; } o.getPushSubscriptionId = X; async function Z() { return i(e) ? await e.getPushSubscriptionId() : Promise.reject( new Error("OneSignal native module not loaded") ); } o.getIdAsync = Z; function U() { return i(e) ? (console.warn( "OneSignal: This method has been deprecated. Use getTokenAsync instead for getting push subscription token." ), O.token ? O.token : "") : ""; } o.getPushSubscriptionToken = U; async function ee() { return i(e) ? await e.getPushSubscriptionToken() : Promise.reject( new Error("OneSignal native module not loaded") ); } o.getTokenAsync = ee; function te() { return i(e) ? (console.warn( "OneSignal: This method has been deprecated. Use getOptedInAsync instead for getting push subscription opted in status." ), O.optedIn ?? !1) : !1; } o.getOptedIn = te; async function ne() { return i(e) ? await e.getOptedIn() : Promise.reject( new Error("OneSignal native module not loaded") ); } o.getOptedInAsync = ne; function ie() { i(e) && e.optOut(); } o.optOut = ie; function re() { i(e) && e.optIn(); } o.optIn = re; })(t.pushSubscription || (t.pushSubscription = {})); function p(o, c) { i(e) && (b(c), e.addUserStateObserver(), l.addEventListener(V, c)); } t.addEventListener = p; function v(o, c) { i(e) && l.removeEventListener(V, c); } t.removeEventListener = v; async function a() { return i(e) ? e.getOnesignalId() : Promise.reject(new Error("OneSignal native module not loaded")); } t.getOnesignalId = a; async function f() { return i(e) ? e.getExternalId() : Promise.reject(new Error("OneSignal native module not loaded")); } t.getExternalId = f; function I(o) { i(e) && e.setLanguage(o); } t.setLanguage = I; function T(o, c) { i(e) && e.addAlias(o, c); } t.addAlias = T; function g(o) { i(e) && e.addAliases(o); } t.addAliases = g; function S(o) { i(e) && e.removeAlias(o); } t.removeAlias = S; function L(o) { i(e) && e.removeAliases(o); } t.removeAliases = L; function s(o) { i(e) && e.addEmail(o); } t.addEmail = s; function u(o) { i(e) && e.removeEmail(o); } t.removeEmail = u; function d(o) { i(e) && e.addSms(o); } t.addSms = d; function A(o) { i(e) && e.removeSms(o); } t.removeSms = A; function H(o, c) { if (i(e)) { if (!o || c === void 0 || c === null) { console.error("OneSignal: addTag: must include a key and a value"); return; } typeof c != "string" && (console.warn( "OneSignal: addTag: tag value must be of type string; attempting to convert" ), c = String(c)), e.addTag(o, c); } } t.addTag = H; function Y(o) { if (!i(e)) return; if (!o || Object.keys(o).length === 0) { console.error( 'OneSignal: addTags: argument must be of type object of the form { key : "value" }' ); return; } const c = o; Object.keys(o).forEach(function(D) { typeof c[D] != "string" && (console.warn( "OneSignal: addTags: tag value for key " + D + " must be of type string; attempting to convert" ), c[D] = String(c[D])); }), e.addTags(o); } t.addTags = Y; function $(o) { if (i(e)) { if (typeof o != "string") { console.error( "OneSignal: removeTag: key argument must be of type string" ); return; } e.removeTags([o]); } } t.removeTag = $; function J(o) { if (i(e)) { if (!Array.isArray(o)) { console.error("OneSignal: removeTags: argument must be of array type"); return; } e.removeTags(o); } } t.removeTags = J; function Q() { return i(e) ? e.getTags() : Promise.reject(new Error("OneSignal native module not loaded")); } t.getTags = Q; })(n.User || (n.User = {})), ((t) => { function p() { return console.warn( "OneSignal: This method has been deprecated. Use getPermissionAsync instead for getting notification permission status." ), z; } t.hasPermission = p; async function v() { return e.hasNotificationPermission(); } t.getPermissionAsync = v; function a(d) { return i(e) ? e.requestNotificationPermission(d) : Promise.reject(new Error("OneSignal native module not loaded")); } t.requestPermission = a; function f() { return i(e) ? e.canRequestNotificationPermission() : Promise.reject(new Error("OneSignal native module not loaded")); } t.canRequestPermission = f; function I(d) { i(e) && (P.OS === "ios" ? (b(d), e.registerForProvisionalAuthorization(d)) : console.warn( "registerForProvisionalAuthorization: this function is not supported on Android" )); } t.registerForProvisionalAuthorization = I; function T() { return i(e) ? e.permissionNative() : Promise.reject(new Error("OneSignal native module not loaded")); } t.permissionNative = T; function g(...[d, A]) { i(e) && (b(A), d === "click" ? (e.addNotificationClickListener(), l.addEventListener(M, A)) : d === "foregroundWillDisplay" ? (e.addNotificationForegroundLifecycleListener(), l.addEventListener(N, A)) : d === "permissionChange" && (b(A), e.addPermissionObserver(), l.addEventListener(k, A))); } t.addEventListener = g; function S(...[d, A]) { d === "click" ? l.removeEventListener(M, A) : d === "foregroundWillDisplay" ? l.removeEventListener(N, A) : d === "permissionChange" && l.removeEventListener(k, A); } t.removeEventListener = S; function L() { i(e) && e.clearAllNotifications(); } t.clearAll = L; function s(d) { i(e) && (P.OS === "android" ? e.removeNotification(d) : console.warn( "removeNotification: this function is not supported on iOS" )); } t.removeNotification = s; function u(d) { i(e) && (P.OS === "android" ? e.removeGroupedNotifications(d) : console.warn( "removeGroupedNotifications: this function is not supported on iOS" )); } t.removeGroupedNotifications = u; })(n.Notifications || (n.Notifications = {})), ((t) => { function p(...[s, u]) { i(e) && (b(u), s === "click" ? (e.addInAppMessageClickListener(), l.addEventListener(j, u)) : s === "willDisplay" ? (e.addInAppMessagesLifecycleListener(), l.addEventListener(q, u)) : s === "didDisplay" ? (e.addInAppMessagesLifecycleListener(), l.addEventListener(R, u)) : s === "willDismiss" ? (e.addInAppMessagesLifecycleListener(), l.addEventListener(G, u)) : s === "didDismiss" && (e.addInAppMessagesLifecycleListener(), l.addEventListener(W, u))); } t.addEventListener = p; function v(...[s, u]) { i(e) && (b(u), s === "click" ? l.removeEventListener(j, u) : s === "willDisplay" ? l.removeEventListener(q, u) : s === "didDisplay" ? l.removeEventListener(R, u) : s === "willDismiss" ? l.removeEventListener(G, u) : s === "didDismiss" && l.removeEventListener(W, u)); } t.removeEventListener = v; function a(s, u) { if (!i(e)) return; (!s || u == null) && console.error("OneSignal: addTrigger: must include a key and a value"); let d = {}; d[s] = u, e.addTriggers(d); } t.addTrigger = a; function f(s) { if (!i(e)) return; Object.keys(s).length === 0 && console.error( "OneSignal: addTriggers: argument must be an object of the form { key : 'value' }" ), e.addTriggers(s); } t.addTriggers = f; function I(s) { i(e) && e.removeTrigger(s); } t.removeTrigger = I; function T(s) { i(e) && e.removeTriggers(s); } t.removeTriggers = T; function g() { i(e) && e.clearTriggers(); } t.clearTriggers = g; function S(s) { i(e) && e.paused(s); } t.setPaused = S; function L() { return i(e) ? e.getPaused() : Promise.reject(new Error("OneSignal native module not loaded")); } t.getPaused = L; })(n.InAppMessages || (n.InAppMessages = {})), ((t) => { function p() { i(e) && e.requestLocationPermission(); } t.requestPermission = p; function v(f) { i(e) && e.setLocationShared(f); } t.setShared = v; function a() { return i(e) ? e.isLocationShared() : Promise.reject(new Error("OneSignal native module not loaded")); } t.isShared = a; })(n.Location || (n.Location = {})), ((t) => { function p(f) { i(e) && e.addOutcome(f); } t.addOutcome = p; function v(f) { i(e) && e.addUniqueOutcome(f); } t.addUniqueOutcome = v; function a(f, I) { i(e) && e.addOutcomeWithValue(f, Number(I)); } t.addOutcomeWithValue = a; })(n.Session || (n.Session = {})); })(_ || (_ = {})); export { ye as LogLevel, fe as NotificationWillDisplayEvent, ue as OSNotification, Se as OSNotificationPermission, _ as OneSignal };