Steps to Integrate Push Notification with Unity3d on iOS

來源:互聯網
上載者:User
Steps to Integrate Push Notification with Unity3d on iOS 時間 2013-08-09 00:09:38   DZone-java 原文  http://blogs.shephertz.com/2013/08/08/steps-to-integrate-push-notification-with-unity3d-on-ios/

Implementing Push Notifications in Unity Apps/games for iOS is an easy task and 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 .

To configure Push Notifications for iOS Apps/games; the prerequisites are: Create a new App ID and provisioning profile for each App that uses push, as well as a SSL certificate for the server. To do this you should have  an iOS Developer Program membership on iOS Dev Center . Create .p12 file from the SSL certificate which was downloaded in the above step from the iOS Dev Center. A Server and 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 previous blog  first.

Now let’s understand how the sample code implements the Push Notifications by examining the key code snippets. 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.

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:

12345
// Use this for initializationvoid 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.

12345678
//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.

1234567
//Registers a user with the given device token to APP42 push notification servicevoid registerDeviceTokenToApp42PushNotificationService(string devToken,string userName){ServiceAPI serviceAPI = new ServiceAPI(api_key,secret_key);PushNotificationService pushService = serviceAPI.BuildPushNotificationService();pushService.StoreDeviceToken(userName,devToken,"iOS");}

The SendPushToUser method of this script that can be written/called wherever it’s required to send a request to App42 server to send a Push Notification to a specific user.

1234567
//Sends push to a given uservoid SendPushToUser(string userName,string message){ServiceAPI serviceAPI = new ServiceAPI(api_key,secret_key);PushNotificationService pushService = serviceAPI.BuildPushNotificationService();pushService.SendPushMessageToUser(userName,message);}

The “onPushNotificationsReceived” call back of the PushScript will be called when you receive a push notification.

1234567
//Sent when the application Receives a push notificationvoid onPushNotificationsReceived(string pushMessageString){Console.WriteLine("onPushNotificationsReceived");//dump you code here to handle the pushMessageStringDebug.Log(pushMessageString);}

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

If you have any questions or need any further assistance to integrate this in your App, please feel free to write us at support@shephertz.com

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.