Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parse.com - iOS push notifications and Unity integration

I notice that Parse Unity support still doesn't provide push notification for iOS.

Has anyone implemented a Unity plugin or another solution to support iOS Push Notifications via Parse?

(Cross posted on Unity Answers.)

like image 658
ina Avatar asked May 19 '26 00:05

ina


1 Answers

It's actually possible now, using a ParseObject to mock up the ParseInstallation object.

Gist here: https://gist.github.com/gfosco/a3d092651c32ba3385e6

Explanation in the Parse Google Group: https://groups.google.com/d/msg/parse-developers/ku8-r91_o6s/6ioQ9T2TP7wJ

Attach this script to a GameObject, replace the important parts with your own:

using UnityEngine;
using System.Collections;
using Parse;

public class PushBehaviorScript : MonoBehaviour {

    bool tokenSent = false;
    public ParseObject currentInstallation = null;

    void Start () {
        if (PlayerPrefs.HasKey ("currentInstallation")) {
            string objId = PlayerPrefs.GetString ("currentInstallation");
            currentInstallation = ParseObject.CreateWithoutData ("_Installation", objId);
        }

        if (currentInstallation == null) {
            #if UNITY_IPHONE && !UNITY_EDITOR
                NotificationServices.RegisterForRemoteNotificationTypes (RemoteNotificationType.Alert | RemoteNotificationType.Badge | RemoteNotificationType.Sound);
            #endif
        }
    }

    void  FixedUpdate () {
        if (!tokenSent && currentInstallation == null) {
            #if UNITY_IPHONE && !UNITY_EDITOR
                byte[] token   = NotificationServices.deviceToken;
                if(token != null) {
                    tokenSent = true;
                    string tokenString =  System.BitConverter.ToString(token).Replace("-", "").ToLower();
                    Debug.Log ("OnTokenReived");
                    Debug.Log (tokenString);
                    ParseObject obj = new ParseObject("_Installation");
                    obj["deviceToken"] = tokenString;
                    obj["appIdentifier"] = "com.parse.unitypush";
                    obj["deviceType"] = "ios";
                    obj["timeZone"] = "UTC";
                    obj["appName"] = "UnityPushTest";
                    obj["appVersion"] = "1.0.0";
                    obj["parseVersion"] = "1.3.0";
                    obj.SaveAsync().ContinueWith(t =>
                    {
                        if (obj.ObjectId != null) {
                            PlayerPrefs.SetString ("currentInstallation", obj.ObjectId);
                        }
                    });
                }
            #endif
        }
    }
}
like image 168
Fosco Avatar answered May 21 '26 02:05

Fosco



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!