Exemplo n.º 1
0
    // ______________________________________
    //
    // 6. NOTIFICATION SENDING.
    // ______________________________________
    //


    public static void Send_Notification(FCM_Params param)
    {
        if (param.Notification_Key == "")
        {
            Get_Notification_Key("user_id" + param.User_Id.ToString(), (object[] data_get) =>
            {
                Response_Status status_get = Http_Client.Parse_Status(data_get[0]);

                if (status_get == Response_Status.Ok)
                {
                    param.Notification_Key = (string)data_get[1];
                    Send_Notification(param);
                }
                else
                {
                    Debug.LogWarning("Could not send notification due to error on getting the notification_key!");
                    param.Concluding_Method?.Invoke(new object[] { Response_Status.HTTP_Error, "notification_key not found", "-1" });
                }
            });
            return;
        }

        string content =
            "{" +
            s + "to" + s + ":" + s + param.Notification_Key + s + "," +
            s + "data" + s +
            ":{";

        if (param.Data_Pairs != null)
        {
            foreach (KeyValuePair <string, string> data in param.Data_Pairs)
            {
                content += s + data.Key + s + ":" + s + data.Value + s + ",";
            }
        }

        if (content[content.Length - 1] == ',')
        {
            content = content.Substring(0, content.Length - 1);
        }

        content +=
            "}" +
            s + "notification" + s + ": " +
            "{" +
            s + "title" + s + ": " + s + param.Title + s + "," +
            s + "body" + s + ": " + s + param.Body + s + "," +
            s + "priority" + s + ": " + param.Priority +
            "}" +
            "}";

        Http_Client.Send_HTTP_Request(
            "https://fcm.googleapis.com/fcm/send",
            Http_Client.Request_Type.POST,
            default_header,
            Send_Notification_Response,
            param.Concluding_Method,
            content
            );
    }
Exemplo n.º 2
0
    // ______________________________________
    //
    // 5. REGISTRATION TOKEN HANDLING.
    // ______________________________________
    //


    public static void Modify_Registration_Token(Operation operation, FCM_Params param = null)
    {
        if (param == null)
        {
            param = new FCM_Params();
        }

        if (param.Notification_Key_Name == "")
        {
            param.Notification_Key_Name = "user_id" + User.User_Info.Id.ToString();
        }

        if (param.Registration_Token == "")
        {
            param.Registration_Token = Own_Registration_Token;
        }

        if (param.Notification_Key.Length <= 1)
        {
            if (Own_Notification_Key.Length > 1)
            {
                param.Notification_Key = Own_Notification_Key;
            }
            else
            {
                Get_Notification_Key("", (object[] data_get) =>
                {
                    switch (Http_Client.Parse_Status(data_get[0]))
                    {
                    case Response_Status.Ok:
                        Modify(data_get);
                        break;

                    case Response_Status.HTTP_Error:
                        if ((string)data_get[1] == "notification_key not found")
                        {
                            Create_Notification_Key("", "", (object[] data_create) =>
                            {
                                if (Http_Client.Parse_Status(data_create[0]) == Response_Status.Ok)
                                {
                                    Modify(data_create);
                                }
                                else
                                {
                                    Debug.LogError("Could not get notifiaction_key!");
                                }

                                param.Concluding_Method?.Invoke(data_create);
                            });
                        }
                        else
                        {
                            param.Concluding_Method?.Invoke(data_get);
                        }
                        break;

                    default:
                        Debug.LogError("Could not get the notification_key of notification_key_name " + param.Notification_Key_Name);
                        param.Concluding_Method?.Invoke(data_get);
                        break;
                    }

                    void Modify(object[] data_param)
                    {
                        Modify_Registration_Token(operation, new FCM_Params()
                        {
                            Notification_Key  = (string)data_param[1],
                            Concluding_Method = (object[] data_modify) =>
                            {
                                if (Http_Client.Parse_Status(data_modify[0]) == Response_Status.Ok)
                                {
                                    Debug.Log("Registration_token has been " + operation + "ed successfully from notification_key_name " + param.Notification_Key_Name);
                                }
#if !UNITY_EDITOR
                                else
                                {
                                    Debug.LogError("Could not " + operation.ToString() + " the registration_token " + param.Registration_Token + " from notification_key_name " +
                                                   param.Notification_Key_Name + " and notification_key " + param.Notification_Key);
                                }
#endif
                                param.Concluding_Method?.Invoke(data_modify);
                            }
                        });
                    }
                });
                return;
            }
        }

        string content =
            "{" +
            s + "operation" + s + ":" + s + operation.ToString() + s + "," +
            s + "notification_key_name" + s + ":" + s + param.Notification_Key_Name + s + "," +
            s + "notification_key" + s + ":" + s + param.Notification_Key + s + "," +
            s + "registration_ids" + s + ":[" + s + param.Registration_Token + s + "]" +
            "}";

        Http_Client.Send_HTTP_Request(
            "https://fcm.googleapis.com/fcm/notification",
            Http_Client.Request_Type.POST,
            default_header,
            Get_Notification_Key_Response,
            param.Concluding_Method,
            content
            );
    }