//protected void SendPush(string message, PushDevice device, string badge = "+1", string sound = "Default", Dictionary<string, string> metadata = null) //{ // //TODO: This is a stupid dumb hack until i can replace this garbage with a json object hirarchy // var baseAddress = new Uri("https://android.googleapis.com/gcm/send"); // var client = new WebClient(); // client.Headers[HttpRequestHeader.ContentType] = "application/json"; // client.Headers["Authorization"] = "key=AIzaSyCgCPIjtdwNauzxLxFYRrCFiX4A29-X4sM"; // var template = @"{ // ""to"": ""**TOKEN**"", // ""notification"": // { // ""title"": ""Homing In"", // ""body"": ""**MESSAGE**"", // ""sound"": ""**SOUND**"" // } // }"; // Console.Out.WriteLine($"PUSHING TO: {device.DeviceId}"); // var postbody = template.Replace("**MESSAGE**", message); // postbody = postbody.Replace("**BADGE**", badge); // postbody = postbody.Replace("**SOUND**", sound); // postbody = postbody.Replace("**TOKEN**", device.DeviceId); // try // { // client.UploadStringTaskAsync(baseAddress, "POST", postbody).ContinueWith((t) => { // var response = t.Result; // if (t.Exception != null) // { // Debug.WriteLine($"Failed to push to device: {device.DeviceId}"); // Debug.WriteLine(t.Exception.ToString()); // } // else // { // Debug.WriteLine($"Pushed to token: {device.DeviceId}"); // Debug.WriteLine(response); // } // }); // } // catch (Exception exc) // { // Debug.WriteLine($"Failed to push to device: {device.DeviceId}"); // Debug.WriteLine(exc.ToString()); // } //} protected void SendPush(string message, PushDevice device, string badge = "+1", string sound = "Default", Dictionary <string, string> metadata = null) { //if (device.DeviceType == DeviceType.Android) { // SendPushToAndroid(message, device); //} SendPushToFCM(message, device); }
protected void SendPushToFCM(string message, PushDevice device, string badge = "+1", string sound = "Default", Dictionary <string, string> metadata = null) { string fcmAuthKey = "AIzaSyCqx-iSqA8RSLq9anp0oWhaZRJulGp0GJQ"; string fcmSenderKey = "439392935545"; WebRequest tRequest = WebRequest.Create("https://fcm.googleapis.com/fcm/send"); tRequest.Method = "post"; tRequest.ContentType = "application/json"; var objNotification = new { to = device.DeviceId, notification = new { body = "Homing In", title = message } }; var serializer = new JavaScriptSerializer(); string jsonNotificationFormat = serializer.Serialize(objNotification); Byte[] byteArray = Encoding.UTF8.GetBytes(jsonNotificationFormat); tRequest.Headers.Add(string.Format("Authorization: key={0}", fcmAuthKey)); tRequest.Headers.Add(string.Format("Sender: id={0}", fcmSenderKey)); tRequest.ContentLength = byteArray.Length; tRequest.ContentType = "application/json"; try { using (Stream dataStream = tRequest.GetRequestStream()) { dataStream.Write(byteArray, 0, byteArray.Length); using (WebResponse tResponse = tRequest.GetResponse()) { using (Stream dataStreamResponse = tResponse.GetResponseStream()) { using (StreamReader tReader = new StreamReader(dataStreamResponse)) { String responseFromFirebaseServer = tReader.ReadToEnd(); FCMResponse response = serializer.Deserialize <FCMResponse>(responseFromFirebaseServer); } } } } } catch (Exception e) { Debug.WriteLine($"Failed to push to device: {device.DeviceId}"); Debug.WriteLine(e.ToString()); } }
public bool AddPushToken(string UserId, string OldToken, string NewToken, PushDevice PushDevice) { var old = _context.UserPushToken.Where(x => x.Token == OldToken).FirstOrDefault(); if (old != null) { _context.UserPushToken.Remove(old); } var token = new UserPushToken { UserId = UserId != ""?UserId:null, Token = NewToken, PushDevice = PushDevice }; _context.UserPushToken.Add(token); _context.SaveChanges(); return(true); }
protected void SendPushToAndroid(string message, PushDevice device, string badge = "+1", string sound = "Default", Dictionary <string, string> metadata = null) { SendPushToFCM(message, device); string androidPushNotificationHostName = "https://android.googleapis.com/gcm/send"; string googleAppId = "AIzaSyCgCPIjtdwNauzxLxFYRrCFiX4A29-X4sM"; var senderId = "2464547879"; string deviceToken = device.DeviceId; string responseFromServer = ""; try { WebRequest tRequest; tRequest = WebRequest.Create(androidPushNotificationHostName); tRequest.Method = "post"; tRequest.ContentType = " application/x-www-form-urlencoded;charset=UTF-8"; tRequest.Headers.Add(string.Format("Authorization: key={0}", googleAppId)); tRequest.Headers.Add(string.Format("Sender: id={0}", senderId)); string postData = "collapse_key=score_update&time_to_live=108&delay_while_idle=1" + "¬ification.title=Homing In" + "¬ification.body=" + message + "&data.time=" + System.DateTime.Now.ToString() + "®istration_id=" + deviceToken + ""; Byte[] byteArray = Encoding.UTF8.GetBytes(postData); tRequest.ContentLength = byteArray.Length; Stream dataStream = tRequest.GetRequestStream(); dataStream.Write(byteArray, 0, byteArray.Length); dataStream.Close(); WebResponse tResponse = tRequest.GetResponse(); dataStream = tResponse.GetResponseStream(); if (dataStream != null) { StreamReader tReader = new StreamReader(dataStream); responseFromServer = tReader.ReadToEnd(); tReader.Close(); } dataStream.Close(); tResponse.Close(); } catch (Exception e) { Debug.WriteLine($"Failed to push to device: {device.DeviceId}"); Debug.WriteLine(e.ToString()); } }
public void OnUnregistered(DeviceType deviceType) { Debug.WriteLine("Push Notification - Device Unnregistered"); PushDevice device = PushDevice.NOT_SET; switch (deviceType) { case DeviceType.Android: device = PushDevice.ANDROID; break; case DeviceType.iOS: device = PushDevice.IOS; break; case DeviceType.Windows: device = PushDevice.WINDOWS_UWP; break; } OnUnregisteredAction?.Invoke(device); }
public void OnRegistered(string token, DeviceType deviceType) { Debug.WriteLine(string.Format("Push Notification - Device Registered - Token : {0}", token)); PushDevice device = PushDevice.NOT_SET; switch (deviceType) { case DeviceType.Android: device = PushDevice.ANDROID; break; case DeviceType.iOS: device = PushDevice.IOS; break; case DeviceType.Windows: device = PushDevice.WINDOWS_UWP; break; } OnRegisteredAction?.Invoke(token, device); }
public object Post(RegisterPushDeviceRequest request) { if (request == null || string.IsNullOrWhiteSpace(request.DeviceId)) { throw new ArgumentException("Missing parameter 'DeviceId'"); } if (request.DeviceType == DeviceType.Unknown) { throw new ArgumentException("Missing parameter 'DeviceType'"); } //verify authenticated user var requestingUser = ValidateAndGetCurrentUser(); if (requestingUser.Id != request.UserId) { throw new UnauthorizedAccessException("'UserId' must be the same as the authenticated user."); } //see if this device is already registered var deviceId = request.DeviceId.Trim(); var device = Db.Single <PushDevice>(d => d.DeviceId == deviceId && d.UserId == requestingUser.Id); if (device == null) { device = new PushDevice { UserId = requestingUser.Id, DeviceId = deviceId, DeviceType = request.DeviceType }; } Db.Save <PushDevice>(device); return(new RegisterPushDeviceResponse { Success = device.Id > 0 }); }