usergrid-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mru...@apache.org
Subject usergrid git commit: Fix push notification delivery expiration.
Date Thu, 10 Sep 2015 22:44:16 GMT
Repository: usergrid
Updated Branches:
  refs/heads/two-dot-o-dev 3bd059c66 -> 1efd17632


Fix push notification delivery expiration.


Project: http://git-wip-us.apache.org/repos/asf/usergrid/repo
Commit: http://git-wip-us.apache.org/repos/asf/usergrid/commit/1efd1763
Tree: http://git-wip-us.apache.org/repos/asf/usergrid/tree/1efd1763
Diff: http://git-wip-us.apache.org/repos/asf/usergrid/diff/1efd1763

Branch: refs/heads/two-dot-o-dev
Commit: 1efd176325a147c82077c694bccda5af917fa23b
Parents: 3bd059c
Author: Michael Russo <michaelarusso@gmail.com>
Authored: Thu Sep 10 15:44:08 2015 -0700
Committer: Michael Russo <michaelarusso@gmail.com>
Committed: Thu Sep 10 15:44:08 2015 -0700

----------------------------------------------------------------------
 .../usergrid/persistence/entities/Notification.java    | 12 ++++++++----
 .../services/notifications/apns/APNsNotification.java  |  8 ++++----
 .../services/notifications/gcm/GCMAdapter.java         |  9 +++++----
 .../services/notifications/wns/WNSAdapter.java         | 13 ++++++++++---
 .../notifications/apns/NotificationsServiceIT.java     |  1 +
 .../notifications/gcm/NotificationsServiceIT.java      |  1 +
 6 files changed, 29 insertions(+), 15 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/usergrid/blob/1efd1763/stack/core/src/main/java/org/apache/usergrid/persistence/entities/Notification.java
----------------------------------------------------------------------
diff --git a/stack/core/src/main/java/org/apache/usergrid/persistence/entities/Notification.java
b/stack/core/src/main/java/org/apache/usergrid/persistence/entities/Notification.java
index 88b2f33..d1101a5 100644
--- a/stack/core/src/main/java/org/apache/usergrid/persistence/entities/Notification.java
+++ b/stack/core/src/main/java/org/apache/usergrid/persistence/entities/Notification.java
@@ -230,12 +230,16 @@ public class Notification extends TypedEntity {
         return State.CREATED;
     }
 
+    @JsonIgnore
+    public long getExpireTimeMillis() {
+        long expireAtMillis = getExpire() != null ? getExpire() * 1000 : 0;
+        return expireAtMillis;
+    }
 
     @JsonIgnore
-    public int getExpireTimeInSeconds() {
-        long expirySeconds = getExpire() != null ? getExpire() * 1000 : 0;
-        return (expirySeconds > Integer.MAX_VALUE) ? Integer.MAX_VALUE
-                : (int) expirySeconds;
+    public long getExpireTTLSeconds() {
+        Long ttlSeconds = (getExpireTimeMillis() - new Date().getTime()) / 1000;
+        return ttlSeconds > 0 ? ttlSeconds : 0;
     }
 
     @JsonSerialize(include = JsonSerialize.Inclusion.NON_NULL)

http://git-wip-us.apache.org/repos/asf/usergrid/blob/1efd1763/stack/services/src/main/java/org/apache/usergrid/services/notifications/apns/APNsNotification.java
----------------------------------------------------------------------
diff --git a/stack/services/src/main/java/org/apache/usergrid/services/notifications/apns/APNsNotification.java
b/stack/services/src/main/java/org/apache/usergrid/services/notifications/apns/APNsNotification.java
index b580410..ff00e4c 100644
--- a/stack/services/src/main/java/org/apache/usergrid/services/notifications/apns/APNsNotification.java
+++ b/stack/services/src/main/java/org/apache/usergrid/services/notifications/apns/APNsNotification.java
@@ -24,7 +24,6 @@ import com.relayrides.pushy.apns.util.TokenUtil;
 import org.apache.usergrid.persistence.entities.Notification;
 import org.apache.usergrid.services.notifications.TaskTracker;
 
-import java.util.Calendar;
 import java.util.Date;
 /**
  * Standard apigee notificatton
@@ -44,12 +43,13 @@ public class APNsNotification extends SimpleApnsPushNotification {
      */
     public static APNsNotification create(String providerId, String payload, Notification
notification, TaskTracker tracker) throws RuntimeException {
 
-        Calendar date  = Calendar.getInstance();
-        date.add(Calendar.SECOND, notification.getExpireTimeInSeconds());
+        // create Date object using milliseconds value
+        Date expiryDate = new Date(notification.getExpireTimeMillis());
+
       try {
           final byte[] token = TokenUtil.tokenStringToByteArray(providerId);
 
-          return new APNsNotification(tracker, date.getTime(), token, payload, notification);
+          return new APNsNotification(tracker, expiryDate, token, payload, notification);
       }catch(MalformedTokenStringException mtse){
           throw new RuntimeException("Exception converting token",mtse);
       }

http://git-wip-us.apache.org/repos/asf/usergrid/blob/1efd1763/stack/services/src/main/java/org/apache/usergrid/services/notifications/gcm/GCMAdapter.java
----------------------------------------------------------------------
diff --git a/stack/services/src/main/java/org/apache/usergrid/services/notifications/gcm/GCMAdapter.java
b/stack/services/src/main/java/org/apache/usergrid/services/notifications/gcm/GCMAdapter.java
index 5580ea8..df1b580 100644
--- a/stack/services/src/main/java/org/apache/usergrid/services/notifications/gcm/GCMAdapter.java
+++ b/stack/services/src/main/java/org/apache/usergrid/services/notifications/gcm/GCMAdapter.java
@@ -36,7 +36,6 @@ import org.apache.usergrid.services.notifications.TaskTracker;
 import java.io.IOException;
 import java.util.*;
 import java.util.concurrent.ConcurrentHashMap;
-import java.util.concurrent.ConcurrentLinkedQueue;
 
 public class GCMAdapter implements ProviderAdapter {
 
@@ -71,9 +70,11 @@ public class GCMAdapter implements ProviderAdapter {
         Map<String,Object> map = (Map<String, Object>) payload;
         final String expiresKey = "time_to_live";
         if(!map.containsKey(expiresKey) && notification.getExpire() != null){
-            int expireSeconds = notification.getExpireTimeInSeconds();
-            expireSeconds = expireSeconds <= 2419200 ? expireSeconds : 2419200; //send
the max gcm value documented here http://developer.android.com/google/gcm/adv.html#ttl
-            map.put(expiresKey, expireSeconds);
+            // ttl provided to GCM is in seconds.  calculate the difference from now
+            Long ttlSeconds = notification.getExpireTTLSeconds();
+            // max ttl for gcm is 4 weeks - https://developers.google.com/cloud-messaging/http-server-ref
+            ttlSeconds = ttlSeconds <= 2419200 ? ttlSeconds : 2419200;
+            map.put(expiresKey, ttlSeconds);
         }
         Batch batch = getBatch( map);
         batch.add(providerId, tracker);

http://git-wip-us.apache.org/repos/asf/usergrid/blob/1efd1763/stack/services/src/main/java/org/apache/usergrid/services/notifications/wns/WNSAdapter.java
----------------------------------------------------------------------
diff --git a/stack/services/src/main/java/org/apache/usergrid/services/notifications/wns/WNSAdapter.java
b/stack/services/src/main/java/org/apache/usergrid/services/notifications/wns/WNSAdapter.java
index 1fe8f72..3ad3d35 100644
--- a/stack/services/src/main/java/org/apache/usergrid/services/notifications/wns/WNSAdapter.java
+++ b/stack/services/src/main/java/org/apache/usergrid/services/notifications/wns/WNSAdapter.java
@@ -74,10 +74,15 @@ public class WNSAdapter implements ProviderAdapter {
         try {
             List<TranslatedNotification> translatedNotifications = ( List<TranslatedNotification>)
payload;
             for(TranslatedNotification translatedNotification : translatedNotifications)
{
+
+                // set the optional TTL value used when pushing notifications
+                WnsNotificationRequestOptional opt = new WnsNotificationRequestOptional();
+                opt.ttl = String.valueOf(notification.getExpireTTLSeconds());
+
                 switch (translatedNotification.getType()) {
                     case "toast":
                         WnsToast toast = new WnsToastBuilder().bindingTemplateToastText01(translatedNotification.getMessage().toString()).build();
-                        service.pushToast(providerId, toast);
+                        service.pushToast(providerId, opt, toast);
                         break;
                     case "badge":
                         WnsBadge badge;
@@ -86,15 +91,17 @@ public class WNSAdapter implements ProviderAdapter {
                         } else {
                             badge = new WnsBadgeBuilder().value(translatedNotification.getMessage().toString()).build();
                         }
-                        service.pushBadge(providerId, badge);
+                        service.pushBadge(providerId, opt, badge);
                         break;
                     case "raw":
                         Object message = translatedNotification.getMessage();
                         if(message instanceof String) {
                             WnsRaw raw = new WnsRawBuilder().stream(((String) message).getBytes()).build();
-                            WnsNotificationRequestOptional opt = new WnsNotificationRequestOptional();
+
+                            // set additional optional parameter for raw notifications
                             opt.cachePolicy = "cache";
                             opt.requestForStatus = "true";
+
                             WnsNotificationResponse response = service.pushRaw(providerId,
opt, raw);
                             if (!response.notificationStatus.equals("received")) { // https://msdn.microsoft.com/en-us/library/windows/apps/hh465435.aspx#pncodes_x_wns_notification
                                 throw new Exception(String.format("Notification failed status:%s,
devicesStatus:%s, description:%s, debug flag:%s", response.notificationStatus, response.deviceConnectionStatus,
response.errorDescription, response.debugTrace));

http://git-wip-us.apache.org/repos/asf/usergrid/blob/1efd1763/stack/services/src/test/java/org/apache/usergrid/services/notifications/apns/NotificationsServiceIT.java
----------------------------------------------------------------------
diff --git a/stack/services/src/test/java/org/apache/usergrid/services/notifications/apns/NotificationsServiceIT.java
b/stack/services/src/test/java/org/apache/usergrid/services/notifications/apns/NotificationsServiceIT.java
index fc80fdf..bd248a5 100644
--- a/stack/services/src/test/java/org/apache/usergrid/services/notifications/apns/NotificationsServiceIT.java
+++ b/stack/services/src/test/java/org/apache/usergrid/services/notifications/apns/NotificationsServiceIT.java
@@ -153,6 +153,7 @@ public class NotificationsServiceIT extends AbstractServiceNotificationIT
{
         app.put("payloads", payloads);
         app.put("queued", System.currentTimeMillis());
         app.put("debug",true);
+        app.put("expire", (System.currentTimeMillis() / 1000) + 300); // add 5 minutes to
current time
 
         // post notification to service manager
         Entity e = app.testRequest(ServiceAction.POST, 1,"devices",device1.getUuid(), "notifications").getEntity();

http://git-wip-us.apache.org/repos/asf/usergrid/blob/1efd1763/stack/services/src/test/java/org/apache/usergrid/services/notifications/gcm/NotificationsServiceIT.java
----------------------------------------------------------------------
diff --git a/stack/services/src/test/java/org/apache/usergrid/services/notifications/gcm/NotificationsServiceIT.java
b/stack/services/src/test/java/org/apache/usergrid/services/notifications/gcm/NotificationsServiceIT.java
index 63869ca..38a6f2e 100644
--- a/stack/services/src/test/java/org/apache/usergrid/services/notifications/gcm/NotificationsServiceIT.java
+++ b/stack/services/src/test/java/org/apache/usergrid/services/notifications/gcm/NotificationsServiceIT.java
@@ -153,6 +153,7 @@ public class NotificationsServiceIT extends AbstractServiceNotificationIT
{
         app.put("payloads", payloads);
         app.put("queued", System.currentTimeMillis());
         app.put("debug",true);
+        app.put("expire", (System.currentTimeMillis() / 1000) + 300); // add 5 minutes to
current time
 
         Entity e = app.testRequest(ServiceAction.POST, 1, "devices",device1.getUuid(),"notifications").getEntity();
         app.testRequest(ServiceAction.GET, 1, "notifications", e.getUuid());


Mime
View raw message