【问题标题】:Unity3d: Android and iOS Push NotificationsUnity3d:Android 和 iOS 推送通知
【发布时间】:2015-06-20 22:21:34
【问题描述】:

我正在使用 Unity 开发一款手机游戏,并打算使用推送通知。我发现 NotificationServices 类仅适用于 iOS。但是我没有找到该类的任何服务器端代码示例。

我要求提供服务器和客户端代码(Android 客户端和 Android 和 iOS 的后端)的任何很好的示例或解决方案。

我知道诸如 pushwoosh 之类的服务。他们不适合我,因为我的公司有自己的游戏服务器并想从它发送通知。

我想这不是什么独特的东西,应该已经有人这样做了。

谢谢。

【问题讨论】:

    标签: android ios unity3d push-notification android-notifications


    【解决方案1】:

    你是对的,确实有人这样做了:) 我是新 Unity 资产 UTNotifications 的开发人员之一。它可以满足您的所有需求,甚至更多。另请注意,没有单一的方法可以实现推送通知以与任何 Android 设备一起使用 - 谷歌云消息传递 (GCM) 仅适用于基于 Google Play 的设备,而亚马逊设备消息传递 (ADM) 则适用于亚马逊设备。幸运的是,UTNotifications 既支持服务,也支持 iOS 的 Apple 推送通知服务 (APNS)。它提供了完整的源代码,因此您可以调整任何您喜欢的内容。它还包括演示服务器源代码,因此您可以使用自己的服务器发送推送通知,而无需使用任何 3rd 方服务。

    更多信息:http://forum.unity3d.com/threads/released-utnotifications-professional-cross-platform-push-notifications-and-more.333045/

    这里有一些代码示例。
    例如,您可以通过以下方式初始化系统并将推送通知注册 id 发送到服务器:

    public void Start()
    {
        UTNotifications.Manager notificationsManager = UTNotifications.Manager.Instance;
        notificationsManager.OnSendRegistrationId += SendRegistrationId;
    
        bool result = notificationsManager.Initialize(false);
        Debug.Log("UTNotifications Initialize: " + result);
    }
    
    private void SendRegistrationId(string providerName, string registrationId)
    {
        StartCoroutine(_SendRegistrationId(providerName, registrationId));
    }
    
    private IEnumerator _SendRegistrationId(string providerName, string registrationId)
    {
        WWWForm wwwForm = new WWWForm();
    
        wwwForm.AddField("provider", providerName);
        wwwForm.AddField("id", registrationId);
    
        WWW www = new WWW(m_webServerAddress + "/register", wwwForm);
        yield return www;
    
        if (www.error != null)
        {
            Debug.LogError(www.error);
        }
    }
    

    这就是您请求演示服务器从 Unity 向每个注册设备发送推送通知的方式:

    public IEnumerator NotifyAll(string title, string text)
    {
        title = WWW.EscapeURL(title);
        text = WWW.EscapeURL(text);
    
        WWW www = new WWW(m_webServerAddress + "/notify?title=" + title + "&text=" + text);
        yield return www;
    }
    

    您还可以处理传入的通知:

    UTNotifications.Manager.Instance.OnNotificationsReceived += (receivedNotifications) =>
    {
        Debug.Log(receivedNotifications[0].userData["CustomMessage"]);
    };
    

    你可以用类似的语法做很多其他的事情。该资产包括SampleUI 类。这会对你有很大帮助。还有 API 参考 (http://universal-tools.github.io/UTNotifications/html/annotated.html) 和详细手册。

    【讨论】:

    • 乍一看我觉得不错。你能在这里添加一些代码示例,我该如何使用它?我没有在您提供的链接之后找到它们,只有 API 参考。
    • 我已经编辑了我的原始答案。它现在包含一些代码示例。也欢迎您向我们的支持电子邮件universal.tools.contact@gmail.com 提出任何问题。我们很乐意为您提供帮助!
    猜你喜欢
    • 1970-01-01
    • 2017-04-19
    • 2011-08-07
    • 1970-01-01
    • 2016-04-20
    • 2013-06-11
    • 1970-01-01
    相关资源
    最近更新 更多