Push Widget

Unity iOS Push Notification

Push Notification helps developer in sending event or alert to app users even when they are not logged into the app. App42 uses APNS (Apple Push Notification Service) to send Push Notification on IOS devices. To configure Push Notification on Unity iOS requires some plugin to be written and it is not straight forward, especially if you are new to Push Notification and plugin writing.

To overcome on this issue and make this process easier for our developer, we have created a plugin that can be used in your project with minimal effort. To make it easy to understand, we have created a Sample Project. Here we will walk you through a step-by-step integration process by taking the examples of the sample we created.

1. Configuring your iOS Application for Push Notification

After following all the steps mention in this tutorial you can start building your Unity3D iOS project by following below steps:

  • Open the folder which you downloaded and go to assets folder and double click PushSample.unity file to open the sample.
  • To implement Push just drag and drop our “App42PushHandlerInternal.h/.m” classes to “Assets/Plugins/iOS” folder and “PushScript.cs” C# script 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.
  • 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 ()
{
setListenerGameObject(this.gameObject.name);
}
  • 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();
pushService.StoreDeviceToken(userName,devToken,"iOS");
}
2. Sending Message to User

Using API

Once your app is registered you are ready to send message to your app user using following code snippet from any App42 Unity SDK.

App42API.BuildPushNotificationService().SendPushMessageToUser(userName,message,new Callback());

Using AppHQ Console

You can also send push messages to your registered app user directly from AppHQ console. To do this follow the simple steps:

  • Login to AppHQ Management Console, Click on Unified Notification
  • Click on Push -> Click on Users
  • Select your App and user
  • Click on Manage from table and click on send push button as shown in the below image:

PushFromConsole

3. Sending Message to iOS Device with Sound and Badge

If you want to send push message with sound and badge effect on iOS device, you have to pass JSON string in below format.

string userName = "Nick";
string message = "{'alert':'Hi There...','badge':1,'sound':'default'}"; 
App42API.BuildPushNotificationService().SendPushMessageToUser(userName ,message ,new Callback());

You can also do it from AppHQ by putting this JSON format in message text area for sending message to iOS device with sound and badge effect.

4. Sending Message to Channel

App42 Push notification also supports channel subscription model where user can subscribe on channel of his interest to receive the notification. You can send message to channel which will deliver message to all users who are subscribed to that channel. A channel can be created from AppHQ console. To do this follow the simple steps:

  • Login to AppHQ Management Console, Click on Unified Notification
  • Click on Push -> Click on Channels
  • Select your App -> Click on Add channel button

AddChannelFromConsole

Unity Method :

Here is Unity Code to send Push Notification to Channel.

     
string channelName = "cricket channel";  
string description= "channel description"; 
//User must be registered with App42 for Push Notification
string userName = "Nick"; 
//Create Channel
PushNotificationService pushNotificationService =App42API.BuildPushNotificationService();
pushNotificationService.CreateChannelForApp (channelName , description,new Callback());
//Subscribe user to that channel
pushNotificationService.SubscribeToChannel (channelName ,userName,new Callback());     
// Now send message on the Channel
string message = "Mumbai Indians won IPL 6";  
PushNotification pushNotification = pushNotificationService.SendPushMessageToChannel(channelName ,message,new Callback()); 
5. User Segmentation (Applying Filter)

App42 Push Notification Service has introduced API to target users who have specific profile/preference. This API requires user preference or profile persistence in Storage Service as a prerequisite. To identify a specific user segmentation, your app user profile or preference information should be saved on App42 cloud e.g. if you are building a music streaming app and you want to send push notifications to all those users who like Rock music, you should have preference of user stored in App42 cloud already and it can be done using Storage Service as shown below.

Saving User Preference

//Set Logged in User Name 
App42API.SetLoggedInUser("USER_NAME"); 
String profileJSON = "{\"preference\":\"Rock\",\"age\":30,\"company\":\"ShepHertz\"}";
StorageService storageService = App42API.BuildStorageService(); 
storageService.InsertJSONDocument("DB_NAME", "COLLECTION_NAME", profileJSON, new Callback() );

Sending Push to Segmented User

Once your app user’s profile/preference information is saved, you can send Push Notification to the segmented user who meets specific criteria (Assuming your app user is already subscribed for push notification. For example, here you can send Push Notification to segmented users who likes Rock music as explained below.

Query targetQuery = QueryBuilder.Build("preference","Rock", Operator.EQUALS);
String message = "New Album of Rock Music has just Arrived. Please check it out here!!";
PushNotificationService pushNotificationService = App42API.BuildPushNotificationService();
pushNotificationService.SendPushToTargetUsers(message, "DB_NAME", "COLLECTION_NAME", targetQuery);
6. Scheduling Message to User

You can also schedule message to your app user on specified time from AppHQ console. To do this follow the simple steps:

  • Login to AppHQ Management Console, Click on Unified Notification
  • Click on Push -> Click on Users
  • Select your App -> select target user and click on manage
  • Click on Send button and type your message in pop up and pass your time to schedule the message.

SchedulePushMessageFromConsole

7. Scheduling message to Channel

Message scheduling on channel can be done in similar way. To do this follow the simple steps:

  • Login to AppHQ Management Console, Click on Unified Notification
  • Click on Push -> Click on Channels
  • Select your App -> select target channel and click on manage
  • Click on Send Push button and type your message in pop up and pass your time to schedule the message.

SchedulePushMessageFromConsole

8. Doing Push Analytics

Push notification goes from App42 server to GCM server and then device. Delivery of Push notification is not guaranteed from GCM to device and it is stated as best effort. Once it is delivered on user device, there might be a chance that user clears it without actually Opening the message.

Using App42 Push Analytics, you can track how many push notification was sent from your side, how many were delivered and how many were opened by the user. You can see these analytics from AppHQ console which will give a better insight of push notification campaign.

If you want to track push message read/opened event you have to simply put following snippet after Message is clicked and lands to your game/app. This will enable tracking of push message and analytics can be seen from AppHQ console.

App42API.BuildLogService().SetEvent("Message", "Opened", new Callback()); 
9. Send Multilingual Push Notification

Once your device is registered for Push Notification you are ready to send multilingual(UTF-8) push message to your app user using following code snippet from any App42 SDK (Android/Java/WP etc)

String userName = "Nick";
String message = "Message which you have to send";
App42Log.SetDebug(true);		//Print output in your editor console
Dictionary<String, String> otherMetaHeaders = new Dictionary<String, String>();
otherMetaHeaders.Add("dataEncoding", "true");
pushNotificationService.SetOtherMetaHeaders(otherMetaHeaders);
pushNotificationService.SendPushMessageToUser(userName,message, new UnityCallBack()); 
public class UnityCallBack : App42CallBack
{
	public void OnSuccess(object response)
	{
		PushNotification pushNotification = (PushNotification) response;  
		App42Log.Console("Message is " + pushNotification.GetMessage());
		App42Log.Console("userName is : " + pushNotification.GetMessage());
	}

	public void OnException(Exception e)
	{
		App42Log.Console("Exception : " + e);
	}
}  

For more details of Push Notification Documentation