Beispiel #1
0
    // Test Menu
    // Includes SendTag/SendTags, getting the userID and pushToken, and scheduling an example notification
    void OnGUI()
    {
        GUIStyle customTextSize = new GUIStyle("button");

        customTextSize.fontSize = 30;

        GUIStyle guiBoxStyle = new GUIStyle("box");

        guiBoxStyle.fontSize = 30;

        GUIStyle textFieldStyle = new GUIStyle("textField");

        textFieldStyle.fontSize = 30;


        float itemOriginX      = 50.0f;
        float itemWidth        = Screen.width - 120.0f;
        float boxWidth         = Screen.width - 20.0f;
        float boxOriginY       = 120.0f;
        float boxHeight        = requiresUserPrivacyConsent ? 980.0f : 890.0f;
        float itemStartY       = 200.0f;
        float itemHeightOffset = 90.0f;
        float itemHeight       = 60.0f;

        GUI.Box(new Rect(10, boxOriginY, boxWidth, boxHeight), "Test Menu", guiBoxStyle);

        float count = 0.0f;

        if (GUI.Button(new Rect(itemOriginX, itemStartY + (count * itemHeightOffset), itemWidth, itemHeight), "SendTags", customTextSize))
        {
            // You can tags users with key value pairs like this:
            OneSignal.SendTag("UnityTestKey", "TestValue");
            // Or use an IDictionary if you need to set more than one tag.
            OneSignal.SendTags(new Dictionary <string, string>()
            {
                { "UnityTestKey2", "value2" }, { "UnityTestKey3", "value3" }
            });

            // You can delete a single tag with it's key.
            // OneSignal.DeleteTag("UnityTestKey");
            // Or delete many with an IList.
            // OneSignal.DeleteTags(new List<string>() {"UnityTestKey2", "UnityTestKey3" });
        }

        count++;

        if (GUI.Button(new Rect(itemOriginX, itemStartY + (count * itemHeightOffset), itemWidth, itemHeight), "GetIds", customTextSize))
        {
            OneSignal.IdsAvailable((userId, pushToken) => {
                extraMessage = "UserID:\n" + userId + "\n\nPushToken:\n" + pushToken;
            });
        }


        count++;

        if (GUI.Button(new Rect(itemOriginX, itemStartY + (count * itemHeightOffset), itemWidth, itemHeight), "TestNotification", customTextSize))
        {
            extraMessage = "Waiting to get a OneSignal userId. Uncomment OneSignal.SetLogLevel in the Start method if it hangs here to debug the issue.";
            OneSignal.IdsAvailable((userId, pushToken) => {
                if (pushToken != null)
                {
                    // See http://documentation.onesignal.com/docs/notifications-create-notification for a full list of options.
                    // You can not use included_segments or any fields that require your OneSignal 'REST API Key' in your app for security reasons.
                    // If you need to use your OneSignal 'REST API Key' you will need your own server where you can make this call.

                    var notification         = new Dictionary <string, object>();
                    notification["contents"] = new Dictionary <string, string>()
                    {
                        { "en", "Test Message" }
                    };
                    // Send notification to this device.
                    notification["include_player_ids"] = new List <string>()
                    {
                        userId
                    };
                    // Example of scheduling a notification in the future.
                    notification["send_after"] = System.DateTime.Now.ToUniversalTime().AddSeconds(30).ToString("U");

                    extraMessage = "Posting test notification now.";

                    OneSignal.PostNotification(notification, (responseSuccess) => {
                        extraMessage = "Notification posted successful! Delayed by about 30 secounds to give you time to press the home button to see a notification vs an in-app alert.\n" + Json.Serialize(responseSuccess);
                    }, (responseFailure) => {
                        extraMessage = "Notification failed to post:\n" + Json.Serialize(responseFailure);
                    });
                }
                else
                {
                    extraMessage = "ERROR: Device is not registered.";
                }
            });
        }

        count++;

        email = GUI.TextField(new Rect(itemOriginX, itemStartY + (count * itemHeightOffset), itemWidth, itemHeight), email, customTextSize);

        count++;

        if (GUI.Button(new Rect(itemOriginX, itemStartY + (count * itemHeightOffset), itemWidth, itemHeight), "SetEmail", customTextSize))
        {
            extraMessage = "Setting email to " + email;

            OneSignal.SetEmail(email, () => {
                Debug.Log("Successfully set email");
            }, (error) => {
                Debug.Log("Encountered error setting email: " + Json.Serialize(error));
            });
        }

        count++;

        if (GUI.Button(new Rect(itemOriginX, itemStartY + (count * itemHeightOffset), itemWidth, itemHeight), "LogoutEmail", customTextSize))
        {
            extraMessage = "Logging Out of [email protected]";

            OneSignal.LogoutEmail(() => {
                Debug.Log("Successfully logged out of email");
            }, (error) => {
                Debug.Log("Encountered error logging out of email: " + Json.Serialize(error));
            });
        }

        count++;

        externalId = GUI.TextField(new Rect(itemOriginX, itemStartY + (count * itemHeightOffset), itemWidth, itemHeight), externalId, customTextSize);

        count++;

        if (GUI.Button(new Rect(itemOriginX, itemStartY + (count * itemHeightOffset), itemWidth, itemHeight), "SetExternalId", customTextSize))
        {
            extraMessage = "Setting External User Id";

            OneSignal.SetExternalUserId(externalId);
        }

        count++;

        if (GUI.Button(new Rect(itemOriginX, itemStartY + (count * itemHeightOffset), itemWidth, itemHeight), "RemoveExternalId", customTextSize))
        {
            extraMessage = "Removing External User Id";

            OneSignal.RemoveExternalUserId();
        }

        if (requiresUserPrivacyConsent)
        {
            count++;

            if (GUI.Button(new Rect(itemOriginX, itemStartY + (count * itemHeightOffset), itemWidth, itemHeight), (OneSignal.UserProvidedConsent() ? "Revoke Privacy Consent" : "Provide Privacy Consent"), customTextSize))
            {
                extraMessage = "Providing user privacy consent";

                OneSignal.UserDidProvideConsent(!OneSignal.UserProvidedConsent());
            }
        }

        if (extraMessage != null)
        {
            guiBoxStyle.alignment = TextAnchor.UpperLeft;
            guiBoxStyle.wordWrap  = true;
            GUI.Box(new Rect(10, boxOriginY + boxHeight + 20, Screen.width - 20, Screen.height - (boxOriginY + boxHeight + 40)), extraMessage, guiBoxStyle);
        }
    }