Steps to Integrate Push Notifications With Unity3D on iOS

DZone 's Guide to

Steps to Integrate Push Notifications With Unity3D on iOS

Learn how to implement push notifications in your iOS mobile app using the Unity3D game engine with this step-by-step tutorial.

· Mobile Zone ·
Free Resource

Implementing push notifications in Unity apps/games for iOS is an easy task, and we will explain the process of doing this through a sample Unity demo app. The source code can be downloaded or viewed from our GitHub repo.

Image title

Prerequisites to Configure Push Notifications for iOS Apps/Games:

  • Create a new App ID and provisioning profile for each app that uses push notifications, as well as an SSL certificate for the server. To do this, you should have an iOS Developer Program membership on iOS Dev Center.
  • Create a .p12 file from the SSL certificate from the iOS Dev Center.
  • A server; this is where App42 Push Service comes into the picture.

Note: If you are new to push notifications for iOS or App42 , you can go through my App42 , you can go through my previous blog first.

Now let’s understand how the sample code implements the push notifications by examining the key code snippets. Open the folder you downloaded, go to the assets folder, and double-click PushSample.unity file to open the sample.

To implement push notifications, just drag and drop our “App42PushHandlerInternal.h/.m” classes to “Assets/Plugins/iOS” folder and “PushScript.cs” and PushResponse.cs  C# scripts to “Assets” folder. Assign PushScript.cs to a game object; in the demo, it was assigned to the Main Camera. So, let’s browse through the PushScript.cs code.

To receive push notifications, the iOS needs to be notified that your app wants to receive push notifications, and “App42PushHandlerInternal.m” class does it by default.

In the PushScript, all the required callbacks are defined, and to get it called, set a listener game object as:

// Use this for initialization
void Start ()
As "App42PushHandlerInternal.m" sends a request to register this device for push notification service; the device token is received from Apple Push Service on the successful response. It is available via "onDidRegisterForRemoteNotificationsWithDeviceToken" call back of PushScript.cs.
//Sent when the application successfully registered with Apple Push Notification Service (APNS).
void onDidRegisterForRemoteNotificationsWithDeviceToken(string deviceToken)
  if (deviceToken != null && deviceToken.Length!=0)
     registerDeviceTokenToApp42PushNotificationService(deviceToken,"User Name");

Now you need to register this device to App42 Push Notification Service to start sending/receiving push notifications. To do that, just call "registerDeviceTokenToApp42PushNotificationService" method of PushScript.cs from the above call back.

//Registers a user with the given device token to App42 push notification service
void registerDeviceTokenToApp42PushNotificationService(string devToken,string userName)
  ServiceAPI serviceAPI = new ServiceAPI(api_key,secret_key);
  PushNotificationService pushService = serviceAPI.BuildPushNotificationService();

The SendPushToUser method of this script can be written/called wherever it's required to send a request to App42 server to send a push notification to a specific user.

//Sends push to a given user
void SendPushToUser(string userName,string message)
  ServiceAPI serviceAPI = new ServiceAPI(api_key,secret_key);
  PushNotificationService pushService = serviceAPI.BuildPushNotificationService();

The "onPushNotificationsReceived" callback of the PushScript will be called when you receive a push notification.

//Sent when the application Receives a push notification
void onPushNotificationsReceived(string pushMessageString)
  //dump you code here to handle the pushMessageString

Now your app has been successfully set up to receive/send push notifications through our App42 Server using App42 Push Notification Service.

ios, mobile, mobile app development, push notifications, unity 3d

Published at DZone with permission of Rajeev Ranjan . See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}