提交 8e29972d authored 作者: Whispa's avatar Whispa

commit

上级 b0e45e52
......@@ -46,7 +46,7 @@ import java.util.stream.Collectors;
@CrossOrigin(origins = "*", maxAge = 3600)
@RestController
@RequestMapping("/api/auth")
public class AuthController {
public class AuthController extends Controller {
private final AuthenticationManager authenticationManager;
private final UserRepository userRepository;
......@@ -94,6 +94,10 @@ public class AuthController {
userInfo.setToken(jwt);
if( userInfo.hasFcm() ){
sendNotification(userInfo.getFcm(),"Welcome back",userInfo.display() + ", Welcome back and enjoy shopping ");
}
return new Result<>(userInfo);
}
......
package com.example.afrishop_v3.controllers;
import com.example.afrishop_v3.util.HttpClientUtil;
import com.example.afrishop_v3.util.IdUtil;
import org.json.JSONObject;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.web.client.RestTemplate;
import java.util.HashMap;
class Controller {
String uid(){
return IdUtil.createIdbyUUID();
}
static int id = 0;
final String serverToken = "AAAAg-FJSfc:APA91bHr7OgmwmcP401YFBJZK_hkeeSO2qbMHXZaJa7Zup9U8dZ8XQqlFnF_z5q5fa9wskRu4PNiK8IMn0lYBjxJgi8srcafC1Mc-p2GU6T8v5kAQKvjwZoDJ_R0PVCvfroX09muIwln";
final String firebaseUrl = "https://fcm.googleapis.com/fcm/send";
boolean sendNotification(String token,String title,String message){
// Dio().post(
// 'https://fcm.googleapis.com/fcm/send',
// options: Options(headers: <String, String>{
// 'Content-Type': 'application/json',
// 'Authorization': 'key=$serverToken',
// }),
// data: <String, dynamic>{
// 'notification': <String, dynamic>{
// 'body': 'this is a body from app',
// 'title': 'this is a title from app'
// },
// 'priority': 'high',
// 'data': <String, dynamic>{
// 'click_action': 'FLUTTER_NOTIFICATION_CLICK',
// 'id': '1',
// 'status': 'done'
// },
// 'to': token,
// },
// ).then((f){print(f.data.toString());});
JSONObject request = new JSONObject();
HashMap<String, Object> notification = new HashMap<>();
HashMap<String, Object> data = new HashMap<>();
notification.put("body",message);
notification.put("title",title);
data.put("click_action","FLUTTER_NOTIFICATION_CLICK");
data.put("id",id++);
data.put("status","done");
request.put("notification",notification);
request.put("data",data);
request.put("priority","high");
request.put("to",token);
String postRequest = sendPOSTRequest(request);
return postRequest != null;
}
private String sendPOSTRequest(JSONObject data) {
RestTemplate restTemplate = new RestTemplate();
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
headers.setBasicAuth("key="+serverToken);
HttpEntity entity = new HttpEntity<>(data, headers);
String answer = restTemplate.postForObject(firebaseUrl, entity, String.class);
System.out.println(answer);
return answer;
}
}
......@@ -226,6 +226,15 @@ public class TbCfUserInfo {
this.slogan = slogan;
}
public boolean hasFcm(){
return fcm != null;
}
public String getFcm(){
return fcm;
}
public String getSlogan(){
return slogan;
}
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论