OneSignal

Integrate RevenueCat subscription status with OneSignal

👍

The OneSignal integration is available on the Pro plan.

OneSignal can be a useful integration tool for understanding what stage a customer is in to react accordingly. RevenueCat can automatically update user tags in OneSignal with their latest subscription status.

With our OneSignal integration, you can:

  • Send an onboarding campaign to a user in a free trial
  • Send a push notification to churned users and offer them a discount

With accurate and up-to-date subscription data in OneSignal, you'll be set to turbocharge your campaigns ⚡️

For every auto-renewing subscription event in RevenueCat, the following tags get added or updated on the user in OneSignal. By leaving the tag blank in the RevenueCat dashboard, you can choose to not send any value for specific tag(s).

TagDescription
app_user_idThe RevenueCat App User Id that triggered the event
period_typeThe latest period type for the purchase or renewal. Either:
- TRIAL (for free trials)
- INTRO (or introductory pricing)
- NORMAL (standard subscription)
purchased_atepoch time in seconds of the latest subscription purchase or renewal
expiration_atepoch time in seconds of the latest subscription expiration date
storeEither APP_STORE , PLAY_STORE, or STRIPE
environmentEither SANDBOX or PRODUCTION
last_event_typeThe latest event type from the user. Either:
- INITIAL_PURCHASE
- TRIAL_STARTED
- TRIAL_CONVERTED
- TRIAL_CANCELLED
- RENEWAL
- CANCELLATION
product_idThe latest subscription product identifier that the user has purchased or renewed
entitlement_idsComma separated string of RevenueCat Entitlement identifiers that the user unlocked
active_subscriptionThe value will be set to true on any purchase/renewal event, and false on EXPIRATION
grace_period_expiration_atIf a billing issue occurs we will send the date of the grace period expiration.

📘

Auto-renewing subscriptions only

RevenueCat only updates data tags in OneSignal in response to auto-renewing subscription events.

Events

The OneSignal integration tracks the following events:

EventDefault Event NameDescriptionApp StorePlay StoreAmazonStripePromo
Initial Purchaseinitial_purchaseA new subscription has been purchased.
Trial Startedtrial_startedThe start of an auto-renewing subscription product free trial.
Trial Convertedtrial_convertedWhen an auto-renewing subscription product converts from a free trial to normal paid period.
Trial Cancelledtrial_cancelledWhen a user turns off renewals for an auto-renewing subscription product during a free trial period.
RenewalrenewalAn existing subscription has been renewed or a lapsed user has resubscribed.
CancellationcancellationA subscription or non-renewing purchase has been cancelled. See cancellation reasons for more details.
UncancellationuncancellationA non-expired cancelled subscription has been re-enabled.
ExpirationexpirationA subscription has expired and access should be removed.

If you have Platform Server Notifications configured, this event will occur as soon as we are notified (within seconds to minutes) of the expiration.

If you do not have notifications configured, delays may be approximately 1 hour.
Billing Issuebilling_issueThere has been a problem trying to charge the subscriber. This does not mean the subscription has expired.

Can be safely ignored if listening to CANCELLATION event + cancel_reason=BILLING_ERROR.

1. Send device data to RevenueCat

The OneSignal integration requires some device-specific data. RevenueCat will only update users in OneSignal if the below data has been added as Subscriber Attributes for the user.

KeyDescriptionRequired
$onesignalIdThe OneSignal Player Id for the user.

This property can be set manually, like any other Subscriber Attributes, or through the helper method setOnesignalID().

You can listen for changes to the OneSignal Id through their SDK, and send the value to RevenueCat. If you already have OneSignal set up, you should make sure that you're also sending the OneSignal Id for users that are updating to the latest version of your app.

class AppDelegate: UIResponder, UIApplicationDelegate, OSSubscriptionObserver {

    var window: UIWindow?

    func application(_ application: UIApplication,
                     didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {

        Purchases.configure(withAPIKey: "<revenuecat_api_key>", appUserID: nil)

        OneSignal.initWithLaunchOptions(launchOptions, appId: "<onesignal_app_id>")
        OneSignal.add(self as OSSubscriptionObserver)

        // If you've already set up OneSignal, then users should already have
        // a OneSignal Id. We can check if it's available and send it to RevenueCat
        if let onesignalId = OneSignal.getUserDevice()?.getUserId() {
            Purchases.shared.attribution.setOnesignalID(onesignalId)
        }

        return true
    }

    // Add this method to update the $onesignalId in RevenueCat whenever it changes
    // This code should be sufficient to capture all new users if you're setting
    // up OneSignal for the first time.
    func onOSSubscriptionChanged(_ stateChanges: OSSubscriptionStateChanges!) {
        if !stateChanges.from.subscribed && stateChanges.to.subscribed {
            // The user is subscribed
            // Either the user subscribed for the first time
            Purchases.shared.attribution.setOnesignalID(stateChanges.to.userId)
        }
    }
}

2. Send RevenueCat events into OneSignal

After you've set up the Purchases SDK to send device data to RevenueCat, you can "turn on" the integration and configure the tag names from the RevenueCat dashboard.

  1. Navigate to your project in the RevenueCat dashboard and find the Integrations card in the left menu. Select + New

  1. Choose OneSignal from the Integrations menu.
  2. Add your OneSignal App Id and OneSignal API key.
  3. Enter the tag names that RevenueCat should use, or choose the default tag names.
OneSignal configuration screen

OneSignal configuration screen

3. Testing the OneSignal integration

You can test the OneSignal integration end-to-end before going live. It's recommended that you test the integration is working properly for new users, and any existing users that may update their app to a new version.

Make a sandbox purchase with a new user

Simulate a new user installing your app, and go through your app flow to complete a sandbox purchase.

Check that the required device data is collected

Navigate the the Customer View for the test user that just made a purchase. Make sure that all of the required data from step 1 above is listed as attributes for the user.

Check that the OneSignal event delivered successfully

While still on the Customer View, click into the test purchase event in the Customer History and make sure that the OneSignal integration event exists and was delivered successfully.

Sample Events

Below are sample JSONs that are delivered to OneSignal for events.

{
    "app_id": "12345678-1234-1234-1234-123456789012",
    "tags": {
        "user_id": "$RCAnonymousID:87c6049c58069238dce29853916d624c",
        "period_type": "NORMAL",
        "purchased_at": 1600016247,
        "expiration_at": 1602608247,
        "store": "APP_STORE",
        "environment": "PRODUCTION",
        "last_event_type": "initial_purchase",
        "last_event_at": 1600016250,
        "product_id": "monthly_sub",
        "entitlement_ids": "Pro"
    }
}
{
    "app_id": "12345678-1234-1234-1234-123456789012",
    "tags": {
        "user_id": "$RCAnonymousID:87c6049c58069238dce29853916d624c",
        "period_type": "TRIAL",
        "purchased_at": 1600031584,
        "expiration_at": 1600290784,
        "store": "APP_STORE",
        "environment": "PRODUCTION",
        "last_event_type": "trial_started",
        "last_event_at": 1600031586,
        "product_id": "three_month_sub_trial",
        "entitlement_ids": "Pro"
    }
}
{
    "app_id": "12345678-1234-1234-1234-123456789012",
    "tags": {
        "app_user_id": "$RCAnonymousID:87c6049c58069238dce29853916d624c",
        "period_type": "NORMAL",
        "purchased_at": 1602136340,
        "expiration_at": 1602741140,
        "store": "APP_STORE",
        "environment": "PRODUCTION",
        "last_event_type": "trial_converted",
        "last_event_at": 1602114850,
        "product_id": "weekly_sub_trial",
        "entitlement_ids": null
    }
}
{
    "app_id": "12345678-1234-1234-1234-123456789012",
    "tags": {
        "app_user_id": "$RCAnonymousID:87c6049c58069238dce29853916d624c",
        "period_type": "TRIAL",
        "purchased_at": 1602051920,
        "expiration_at": 1602311120,
        "store": "APP_STORE",
        "environment": "PRODUCTION",
        "last_event_type": "trial_cancelled",
        "last_event_at": 1602129368,
        "product_id": "weekly_sub_trial",
        "entitlement_ids": null
    }
}
{
    "app_id": "12345678-1234-1234-1234-123456789012",
    "tags": {
        "app_user_id": "$RCAnonymousID:87c6049c58069238dce29853916d624c",
        "period_type": "NORMAL",
        "purchased_at": 1602125078,
        "expiration_at": 1604807078,
        "store": "APP_STORE",
        "environment": "PRODUCTION",
        "last_event_type": "renewal",
        "last_event_at": 1602122793,
        "product_id": "monthly_sub",
        "entitlement_ids": "Pro"
    }
}
{
    "app_id": "12345678-1234-1234-1234-123456789012",
    "tags": {
        "app_user_id": "$RCAnonymousID:87c6049c58069238dce29853916d624c",
        "period_type": "NORMAL",
        "purchased_at": 1602086660,
        "expiration_at": 1602691460,
        "store": "APP_STORE",
        "environment": "PRODUCTION",
        "last_event_type": "cancellation",
        "last_event_at": 1602118600,
        "product_id": "weekly_sub",
        "entitlement_ids": null
    }
}
{
    "app_id": "12345678-1234-1234-1234-123456789012",
    "tags": {
        "app_user_id": "$RCAnonymousID:87c6049c58069238dce29853916d624c",
        "period_type": "TRIAL",
        "purchased_at": 1663445025,
        "expiration_at": 1664049825,
        "store": "APP_STORE",
        "environment": "PRODUCTION",
        "last_event_type": "uncancellation",
        "last_event_at": 1663969096,
        "product_id": "annual_sub",
        "entitlement_ids": "Premium"
    }
}
{
    "app_id": "12345678-1234-1234-1234-123456789012",
    "tags": {
        "period_type": "NORMAL",
        "purchased_at": 1652374230,
        "expiration_at": 1652979030,
        "last_event_type": "expiration",
        "last_event_at": 1652988735
    }
}
{
    "app_id": "12345678-1234-1234-1234-123456789012",
    "tags": {
        "app_user_id": "$RCAnonymousID:87c6049c58069238dce29853916d624c",
        "period_type": "TRIAL",
        "purchased_at": 1652383957,
        "expiration_at": 1654371157,
        "store": "APP_STORE",
        "environment": "PRODUCTION",
        "last_event_type": "billing_issue",
        "last_event_at": 1652988776,
        "product_id": "annual_sub",
        "entitlement_ids": "Premium"
    }
}

🚧

Why are tags not working?

If your tags aren't working and RevenueCat is sending events successfully with 200 codes, check out OneSignal's troubleshooting guide.

👍

You've done it!

You should start seeing subscription data from RevenueCat appear on users in OneSignal.