React Native SDK for OneSignal's push notification, email, SMS, and in-app messaging service
—
Location permission and sharing management for geo-targeted messaging capabilities.
Request and manage location permissions for geo-targeting features.
/**
* Prompts the user for location permissions to allow geotagging from the OneSignal dashboard.
*/
function requestPermission(): void;Usage Example:
import { OneSignal } from "react-native-onesignal";
// Request location permission for geo-targeting
OneSignal.Location.requestPermission();Control whether location data is shared with OneSignal for geo-targeting.
/**
* Disable or enable location collection (defaults to enabled if your app has location permission).
* @param shared - Whether to share location data with OneSignal
*/
function setShared(shared: boolean): void;
/**
* Checks if location collection is enabled or disabled.
* @returns Promise resolving to whether location sharing is enabled
*/
function isShared(): Promise<boolean>;Usage Examples:
// Enable location sharing for geo-targeted messages
OneSignal.Location.setShared(true);
// Disable location sharing (privacy-focused users)
OneSignal.Location.setShared(false);
// Check current location sharing status
const isLocationShared = await OneSignal.Location.isShared();
console.log('Location sharing enabled:', isLocationShared);import { OneSignal } from "react-native-onesignal";
async function setupLocationServices() {
try {
// Check if location is currently being shared
const currentlyShared = await OneSignal.Location.isShared();
console.log('Current location sharing status:', currentlyShared);
if (!currentlyShared) {
// Request location permission from user
OneSignal.Location.requestPermission();
// Enable location sharing after permission granted
// Note: You may want to wait for permission callback before enabling
OneSignal.Location.setShared(true);
// Verify it was enabled
const newStatus = await OneSignal.Location.isShared();
console.log('Location sharing now enabled:', newStatus);
}
} catch (error) {
console.error('Error setting up location services:', error);
}
}
// Call during app initialization
setupLocationServices();// Provide user control over location sharing
function handleLocationPrivacyToggle(enableLocation: boolean) {
if (enableLocation) {
// User opted in to location sharing
OneSignal.Location.requestPermission();
OneSignal.Location.setShared(true);
} else {
// User opted out - disable location sharing
OneSignal.Location.setShared(false);
}
}async function enableLocationFeaturesIfAllowed() {
const isShared = await OneSignal.Location.isShared();
if (isShared) {
// Enable location-based features in your app
console.log('Location-based notifications enabled');
// You can now use geo-targeted campaigns from OneSignal dashboard
} else {
// Provide alternative non-location-based experience
console.log('Using non-location-based targeting');
}
}// For GDPR compliance, check consent before enabling location
function handleGDPRLocationConsent(hasConsent: boolean) {
if (hasConsent) {
OneSignal.Location.requestPermission();
OneSignal.Location.setShared(true);
} else {
// Ensure location sharing is disabled
OneSignal.Location.setShared(false);
}
}// Enable location for store-based promotions
async function setupRetailLocationFeatures() {
const isShared = await OneSignal.Location.isShared();
if (!isShared) {
// Explain benefits to user
showLocationBenefitsModal({
onAccept: () => {
OneSignal.Location.requestPermission();
OneSignal.Location.setShared(true);
},
onDecline: () => {
OneSignal.Location.setShared(false);
}
});
}
}// Location for event-based notifications
async function setupEventLocationTracking() {
// Request location for event proximity notifications
OneSignal.Location.requestPermission();
OneSignal.Location.setShared(true);
console.log('Event location tracking enabled');
// Users will now receive notifications based on proximity to event venues
}// Essential location tracking for delivery apps
async function setupDeliveryLocationTracking() {
// Location is critical for delivery apps
OneSignal.Location.requestPermission();
OneSignal.Location.setShared(true);
const isEnabled = await OneSignal.Location.isShared();
if (!isEnabled) {
// Show critical location requirement dialog
showLocationRequiredDialog();
}
}// Location for weather-based alerts
async function setupWeatherLocationServices() {
OneSignal.Location.requestPermission();
OneSignal.Location.setShared(true);
// Users will receive weather alerts based on their location
console.log('Weather location services enabled');
}import { Platform } from 'react-native';
function platformSpecificLocationSetup() {
if (Platform.OS === 'ios') {
// iOS-specific location setup if needed
OneSignal.Location.requestPermission();
} else if (Platform.OS === 'android') {
// Android-specific location setup if needed
OneSignal.Location.requestPermission();
}
// Common location sharing setup
OneSignal.Location.setShared(true);
}Install with Tessl CLI
npx tessl i tessl/npm-react-native-onesignal