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
Step 3
Step 4
Step 5
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
Objective-C
Kotlin
Java
Swift
Apphud.start(apiKey: "Apphud_API_Key")
OneSignal.initWithLaunchOptions(launchOptions, appId: "OneSignalAPIKey", handleNotificationReceived: { notif in
if let aps = notif?.payload.rawPayload {
Apphud.handlePushNotification(apsInfo: aps)
}
}, handleNotificationAction: nil, settings: onesignalInitSettings)
OneSignal.setExternalUserId(Apphud.userID())
Objective-C
[Apphud startWithApiKey:@"API_KEY" userID:nil observerMode:NO];
[OneSignal initWithLaunchOptions:launchOptions appId:@"APP_ID" handleNotificationReceived:^(OSNotification *notification) {
if (notification.payload.rawPayload != nil) {
[Apphud handlePushNotificationWithApsInfo:notification.payload.rawPayload];
}
} handleNotificationAction:nil
settings:@{kOSSettingsKeyAutoPrompt: @false, kOSSettingsKeyInAppLaunchURL: @false}];
[OneSignal setExternalUserId:Apphud.userID];
Kotlin
// OneSignal Initialization
OneSignal.startInit(this)
.inFocusDisplaying(OneSignal.OSInFocusDisplayOption.Notification)
.unsubscribeWhenNotificationsAreDisabled(true)
.init()
OneSignal.setExternalUserId(Apphud.userID())
Java
// OneSignal Initialization
OneSignal.startInit(this)
.inFocusDisplaying(OneSignal.OSInFocusDisplayOption.Notification)
.unsubscribeWhenNotificationsAreDisabled(true)
.init();
OneSignal.setExternalUserId(Apphud.userID());

Data Tags

Apphud sends the following payload in each event:

Key

Description

ah_event

Event Name

ah_status

Subscription Status. Possible Values:

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

ah_spent

Total amount of money that user has been charged, in USD

ah_payments

Number of transactions user has been charged

ah_country

User's App Store Country

ah_autorenew

Subscription auto-renew state. "1" or "0"

ah_billing_issue

Whether subscription has billing issue. "1" or "0"

ah_product

Product ID of the subscription

Example:

{
"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.initWithLaunchOptions(launchOptions, appId: "APP_ID", handleNotificationReceived: { notif in
if let aps = notif?.payload.rawPayload {
Apphud.handlePushNotification(apsInfo: aps)
}
}, handleNotificationAction: { result in
}, settings: onesignalInitSettings)

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)
}
}