3

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.)

2 Answers 2

3

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
        }
    }
}
Sign up to request clarification or add additional context in comments.

4 Comments

Hmm, is this officially supported or might this change?
I can create an Installation object follow this code. However, parse still says "No registered devices" when send push.
This doesn't work anymore. Can't modify deviceType of installation object, and if not set the saveasync fails with a "file not found". Tried to use this method because my app doesnt create installation object on ios (works fine on android though).
Please have a look at other answer here. Tested and works
0

To implement iOS push with parse.com You need to get the token from apple first. Then save Current instalation Unity does have this functionality build in now.

//push notification
bool tokenSent = false;


void RegisterForPush()
{
    Debug.Log("Register for push");
    tokenSent = false;

    #if UNITY_IOS
    UnityEngine.iOS.NotificationServices.RegisterForNotifications(NotificationType.Alert |
                                                            NotificationType.Badge |
                                                            NotificationType.Sound, true);

    #endif

}


void Update () {

        if (!tokenSent) {
            byte[] token = UnityEngine.iOS.NotificationServices.deviceToken;

            if (token != null) {
                // send token to a provider
                tokenSent = true;
                string hexToken = "%" + System.BitConverter.ToString(token).Replace('-', '%');

                #if UNITY_IOS
                    ParseManager.instance.RegisterForPush(hexToken);
                #endif

            }
        }
}

And inside ParseManager (or whatever class you use to manage parse>client communication)

public void RegisterForPush(string token) {

    Debug.Log("Parse updating instalation");

    ParseInstallation instalation = ParseInstallation.CurrentInstallation;
    instalation["deviceToken"] = token;
    instalation["user"] = ParseUser.CurrentUser.ObjectId;
    instalation["timeZoneOffset"] = TimeZone.CurrentTimeZone.GetUtcOffset(DateTime.Now);
    instalation.SaveAsync();

}

Tested on Unity 5.1.2 and iOS 8.4

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.