send notofication to mobile
This commit is contained in:
1 parent
0a27008cfd
commit
c27464f1a2
4 files changed
+68
-26
No files matched your search
@@ -1,7 +1,5 @@
|
||||
package application;
|
||||
|
||||
import application.events.impl.EntryEvent;
|
||||
import application.model.User;
|
||||
import application.repositories.*;
|
||||
import application.utils.Initializer;
|
||||
import application.utils.TokensGenerator;
|
||||
@@ -30,7 +28,7 @@ import org.springframework.web.servlet.view.JstlView;
|
||||
@EnableScheduling
|
||||
@EnableWebMvc
|
||||
public class Config extends SpringBootServletInitializer {
|
||||
|
||||
public final static String GCM_KEY = "AIzaSyBOK0PgwyTopom2thLGBmLS3lOzTkDKm0M";
|
||||
|
||||
@Scheduled(fixedRate = 50000)
|
||||
public void generatePassword() {
|
||||
|
||||
@@ -1,14 +1,20 @@
|
||||
package application.events;
|
||||
|
||||
import application.Config;
|
||||
import application.events.impl.DoorLockedEvent;
|
||||
import application.events.impl.EntryEvent;
|
||||
import application.events.impl.MainDoorEntryEvent;
|
||||
import application.model.Notification;
|
||||
import application.model.User;
|
||||
import application.repositories.*;
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import org.reflections.Reflections;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.http.HttpEntity;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
|
||||
import java.util.HashSet;
|
||||
@@ -39,13 +45,14 @@ public class EventManager {
|
||||
|
||||
Set<Class<? extends EventObject>> eventClassSet;
|
||||
Set<EventObject> eventsSet;
|
||||
|
||||
public EventManager() {
|
||||
/* eventClassSet = findEventClasses();
|
||||
eventsSet = instantiateEvents(eventClassSet);*/
|
||||
System.out.println("Class EventManagerCreated");
|
||||
}
|
||||
|
||||
public Set<Class<? extends EventObject>> findEventClasses(){
|
||||
public Set<Class<? extends EventObject>> findEventClasses() {
|
||||
System.out.println("Class Finder started...");
|
||||
Reflections reflections = new Reflections("application.events.impl");
|
||||
Set<Class<? extends EventObject>> classes = reflections.getSubTypesOf(EventObject.class);
|
||||
@@ -53,12 +60,12 @@ public class EventManager {
|
||||
return classes;
|
||||
}
|
||||
|
||||
public Set<EventObject> instantiateEvents(Set<Class<? extends EventObject>> classes){
|
||||
public Set<EventObject> instantiateEvents(Set<Class<? extends EventObject>> classes) {
|
||||
Set<EventObject> eventObjectSet = new HashSet<>();
|
||||
for (Class<? extends EventObject> c : classes) {
|
||||
try {
|
||||
EventObject obj=c.newInstance();
|
||||
System.out.println("Crated new Event: "+c.getName());
|
||||
EventObject obj = c.newInstance();
|
||||
System.out.println("Crated new Event: " + c.getName());
|
||||
eventObjectSet.add(obj);
|
||||
} catch (InstantiationException e) {
|
||||
e.printStackTrace();
|
||||
@@ -69,42 +76,54 @@ public class EventManager {
|
||||
return eventObjectSet;
|
||||
}
|
||||
|
||||
public void startEvents(){
|
||||
for(EventObject e:eventsSet){
|
||||
public void startEvents() {
|
||||
for (EventObject e : eventsSet) {
|
||||
e.validEvent();
|
||||
if(e.isValid())e.handleEvent();
|
||||
if (e.isValid()) e.handleEvent();
|
||||
}
|
||||
}
|
||||
|
||||
public void sendNotification(EventObject eventObject,User user){
|
||||
public void sendNotification(EventObject eventObject, User user) {
|
||||
String uri = "https://android.googleapis.com/gcm/send";
|
||||
System.out.print(eventObject.createMessage() + user);
|
||||
RestTemplate restTemplate = new RestTemplate();
|
||||
restTemplate.getForObject(uri,
|
||||
String.class);
|
||||
Notification notification = new Notification(eventObject, user);
|
||||
|
||||
HttpHeaders headers = new HttpHeaders();
|
||||
headers.setContentType(MediaType.APPLICATION_JSON);
|
||||
headers.add("Authorization", "key=" + Config.GCM_KEY);
|
||||
HttpEntity<String> entity;
|
||||
try {
|
||||
entity = new HttpEntity<>(new ObjectMapper().writeValueAsString(notification), headers);
|
||||
} catch (JsonProcessingException e) {
|
||||
entity = new HttpEntity<>("Ni mo dżejsonów", headers);
|
||||
}
|
||||
|
||||
restTemplate.postForObject(uri, entity, Object.class, headers);
|
||||
}
|
||||
|
||||
public void processEvent(long userId , long doorId){
|
||||
if(doorId==1){
|
||||
for(MainDoorEntryEvent e: mainDoorEntryEventRepository.findAll()){
|
||||
if(e.getWho()==userId){
|
||||
sendNotification(e,userRepository.findOne(e.getUser()));
|
||||
public void processEvent(long userId, long doorId) {
|
||||
if (doorId == 1) {
|
||||
for (MainDoorEntryEvent e : mainDoorEntryEventRepository.findAll()) {
|
||||
if (e.getWho() == userId) {
|
||||
sendNotification(e, userRepository.findOne(e.getUser()));
|
||||
}
|
||||
}
|
||||
}else {
|
||||
} else {
|
||||
for (EntryEvent e : entryEventRepository.findAll()) {
|
||||
if (e.getWho() == userId && e.getDoor() == doorId)
|
||||
sendNotification(e,userRepository.findOne(e.getUser()));
|
||||
sendNotification(e, userRepository.findOne(e.getUser()));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void doorLockedEvent(DoorLockedEvent doorLockedEvent){
|
||||
for(User user:userRepository.findAll()){
|
||||
sendNotification(doorLockedEvent,user);
|
||||
public void doorLockedEvent(DoorLockedEvent doorLockedEvent) {
|
||||
for (User user : userRepository.findAll()) {
|
||||
sendNotification(doorLockedEvent, user);
|
||||
}
|
||||
}
|
||||
public void doorUnlockedEvent(DoorLockedEvent doorLockedEvent){
|
||||
|
||||
public void doorUnlockedEvent(DoorLockedEvent doorLockedEvent) {
|
||||
|
||||
}
|
||||
}
|
||||
@@ -34,7 +34,7 @@ public class MainDoorEntryEvent implements EventObject {
|
||||
|
||||
@Override
|
||||
public String createMessage() {
|
||||
return null;
|
||||
return "User " + who + " entered Main Door";
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,25 @@
|
||||
package application.model;
|
||||
|
||||
import application.events.EventObject;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.ArrayList;
|
||||
|
||||
/**
|
||||
* Created by Marcin on 2015-02-12.
|
||||
*/
|
||||
public class Notification implements Serializable {
|
||||
public ArrayList<String> registration_ids;
|
||||
public Data data;
|
||||
|
||||
public static class Data{
|
||||
public String message;
|
||||
}
|
||||
|
||||
public Notification(EventObject eventObject, User user) {
|
||||
registration_ids = new ArrayList<>();
|
||||
registration_ids.add(user.getRegistratioId());
|
||||
data = new Data();
|
||||
data.message = eventObject.createMessage();
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user