Unity

How to integrate Pushwoosh SDK into your Unity project

Supports iOS, Android, Amazon, Windows Store Apps (Windows 8.1/Phone 8.1/Windows 10)

Download Plugin Download Sample Plugin Docs

Note:

iOS Simulator can neither subscribe nor receive push notifications.

To integrate Pushwoosh into your Unity application you need to do simple following steps:

  1. Get the google-services.json from your Firebase console as described here and locate it to your project's Assets folder. Package name of your app should be added to your Firebase project and persist in the google-services.json file.

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

unitypackage:

If Pushwoosh.unitypackage import fails, copy Assets/Editor, Assets/Plugins, Assets/Pushwoosh folders from the Sample Project into yours.

AndroidManifest.xml changes

Merge contents of Assets/Plugins/Android/PushwooshAndroidManifest.xml into Assets/Plugins/Android/AndroidManifest.xml. If you don't already have an Android Manifest, rename PushwooshAndroidManifest.xml to AndroidManifest.xml. Replace PACKAGE_NAME with your project package. Note that in Unity it is called ‘Bundle Identifier’.

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

using UnityEngine;
using System.Collections;
 
public class PushNotificator : MonoBehaviour {
    // use for initialization
    void Start () {
        Pushwoosh.ApplicationCode = "ENTER_PUSHWOOSH_APP_ID_HERE";
        Pushwoosh.FcmProjectNumber = "ENTER_FCM_SENDER_ID_HERE";
        Pushwoosh.Instance.OnRegisteredForPushNotifications += OnRegisteredForPushNotifications;
        Pushwoosh.Instance.OnFailedToRegisteredForPushNotifications += OnFailedToRegisteredForPushNotifications;
        Pushwoosh.Instance.OnPushNotificationsReceived += OnPushNotificationsReceived;
      	Pushwoosh.Instance.RegisterForPushNotifications ();
    }
 
    void OnRegisteredForPushNotifications(string token)
    {
        // handle here
        Debug.Log("Received token: \n" + token);
    }
     
    void OnFailedToRegisteredForPushNotifications(string error)
    {
        // handle here
        Debug.Log("Error ocurred while registering to push notifications: \n" + error);
    }
     
    void OnPushNotificationsReceived(string payload)
    {
        // handle here
        Debug.Log("Received push notificaiton: \n" + payload);
    }
}

That’s it! How easy is that?

ApplicationCode and FcmProjectNumber should be set before first Pushwoosh.Instance method call.

2. If you need to use Geozones, go to Assets/Pushwoosh/Editor/PushwooshDependencies.xml and uncomment the following lines:

Now you can start and stop tracking Geozones:

//start
Pushwoosh.Instance.StartTrackingGeoPushes();

//stop
Pushwoosh.Instance.StopTrackingGeoPushes();

iOS extras

To hide notification alerts when the application is in the foreground, simply set Pushwoosh_SHOW_ALERT value to NO in your Info.plist.

Since iOS manages push notifications at the OS level, make sure to include the Pushwoosh Notification Service Extension to track the delivery of push notifications for your iOS app. Learn how to add it

XCode

XCode requires Push Notifications to be enabled in Capabilities section in order to register for push notfications.

Enable Push Notifications in the Capabilities section.

Enable Remote notifications for Background Modes in the Capabilities section.

Android extras

To receive push notifications on Android when the application is active without posting the notification to the notification center, set the following line in AndroidManifest.xml to true:

<meta-data android:name="PW_BROADCAST_PUSH" android:value="false" />

Amazon API Key

Get the API KEY from Amazon according to the Amazon guide. For a pre-release or “debug” version of your app, you must create an API Key and store it as the only data in a file named api_key.txt . The api_key.txt file must be located inside the Assets folder for your project. Note: ADM cannot recognize your API Key if it contains any spaces or line breaks. For a release or “production” version of your app, you may not need to create an API Key. Only if you sign the release version of your app using your own certificate, must you create an additional API Key for the release version of your app. Otherwise, if you allow Amazon to sign your app on your behalf, you do not need to create an additional API Key.

Windows Store Apps

Follow the guide below

You might need to select WSAPlayer platform for the libraries in Plugins/WSA as per screenshot below.

You also need to Associate App with the Store in exported Visual Studio project. And don't forget to set Notifications->Toast Capable in the project manifest.

Issues with Unity projects with the Pushwoosh plugin installed

If you face the error like the one shown in the picture below, it might be caused by the Unity framework issue with the projects with 3rd-party plugins.

That’s what the Unity resolution note on this issue says:

At some point, UnityFramework runtime together with plugins is loaded at run time by dyld/NSBundle.load. Please keep that in mind while developing a plugin.

Any API-rich initialization on +(load), attribute(constructor), and global object construction code paths should be moved to a later phase preferably after UnityFramework is loaded, and even better when Unity is initialized.

Simple plain data initialization is safe, but API-rich calls that deal with other os parts especially networking and user interface APIs might lead to deadlock leading to watchdog termination.

By doing delayed/lazy initialization you can get better control over initialization order, you can reduce the application load time, and most importantly you avoid possible deadlock.

Possible workaround:

By default, UnityFramework.framework is embedded in Unity-iPhone target and it is not linked with it, UnityFramework is loaded later at runtime and some plugins are sensitive to this initialization path.

Go to Unity-iPhone / Build Phases / Link Binary with Libraries build section and add UnityFramework.framework.

UnityFramework will be loaded at the same time as the main executable.

Share your feedback with us

Your feedback helps us create a better experience, so we would love to hear from you if you have any issues during the SDK integration process. If you face any difficulties, please do not hesitate to share your thoughts with us via this form.

Last updated