Steps to integrate Push Notification with Unity3d on IOS

Source: Internet
Author: User
Tags call back ssl certificate
Steps to integrate Push Notification with Unity3d on IOSTime 2013-08-09 00:09:38 Dzone-java original 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 a easy task and would explain the process of doing this THR Ough 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; prerequisites are: Create a new App ID and provisioning profile for each app that uses push, as as a SSL certificate for the server. To does this should have a iOS Developer program membership on iOS Dev Center. Create. p12 file from the SSL certificate which is downloaded in the above step from the IOS Dev Center. A Server and this are where App42 Push Service comes into the picture.

Note: If you are are new to Push notifications for IOS or APP42, you can go through my previous blog A.

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 we "APP42PUSHHANDLERINTERNAL.H/.M" classes to "Assets/plugins/ios" folder and "Pushs Cript.cs "C # script to" Assets "folder. Assign PushScript.cs to a game object; In the demo it is assigned to the Main Camera. So, let ' s browse through the PushScript.cs code.

To receive push notifications, the IOS needs to is notified that your APP wants to receive push notifications, and "app42p USHHANDLERINTERNAL.M "class does it by default.

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

1
2
3
4
5
Use this for initialization
void Start ()
{
setlistenergameobject. gameobject.name);
}

As "APP42PUSHHANDLERINTERNAL.M" sends a request to the 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" called back of PushScript.cs.

1 2 3 4 5 6 7 8 
//sent 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 the register this device to the APP42 push Notification Service to start sending/receiving push notifications. To doing that, just called "Registerdevicetokentoapp42pushnotificationservice" method of PushScript.cs from the above call BAC K.

1 2 3 4 5 6 7 
//registers a user with the given device token to APP42 push notification service  void  Registerdevicetok Entoapp42pushnotificationservice (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, can be written/called wherever it's required to send a request to APP42 Server to send a Push Notification to a specific user.

1
2
3
4
5
6 7
Sends push to a given user
void sendpushtouser (string username,string message)
{
new Serviceapi (api_key,secret_key);
Pushnotificationservice Pushservice = Serviceapi.buildpushnotificationservice ();
Pushservice.sendpushmessagetouser (username,message);
}

The "onpushnotificationsreceived" call back of the "Pushscript" is called when you receive a push notification.

1
2
3
4
5
6 7
Sent when the application receives a push notification
void onpushnotificationsreceived (string pushmessagestring)
{
Console.WriteLine ("onpushnotificationsreceived");
Dump your code here to handle the pushmessagestring
Debug.Log (pushmessagestring);
}

Now your app. has been successfully set up to receive/send push notifications through we App42 Server using APP42 push not Ification Service.

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.