static void Send_Notification_Response(object[] data, Action <object[]> concluding_method) { Response_Status status = Http_Client.Parse_Status(data[0]); if (status < Response_Status.Network_Error) { SimpleJSON.JSONNode raw = SimpleJSON.JSON.Parse((string)data[2]); if (status == Response_Status.Ok) { if (raw["failure"].Value == "0") { Debug.Log("Success on sending notification."); concluding_method?.Invoke(new object[] { status, raw["success"].Value, "0" }); } else { Debug.LogWarning("Correct HTTP response but sending failures exists! (" + raw["success"].Value + " success, " + raw["failure"].Value + " failures)"); concluding_method?.Invoke(new object[] { status, raw["success"].Value, raw["failure"].Value }); } } else { concluding_method?.Invoke(new object[] { status, raw["error"].Value, "-1" }); } } }
// ______________________________________ // // 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 ); }
static void Get_Notification_Key_Response(object[] data, Action <object[]> concluding_method) { Response_Status status = Http_Client.Parse_Status(data[0]); if (status < Response_Status.Network_Error) { SimpleJSON.JSONNode raw = SimpleJSON.JSON.Parse((string)data[2]); if (status == Response_Status.Ok) { if (raw["notification_key"] != null) { Debug.Log("Notification key is " + raw["notification_key"].Value); concluding_method?.Invoke(new object[] { status, raw["notification_key"].Value }); } else { Debug.LogError("Correct HTTP response but notification key is null!"); } } else { concluding_method?.Invoke(new object[] { status, raw["error"].Value }); } } }
// ______________________________________ // // 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 ); }