Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

onesignal-expo-plugin

OneSignal81.6kMIT2.0.3

The OneSignal Expo plugin allows you to use OneSignal without leaving the managed workflow. Developed in collaboration with SweetGreen.

expo, onesignal, push, notifications, EAS

readme

Welcome to the onesignal-expo-plugin 👋

Version Documentation Maintenance Twitter: onesignal

The OneSignal Expo plugin allows you to use OneSignal without leaving the managed workflow. Developed in collaboration with SweetGreen.

Overview

This plugin is an Expo Config Plugin. It extends the Expo config to allow customizing the prebuild phase of managed workflow builds (no need to eject to a bare workflow). For the purposes of OneSignal integration, the plugin facilitates automatically generating/configuring the necessary native code files needed to get the OneSignal React-Native SDK to work. You can think of adding a plugin as adding custom native code.

Supported environments:


Install

You need both the onesignal-expo-plugin and the react-native-onesignal npm package.

npx expo install onesignal-expo-plugin

# npm
npm install react-native-onesignal

# yarn
yarn add react-native-onesignal

Configuration in app.json / app.config.js

Plugin

Add the plugin to the front of the plugin array. It should be added automatically if you ran npx expo install. Just make sure it is the first plugin in the array and to configure any desired plugin props:

app.json

{
  "plugins": [
    [
      "onesignal-expo-plugin",
      {
        "mode": "development",
      }
    ]
  ]
}

or

app.config.js

export default {
  ...
  plugins: [
    [
      "onesignal-expo-plugin",
      {
        mode: "development",
      }
    ]
  ]
};

Plugin Prop

You can pass props to the plugin config object to configure:

| Plugin Prop | | | |--------------------------|----------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| | mode | required | Used to configure APNs environment entitlement. "development" or "production" | | devTeam | optional | Used to configure Apple Team ID. You can find your Apple Team ID by running expo credentials:manager e.g: "91SW8A37CR" | | iPhoneDeploymentTarget | optional | Target IPHONEOS_DEPLOYMENT_TARGET value to be used when adding the iOS NSE. A deployment target is nothing more than the minimum version of the operating system the application can run on. This value should match the value in your Podfile e.g: "12.0". | | smallIcons | optional | An array of local paths to small notification icons for Android. Image should be white, transparent, and 96x96 in size. Input images will be automatically scaled down and placed in the appropriate resource folders. e.g: ["./assets/ic_stat_onesignal_default.png"]. See https://documentation.onesignal.com/docs/customize-notification-icons#small-notification-icons. | | largeIcons | optional | An array of local paths to large notification icons for Android. Image should be white, transparent, and 256x256 in size. e.g: ["./assets/ic_onesignal_large_icon_default.png"]. See https://documentation.onesignal.com/docs/customize-notification-icons#large-notification-icons. | | smallIconAccentColor | optional | The accent color to use for notification icons on Android. Must be a valid hex value, e.g: "#FF0000" | | iosNSEFilePath | optional | The local path to a custom Notification Service Extension (NSE), written in Objective-C. The NSE will typically start as a copy of the default NSE, then altered to support any custom logic required. e.g: "./assets/NotificationService.m". |

OneSignal App ID

Add your OneSignal App ID to your Expo constants via the extra param:

Example:

{
  "extra": {
    "oneSignalAppId": "<YOUR APP ID HERE>"
  }
}

You can then access the value to pass to the initialize function:

import { OneSignal } from 'react-native-onesignal';
import Constants from "expo-constants";
OneSignal.initialize(Constants.expoConfig.extra.oneSignalAppId);

Alternatively, pass the app ID directly to the function:

OneSignal.initialize("YOUR-ONESIGNAL-APP-ID");

Versioning

In your configuration file, make sure you set:

Property Details
version Your app version. Corresponds to CFBundleShortVersionString on iOS. It is a human-readable version number of an iOS app, and is typically in the format of "X.X.X" (e.g. "1.0" or "2.3.1"). It is the version number that is typically displayed to users in the App Store and in the app itself. This value will be used in your NSE* target's plist file.
ios.buildNumber Build number for your iOS standalone app. Corresponds to CFBundleVersion and must match Apple's specified format (e.g: "42" or "100"). The build number is used by the App Store and iOS to identify and track different versions of an app, and is typically incremented for each new release. It is a number typically used for the developer's and system reference. This value will be used in your NSE* target's plist file.
ios.bundleIdentifier Bundle identifier for your iOS standalone app. Corresponds to CFBundleIdentifier. It's a unique identifier string that is used to identify an iOS app or bundle. It is typically in the format of "com.companyname.appname" (e.g. "com.example.myapp"). This value will be used in your NSE* target's plist and entitlements file.

* NSE = Notification Service Extension. Learn more about the NSE here.

EAS (Expo Application Services)

See our EAS documentation for help with EAS.

iOS Credentials: OneSignal + EAS

To distribute your iOS application via EAS, you will need to ensure your credentials are set up correctly. See our credentials setup guide for instructions.

Prebuild (optional)

Prebuilding in Expo will result in the generation of the native runtime code for the project (and ios and android directories being built). By prebuilding, we automatically link and configure the native modules that have implemented CocoaPods, autolinking, and other config plugins. You can think of prebuild like a native code bundler.

When you run expo prebuild we enter into a custom managed workflow which provides most of the benefits of bare workflows and managed workflows at the same time.

Why should I prebuild?

It may make sense to prebuild locally to inspect config plugin changes and help in debugging issues.

Run

npx expo prebuild
# nukes changes and rebuilds
npx expo prebuild --clean

EAS Note: if you choose to stay in a fully managed workflow by not prebuilding, EAS will still run npx expo prebuild at build time. You can also prebuild locally but remain in a fully managed workflow by adding the android and ios directories to your .gitignore.

Run

The following commands will prebuild and run your application. Note that for iOS, push notifications will not work in the Simulator.

# Build and run your native iOS project
npx expo run:ios

# Build and run your native Android project
npx expo run:android

🤝 Contributing

Contributions, issues and feature requests are welcome!
Feel free to check issues page.

Show your support

Give a ⭐️ if this project helped you!

OneSignal

📝 License

Copyright © 2023 OneSignal.
This project is MIT licensed.

changelog

Changelog

All notable changes to this project will be documented in this file.

April 2023

1.3.2 - 04/18/23

Changes

  • Update iOS generation to write xcode project via expo mod rather than directly to file system.
  • Bump qs from 6.5.2 to 6.5.3
  • Bump decode-uri-component from 0.2.0 to 0.2.2
  • Bump @xmldom/xmldom from 0.7.5 to 0.7.10
  • Bump multiple dependency versions in OneSignal Example App (ua-parser-js, decode-uri-component, @sideway/formula, @xmldom/xmldom)

March 2023

1.3.1 - 03/03/23

Changes

  • Update monorepo logic to rely on the more reliable require.resolve to find the onesignal-expo-plugin package.

November 2022

1.3.0 - 11/30/22

Changes

  • Add iosNSEFilePath configuration options to the plugin, which allows an app to specify a custom iOS Notification Service Extension.

October 2022

1.2.0 - 10/25/22

Changes

  • Add smallIcons and largeIcons configuration options to the plugin, which allows an app to specify large and small notification icons to package within the Android build.
  • Bump jpeg-js from 0.4.3 to 0.4.4
  • Bump simple-plist from 1.3.0 to 1.3.1

1.1.2 - 10/04/22

Fixes

  • Specify use_frameworks on OneSignalNotificationServiceExtension target in iOS Podfile when applicable (i.e. the app target also specifies use_frameworks).

September 2022

1.1.1 - 09/06/22

Fixes

  • Fix issue where EAS release builds were no longer successfully building due to log statement

1.1.0 - 09/01/22

Changes

  • No longer inject the OneSignal Gradle plugin on Android builds

    Fixes

  • Fix issue where plugin does not support monorepos

April 2022

1.0.1 - 04/11/22

Changes

  • Add more info regarding the expo prebuild command to README.

    Fixes

  • Upstream EAS workaround for "Missing Push Capability on iOS" issue. Adds aps-environment entitlement to NSE template. Although it is not required in normal builds, there is an upstream limitation from EAS that is resolved by including the push entitlement in both target entitlement files. As of 4/8/22, EAS builds will randomly pick an entitlement file if there are multiple in a project without differentiating among targets. This is resulting in flaky builds where half the time they work and half the time they don't. This can be seen as a temporary fix. Note it carries no side-effects / repercussions (i.e: the entitlement is superfluous but not critical to remove in the future).

March 2022

1.0.0 - 03/28/22

Fixes

  • Upgrade dependencies (via dependabot)

    Changes

  • Add credentials guide and update README
  • Advance to General Availability

February 2022

1.0.0-beta11 - 02/18/2022

Added

  • Linting
  • Instead of creating a "WriterManager", we repurpose the ReaderManager class by renaming it to FileManager and adding a synchronized write helper function (synchronous in the sense that it block further execution until complete).
  • iOS constants file

Fixes

  • Import cleanups
  • Linting errors
  • Write safety issu. NSE updater helper functions were previously writing to the entitlements & plist files asynchronously. We should await on each to ensure we don't have multiple writes to the same file at once (which is unsafe).

January 2022

1.0.0-beta10 - 01/24/2022

Added

  • ability to configure version numbers used in the NSE target's plist file.
  • OneSignalLog class for better console logging.
  • this changelog file.

Fixed

  • re-running expo prebuild will no longer result in duplicate entitlements and dependencies added to native files.