// private FcmClientSettings settings; public Broadcaster() { // Save our hub context so we can easily use it // to send to its connected clients _hubContext = GlobalHost.ConnectionManager.GetHubContext <InfoHub>(); _model = new Gempa(); _modelUpdated = false; // settings = FileBasedFcmClientSettings.CreateFromFile("bmkg-f7780", AppDomain.CurrentDomain.BaseDirectory + "bmkg-f7780-firebase-adminsdk-3zfxv-f298fdfb18.json"); // Start the broadcast loop _broadcastLoop = new Timer( BroadcastShape, null, BroadcastInterval, BroadcastInterval); }
public async void BroadcastShape(object state) { // Construct the Client: //using (var client = new FcmClient(settings)) //{ // // Construct the Data Payload to send: // var data = new Dictionary<string, string>() // { // {"A", "B"}, // {"C", "D"} // }; // // The Message should be sent to the News Topic: // var message = new FcmMessage() // { // ValidateOnly = false, // Message = new Message // { Notification =new Notification() { Title="Judul", Body="BODY"}, // Topic = "news" // } // }; // try // { // CancellationTokenSource cts = new CancellationTokenSource(); // // Send the Message and wait synchronously: // var results = client.SendAsync(message, cts.Token).GetAwaiter().GetResult(); // // Print the Result to the Console: // System.Console.WriteLine("Message ID = {0}", results.Name); // } // catch (Exception ex) // { // } // // Finally send the Message and wait for the Result: //} // No need to send anything if our model hasn't changed await Task.Delay(10); var result = GetGempaInfo(); if (result != null) { if (_model == null) { _model = result; _modelUpdated = true; } else if (_model.Tanggal != result.Tanggal || _model.Jam != result.Jam) { _model = result; _modelUpdated = true; } _modelUpdated = true; } if (_modelUpdated) { _model.Jam = DateTime.Now.ToLongTimeString() + " WIT"; _hubContext.Clients.AllExcept(_model.LastUpdatedBy).updateShape(_model); using (var client = new HttpClient()) { client.BaseAddress = new Uri("https://api.appcenter.ms"); client.DefaultRequestHeaders.Accept.Clear(); client.DefaultRequestHeaders.Accept.Add( new MediaTypeWithQualityHeaderValue("application/json")); HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, "/v0.1/apps/ocph23/TestApp/push/notifications"); var str = "{'notification_content' : { 'name' : 'test', 'title' : 'Gempa Dirasakan', 'body' : '" + _model.Wilayah1 + "', 'custom_data':{'sound' : 'alarm', 'waktu' : 'dimuka'}}}"; request.Content = new StringContent(str, Encoding.UTF8, "application/json");//CONTENT-TYPE header request.Content.Headers.Add("x-api-token", "4b46ec5c9477e63a4701a8d1291b9f8c877eb9d1"); //request.Content.Headers.ContentType = new MediaTypeHeaderValue("application/x-www-form-urlencoded"); await client.SendAsync(request) .ContinueWith(responseTask => { Console.WriteLine("Response: {0}", responseTask.Result); }); } _modelUpdated = false; } }
public void UpdateModel(Gempa clientModel) { clientModel.LastUpdatedBy = Context.ConnectionId; // Update the shape model within our broadcaster _broadcaster.UpdateShape(clientModel); }
public void UpdateShape(Gempa clientModel) { _model = clientModel; _modelUpdated = true; }