OneSignal

This guide describes how to add and configure OneSignal integration.

About Integration

Apphud can update User Tags in OneSignal. Tags are key-value attributes that allow you to target specific users or groups using Segments and send personalized messages with Tag Substitution.

How to Add Integration?

Step 1

Step 2

At Apphud go to "Integrations" section and add OneSignal:

Step 3

Enter Application ID and Sandbox Application ID (optional):

Step 4

You can enter your custom event names or disable some:

Step 5

Enable integration and click Save:

Match User IDs

You should set Apphud User ID property to OneSignal's externalUserID property:

Swift

// assuming both Apphud and OneSignal SDKs are initialized:
OneSignal.login(Apphud.userID())

Objective-C

// assuming both Apphud and OneSignal SDKs are initialized:
[OneSignal login:Apphud.userID];

Kotlin

// assuming both Apphud and OneSignal SDKs are initialized:
OneSignal.login(Apphud.userID())

Java

// assuming both Apphud and OneSignal SDKs are initialized:
OneSignal.login(Apphud.userID());

Data Tags

Apphud sends the following payload in each event. Despite default tags, there are advanced tags, which are only being sent when Advanced Tags option is enabled in integration settings.

Default Tags

KeyDescription
ah_eventEvent Name
ah_status

Subscription Status. Possible Values:

trial, intro, regular, promo, refunded, expired, grace

JSON example of event with Default Tags:

{
    "ah_event": "apphud_subscription_renewed",
    "ah_product": "com.product.id"
}

Advanced Tags

If you enable Advanced Tags option in One Signal integration page, then additional tags will be sent:

KeyDescription
ah_spentTotal amount of money that user has been charged, in USD
ah_paymentsNumber of transactions user has been charged
ah_countryUser's App Store Country
ah_autorenewSubscription auto-renew state. "1" or "0"
ah_billing_issueWhether subscription has billing issue. "1" or "0"
ah_productProduct ID of the subscription

JSON example of event with Advanced Tags enabled:

{
    "ah_event": "apphud_subscription_renewed",
    "ah_product": "com.product.id",
    "ah_billing_issue": "0",
    "ah_autorenew": "1",
    "ah_country": "UK",
    "ah_status": "regular",
    "ah_spent": "9.99",
    "ah_payments": "1"
}

Using both OneSignal and Apphud Push Notifications on iOS

On iOS, if you are using Apphud Rules, you will need incoming push notifications to be handled by either Apphud or OneSignal. However, without necessary changes OneSignal doesn't let notifications go through.

Let Apphud to handle incoming push notifications in OneSignal callback:

OneSignal.setNotificationOpenedHandler { result in
    if Apphud.handlePushNotification(apsInfo: result.notification.rawPayload) {
        // Push Notification was handled by Apphud, probably do nothing
    } else {
        // Handle other types of push notifications
    }
}
OneSignal.setNotificationWillShowInForegroundHandler { notif, completion in
    if Apphud.handlePushNotification(apsInfo: notif.rawPayload) {
        // Push Notification was handled by Apphud, probably do nothing
    } else {
        // Handle other types of push notifications
    }
    completion(notif)
}

Modify OneSignal's UINotificationServiceExtension so that it will pass Apphud Notifications through:

// NotificationService.swift
override func didReceive(_ request: UNNotificationRequest, withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void) {
   self.receivedRequest = request;
   self.contentHandler = contentHandler
   bestAttemptContent = (request.content.mutableCopy() as? UNMutableNotificationContent)
        
   // (!) --> add this line -->
   bestAttemptContent?.userInfo.merge(["custom": ["i": NSUUID().uuidString]], uniquingKeysWith: {old, new in return old})    
        
   if let bestAttemptContent = bestAttemptContent {
       OneSignal.didReceiveNotificationExtensionRequest(self.receivedRequest, with: self.bestAttemptContent)
       contentHandler(bestAttemptContent)
   }
}