{"library":"react-native-onesignal","title":"React Native OneSignal SDK","description":"The React Native OneSignal SDK facilitates the integration of OneSignal's push notification, in-app messaging, email, and SMS services into iOS and Android applications built with React Native. The current stable version is 5.4.3. This library is actively maintained with frequent minor and patch releases, often addressing native SDK updates, bug fixes, and new features like the recent support for React Native's New Architecture (TurboModules) introduced in v5.4.0. Key differentiators include its comprehensive cross-platform support for various notification types and its user-centric APIs for managing user data across channels, distinguishing it from lower-level notification APIs provided by mobile OS platforms.","language":"javascript","status":"active","last_verified":"Sun Apr 19","install":{"commands":["npm install react-native-onesignal"],"cli":null},"imports":["import OneSignal from 'react-native-onesignal';","import type { NotificationWillDisplayEvent, Notification } from 'react-native-onesignal';","import type { InAppMessageClickEvent } from 'react-native-onesignal';"],"auth":{"required":false,"env_vars":[]},"quickstart":{"code":"import React, { useEffect } from 'react';\nimport { View, Text, Alert, Platform } from 'react-native';\nimport OneSignal from 'react-native-onesignal';\n\nconst ONESIGNAL_APP_ID = process.env.ONESIGNAL_APP_ID ?? 'YOUR_ONESIGNAL_APP_ID';\n\nconst App = () => {\n  useEffect(() => {\n    // OneSignal Init Code\n    OneSignal.setAppId(ONESIGNAL_APP_ID);\n\n    // Method for setting the External User Id\n    OneSignal.setExternalUserId('your-user-id-from-your-system');\n\n    // Method for handling notifications received while app in foreground\n    OneSignal.setNotificationWillShowInForegroundHandler(notificationReceivedEvent => {\n      console.log(\"OneSignal: notification will show in foreground: \", notificationReceivedEvent);\n      let notification = notificationReceivedEvent.get ;();\n      Alert.alert(\"Notification Received\", notification.body || '');\n      notificationReceivedEvent.complete(notification);\n    });\n\n    // Method for handling notification clicks\n    OneSignal.setNotificationOpenedHandler(notification => {\n      console.log(\"OneSignal: notification opened: \", notification);\n      Alert.alert(\"Notification Clicked\", notification.notification.body || '');\n    });\n\n    // Prompt for push notifications (iOS) and Android 13+ permission\n    if (Platform.OS === 'ios' || (Platform.OS === 'android' && parseFloat(Platform.Version) >= 33)) {\n      OneSignal.promptForPushNotificationsWithProvisionalNotification();\n    }\n\n    return () => {\n      // Clean up listeners if necessary\n    };\n  }, []);\n\n  return (\n    <View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}>\n      <Text>OneSignal React Native Example</Text>\n      <Text>Check console for OneSignal logs</Text>\n    </View>\n  );\n};\n\nexport default App;","lang":"typescript","description":"This quickstart code initializes the OneSignal SDK, sets an external user ID, and registers handlers for incoming notifications (foreground) and notification clicks. It also prompts for push notification permissions on iOS and Android 13+.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}