Follow

Unity iOS Push Notification Code Example

This section describes the steps involved in writing an Unity iOS Application that uses Push Notifications. If you are using Native iOS please follow this guide. IOS Native Push Notification Code Example.

You will have needed to complete the steps in the article Creating Your IOS Push Credentials in order to use the code in this example.

Pre-Requisites:

The Apmetrix Unity Plugin requires you to use our Python Post-Process Build Script when building to iOS. More specifically when building to iOS for Push Notifications our Post-Process Build Script handles a bunch of linking & code injection for you.

The Apmetrix Python Post-Process Build Script handles the following:

  • Automatically links the required Frameworks
  • Automatically includes the required Compiler Flags
  • Automatically includes code into the UnityAppController.mm file

*Note: You MUST install Python 2.7

WINDOWS: DOWNLOAD HERE

MACOSX: DOWNLOAD HERE

Upon building for iOS, Unity will automatically call the /Editor/PostprocessBuildPlayer script. This will in-turn loop through and execute all available PostprocessBuildPlayer_* scripts.  The PostprocessBuildPlayer_Apmetrix script will call the ApmetrixXcodeUpdatePostBuild.pyc

Setting Up the Unity Project:

Before we begin setting up the Unity Project lets do a quick recap of the major points that were covered in the other support article. 

  • Acquired a certificate file
  • Acquired a .p12 file and uploaded it to the Apmetrix Dashboard
  • Acquired a new provisioning profile

 

The ApmetrixStatic.cs File

// ************* APMETRIX VARIABLES ****************** \\
public static int appDataset = 123456;
public static string appVersion = "";
public static string appUnifier = "123456789ABCDEFG";
public static int appPermissions = CHILD_SAFE;

//Used for Android Push Set this if using Android GCM
public static string GCMSenderID = "123456789058246";
// ************* END APMETRIX VARIABLES ****************** \\

 

The Apmetrix.cs File

*Note: The Apmetrix.cs file is designed to wok as an Instance Object. You can attach this script to any of your gameObjects. HOWEVER this object must not be destroyed. Destroying this object will reset the SDK when running in Non-Mobile mode. 

*Note: You may make changes to this file as you see fit but it is your responsibility to make sure the SDK still functions properly. Please inform your Apmetrix Account Manager if you are making changes to this file, include your reasoning and we will discuss the changes with the Apmetrix Engineering Team.

These are used inside the Apmetrix.cs file. More specifically inside the void Awake(). This function inits the Apmetrix SDK using the Apmetrix Variables and initializes Push Notifications. 

You can customize the Apmetrix Push Alert Dialog by changing the inputs to the following function which resides in the Apmetrix.cs file inside the void Awake().

This function MUST be called in order to correctly initialize Push Notifications. Failure to call this function will cause your Push Notifications to not work as intended.

ApmetrixInitMobilePushNotifications("Activate", "Deny");

 

Accessing the Push Notification Data:

To access the data that is contained within the Push Notification you will need to use Apmetrix.notificationHandler(). This function returns a Dictionary<string, string>.

The Map contains at minimum the message accessible via the key "message".

You can expect this Map to contain additional optional Key/Value Pairs that are added via the Send Message UI in the Apmetrix Dashboard.

 

Setting Up the Xcode Project:

After building the iOS project you will need to open up your Unity-iPhone.xcodeproj file.

You will now want to click on Unity-iPhone under TARGETS. Then click on Build Settings and scroll down to Code Signing. You should see something similar to the picture below

 

Remember in the document Creating Your IOS Push Credentials when you added your provising profile to Xcode. We will be using that profile with Push Enabled here.

Set your Code Signing Identity to your Apple Account and then set the Provising Profile to the one with Push Enabled. Your screen should look like similar to the image below.

Now go ahead and press the Play Button in the top Left of Xcode to finish building the app to your iOS device. You should get the pop-up prompting you for Push Notifications. If you do not see this prompt then please review the steps in this article and the Creating Your IOS Push Credentials.  

 

Sending A Push Notification:

Next, log into our dashboard (www.apmetrix.com/login)  and click on your Profile Image->Messaging and Send Push Notification and send yourself some tests to verify everything is working properly.

If you are still having issues please contact Apmetrix Support at support@apmetrix.com

Have more questions? Submit a request