Skip to content

Unity SDK Basic integration guide

This section contains information on how to integrate the Pushwoosh Unity SDK into your application.

Prerequisites

Anchor link to

To integrate the Pushwoosh Unity SDK into your app, you will need the following:

Integration steps

Anchor link to

1. Add Pushwoosh Unity SDK Dependency

Anchor link to

Import Unity push notification plugin into your Assets folder in Unity.

2. Unity SDK Initialization

Anchor link to

Create PushNotificator.cs script and attach it to the Camera Object in the scene. In the script:

  • Initialize the Pushwoosh SDK with the application code and the Firebase project number.
  • Call RegisterForPushNotifications() in your initialization logic to register for push notifications.
  • Add registration event handlers to manage push registration events:
PushNotificator.cs
using UnityEngine;
using UnityEngine.UI;
public class PushNotificator : MonoBehaviour {
void Start () {
Pushwoosh.ApplicationCode = "__YOUR_APP_ID__";
Pushwoosh.FcmProjectNumber = "__YOUR_FCM_SENDER_ID__";
Pushwoosh.Instance.OnRegisteredForPushNotifications += OnRegisteredForPushNotifications;
Pushwoosh.Instance.OnFailedToRegisteredForPushNotifications += OnFailedToRegisteredForPushNotifications;
Pushwoosh.Instance.RegisterForPushNotifications();
}
void OnRegisteredForPushNotifications(string token) {
Debug.LogFormat(LogType.Log, LogOption.NoStacktrace, null, "Received token: \n{0}", token);
}
void OnFailedToRegisteredForPushNotifications(string error) {
Debug.LogFormat(LogType.Log, LogOption.NoStacktrace, null, "Error ocurred while registering to push notifications: \n{0}", error);
}
}

Where:

  • __YOUR_APP_ID__ is the application code from the Pushwoosh Control Panel.
  • __YOUR_FCM_SENDER_ID__ is the Firebase project number from the Firebase Console.

3. iOS Native Setup

Anchor link to

3.1 Capabilities

Anchor link to

To enable Push Notifications in your project, you need to add certain capabilities.

In the Signing & Capabilities section, add the following capabilities:

  • Push Notifications
  • Background Modes. After adding this capability, check the box for Remote notifications.

If you intend to use Time Sensitive Notifications (iOS 15+), also add the Time Sensitive Notifications capability.

3.2 Info.plist

Anchor link to

In your Runner/Info.plist set the __PUSHWOOSH_DEVICE_API_TOKEN__ key to the Pushwoosh Device API Token:

info.plist
<key>Pushwoosh_API_TOKEN</key>
<string>__PUSHWOOSH_DEVICE_API_TOKEN__</string>

3.3 Message delivery tracking

Anchor link to

You must add a Notification Service Extension target to your project. This is essential for accurate delivery tracking and features like Rich Media on iOS.

Follow the native guide’s steps to add the extension target and the necessary Pushwoosh code within it.

4. Android Native Setup

Anchor link to

4.1 Add Firebase configuration file

Anchor link to

Place the google-services.json file into Assets folder in your project directory.

4.2 Adjust build settings

Anchor link to

In the Publishing Settings section of your Android build profile enable Custom Main Manifest option.

4.3 Add Pushwoosh metadata

Anchor link to

In your Assets/Plugins/Android/AndroidManifest.xml add Pushwoosh Device API Token inside the <application> tag:

AndroidManifest.xml
<meta-data android:name="com.pushwoosh.apitoken" android:value="__YOUR_DEVICE_API_TOKEN__" />

Important: Be sure to give the token access to the right app in your Pushwoosh Control Panel. Learn more

5. Windows Store integration

Anchor link to
  1. Add link.xml to your Assets/ directory with the following content:
<linker>
<assembly fullname="PushSDK" preserve="all"/>
</linker>
  1. You also need to Associate App with the Store in exported Visual Studio project. Make sure your app is signed with a certificate matching your Publisher Identity.

Opt in the Internet (Client) capability in Capabilities tab of your .appxmanifest in the exported Visual Studio project.

6. Run the Project

Anchor link to
  1. Build and run the project.
  2. Go to the Pushwoosh Control Panel and send a push notification.
  3. You should see the notification in the app.

Extended integration

Anchor link to

At this stage, you have already integrated the SDK and can send and receive push notifications. Now, let’s explore the core functionality

Push notification event listeners

Anchor link to

In the Pushwoosh SDK there are two event listeners, designed for handling push notifications:

  • OnPushNotificationsReceived event is triggered, when a push notification is received
  • OnPushNotificationsOpened event is triggered, when a user opens a notification

You should set up these event listeners right after initialization of the SDK in the PushNotificator.cs:

PushNotificator.cs
using UnityEngine;
using UnityEngine.UI;
public class PushNotificator : MonoBehaviour {
void Start () {
Pushwoosh.ApplicationCode = "__YOUR_APP_ID__";
Pushwoosh.FcmProjectNumber = "__YOUR_FCM_SENDER_ID__";
Pushwoosh.Instance.OnPushNotificationsReceived += OnPushNotificationsReceived;
Pushwoosh.Instance.OnPushNotificationsOpened += OnPushNotificationsOpened;
Pushwoosh.Instance.RegisterForPushNotifications();
}
void OnPushNotificationsReceived(string payload) {
Debug.LogFormat(LogType.Log, LogOption.NoStacktrace, null, "Received push notificaiton: \n{0}", payload);
}
void OnPushNotificationsOpened(string payload) {
Debug.LogFormat(LogType.Log, LogOption.NoStacktrace, null, "Opened push notificaiton: \n{0}", payload);
}

User configuration

Anchor link to

By focusing on individual user behavior and preferences, you can deliver personalized content, leading to increased user satisfaction and loyalty

public class Registration {
public void afterUserLogin(User user) {
// Set user ID
Pushwoosh.Instance.SetUserId(user.getId());
// Set user email
Pushwoosh.Instance.SetEmail(user.getEmail());
// Setting additional user information as tags for Pushwoosh
Pushwoosh.Instance.SetIntTag("Age", user.getAge());
Pushwoosh.Instance.SetStringTag("Name", user.getName());
Pushwoosh.Instance.SetStringTag("LastLoginDate", user.getLastLoginDate());
}
}

Tags are key-value pairs assigned to users or devices, allowing segmentation based on attributes like preferences or behavior, enabling targeted messaging.

public class UpdateUser {
public void afterUserUpdateProfile(User user) {
// Set list of favorite categories
Pushwoosh.Instance.SetListTag(
"favorite_categories": user.getFavoriteCategoriesList()
);
// Set payment information
Pushwoosh.Instance.SetStringTag("is_subscribed", user.isSubscribed());
Pushwoosh.Instance.SetStringTag("payment_status", user.getPaymentStatus());
Pushwoosh.Instance.SetStringTag("billing_address", user.getBillingAddress());
}
}

Events are specific user actions or occurrences within the app that can be tracked to analyze behavior and trigger corresponding messages or actions

public class Registration {
// Track login event
public void afterUserLogin(User user) {
Pushwoosh.Instance.PostEvent("login", new Dictionary<string, object>() {
{ "name", user.getName() },
{ "last_login", user.getLastLoginDate() }
});
}
public void afterUserPurchase(Product product) {
// Track purchase event
Pushwoosh.Instance.PostEvent("purchase", new Dictionary<string, object>() {
{ "product_id", product.getId() },
{ "product_name", product.getName() },
{ "price", product.getPrice() },
{ "quantity", product.getQuantity() }
});
}
}

Troubleshooting

Anchor link to

If you encounter any issues during the integration process, please refer to the support and community section.