Subversion Repositories SmartDukaan

Rev

Rev 37499 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 37499 Rev 37569
Line 2777... Line 2777...
2777
        InternetAddress senderAddress = new InternetAddress("sdtech@smartdukaan.com", "Smart Dukaan");
2777
        InternetAddress senderAddress = new InternetAddress("sdtech@smartdukaan.com", "Smart Dukaan");
2778
        helper.setFrom(senderAddress);
2778
        helper.setFrom(senderAddress);
2779
        sender.send(message);
2779
        sender.send(message);
2780
    }
2780
    }
2781
 
2781
 
2782
    public void sendNotification() throws Exception {
-
 
2783
        LOGGER.info("FCM sendNotification");
-
 
2784
        List<PushNotifications> pushNotifications = pushNotificationRepository.selectAllPendingNotifications();
-
 
2785
        if (!pushNotifications.isEmpty()) {
2782
    @Autowired
2786
            InputStream serviceAccount = getClass().getClassLoader().getResourceAsStream("service-account.json");
-
 
2787
            GoogleCredentials googleCredentials = GoogleCredentials.fromStream(serviceAccount).createScoped(FCM_SCOPED);
-
 
2788
            googleCredentials.refreshIfExpired();
-
 
2789
            String accessToken = googleCredentials.getAccessToken().getTokenValue();
-
 
2790
 
-
 
2791
            for (PushNotifications pushNotification : pushNotifications) {
2783
    private PushNotificationSendService pushNotificationSendService;
2792
                Device device = deviceRepository.selectById(pushNotification.getDeviceId());
-
 
2793
                NotificationCampaign notificationCampaign = notificationCampaignRepository.selectById(pushNotification.getNotificationCampaignid());
-
 
2794
                SimpleCampaignParams scp = gson.fromJson(notificationCampaign.getImplementationParams(), SimpleCampaignParams.class);
-
 
2795
                Campaign campaign = new SimpleCampaign(scp);
-
 
2796
                String result_url = campaign.getUrl() + "&user_id=" + device.getUser_id();
-
 
2797
 
-
 
2798
                JSONObject message = new JSONObject();
-
 
2799
                message.put("token", device.getFcmId());
-
 
2800
 
-
 
2801
                JSONObject notification = new JSONObject();
-
 
2802
                notification.put("title", campaign.getTitle());
-
 
2803
                notification.put("body", campaign.getMessage());
-
 
2804
                message.put("notification", notification);
-
 
2805
 
-
 
2806
                JSONObject data = new JSONObject();
-
 
2807
                data.put("type", campaign.getType());
-
 
2808
                data.put("url", result_url);
-
 
2809
                data.put("time_to_live", campaign.getExpireTimestamp());
-
 
2810
                data.put("image", campaign.getImageUrl());
-
 
2811
                data.put("largeIcon", "large_icon");
-
 
2812
                data.put("smallIcon", "small_icon");
-
 
2813
                data.put("vibrate", "1");
-
 
2814
                data.put("pid", String.valueOf(pushNotification.getId()));
-
 
2815
                data.put("sound", "1");
-
 
2816
                data.put("priority", "high");
-
 
2817
                message.put("data", data);
-
 
2818
 
2784
 
2819
                JSONObject payload = new JSONObject();
-
 
2820
                payload.put("message", message);
-
 
2821
 
-
 
2822
                try {
2785
    /**
2823
                    CloseableHttpClient client = HttpClientFactory.apacheHttp();
-
 
2824
                    HttpPost httpPost = new HttpPost(FCM_URL);
-
 
2825
 
-
 
2826
                    httpPost.setHeader("Authorization", "Bearer " + accessToken);
2786
     * Queued FCM pushes. The work lives in PushNotificationSendService because it must NOT run in
2827
                    httpPost.setHeader("Content-Type", "application/json");
-
 
2828
 
-
 
2829
                    StringEntity entity = new StringEntity(payload.toString());
-
 
2830
                    httpPost.setEntity(entity);
-
 
2831
                    CloseableHttpResponse response = client.execute(httpPost);
2787
     * this class's class-level transaction: it makes one network call per notification, and a
2832
                    LOGGER.info("google FCM status code: {}", response.getStatusLine().getStatusCode());
2788
     * transaction spanning the batch holds a write lock on every row for the sum of those waits.
2833
                    String responseBody = new BufferedReader(new InputStreamReader(response.getEntity().getContent()))
-
 
2834
                            .lines().collect(Collectors.joining("\n"));
-
 
2835
                    if (response.getStatusLine().getStatusCode() == 200) {
-
 
2836
                        pushNotification.setSentTimestamp(LocalDateTime.now());
-
 
2837
                    } else {
-
 
2838
                        pushNotification.setSentTimestamp(LocalDateTime.of(1970, 1, 1, 00, 00));
-
 
2839
                        LOGGER.info("FCM Response message" + responseBody);
-
 
2840
                        response.toString();
-
 
2841
                    }
2789
     */
2842
 
-
 
2843
                } catch (Exception e) {
2790
    public void sendNotification() throws Exception {
2844
                    e.printStackTrace();
2791
        pushNotificationSendService.sendPending();
2845
                    pushNotification.setSentTimestamp(LocalDateTime.of(1970, 1, 1, 00, 00));
-
 
2846
                    LOGGER.info("message " + "not sent " + e.getMessage());
-
 
2847
                }
-
 
2848
            }
-
 
2849
        }
-
 
2850
    }
2792
    }
2851
 
2793
 
2852
    public void grouping() throws Exception {
2794
    public void grouping() throws Exception {
2853
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("MM-dd-yyyy hh:mm");
2795
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("MM-dd-yyyy hh:mm");
2854
        List<PriceDropIMEI> priceDropImeis = priceDropIMEIRepository.selectByStatus(PriceDropImeiStatus.APPROVED);
2796
        List<PriceDropIMEI> priceDropImeis = priceDropIMEIRepository.selectByStatus(PriceDropImeiStatus.APPROVED);