Skip to content

Commit

Permalink
ADM Sender logic
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastienblanc committed Jan 29, 2015
1 parent c468930 commit 16881b9
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 1 deletion.
Expand Up @@ -44,6 +44,8 @@ public class Message {
@JsonProperty("simple-push")
private String simplePush;

private String consolidationKey;

/**
* Returns the value of the 'action-category', which is used on the client (iOS for now),
* to invoke a certain "user action" on the device, based on the push message. Implemented for iOS8
Expand Down Expand Up @@ -144,6 +146,20 @@ public void setSimplePush(String simplePush) {
this.simplePush = simplePush;
}

/**
* Used for ADM Payload when used for "sync" Push messages.
* Not supported on other platforms.
*
* @return the consolidation key
*/
public String getConsolidationKey() {
return consolidationKey;
}

public void setConsolidationKey(String consolidationKey) {
this.consolidationKey = consolidationKey;
}

@Override
public String toString() {
return "Message{" +
Expand All @@ -152,6 +168,7 @@ public String toString() {
", sound='" + sound + '\'' +
", contentAvailable=" + contentAvailable +
", badge=" + badge +
", consolidationKey=" + consolidationKey +
", user-data=" + userData +
", simple-push='" + simplePush + '\'' +
'}';
Expand Down
Expand Up @@ -534,6 +534,7 @@ public void testMessageToJson() throws IOException {
"\"alert\":\"Howdy\"," +
"\"sound\":\"default\"," +
"\"badge\":2," +
"\"consolidationKey\":null," +
"\"action-category\":null," +
"\"content-available\":false," +
"\"user-data\":{" +
Expand Down
3 changes: 2 additions & 1 deletion push/model/src/test/resources/message-format.json
Expand Up @@ -11,7 +11,8 @@
},
"action-category":"some value",
"content-available":true,
"simple-push":"version=123"
"simple-push":"version=123",
"consolidationKey":null
},
"criteria":{
"categories":[
Expand Down
6 changes: 6 additions & 0 deletions push/sender/pom.xml
Expand Up @@ -112,6 +112,12 @@
<version>0.1.0</version>
</dependency>

<dependency>
<groupId>org.jboss.aerogear</groupId>
<artifactId>java-adm</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>

</dependencies>

<profiles>
Expand Down
@@ -0,0 +1,63 @@
package org.jboss.aerogear.unifiedpush.message.sender;


import org.jboss.aerogear.unifiedpush.api.AdmVariant;
import org.jboss.aerogear.unifiedpush.api.Variant;

import org.jboss.aerogear.unifiedpush.message.UnifiedPushMessage;
import org.jboss.aerogear.unifiedpush.utils.AeroGearLogger;
import org.jboss.aerogear.adm.ADM;
import org.jboss.aerogear.adm.AdmService;
import org.jboss.aerogear.adm.PayloadBuilder;

import java.util.Collection;
import java.util.Set;

@SenderType(AdmVariant.class)
public class AdmPushNotificationSender implements PushNotificationSender {
private final AeroGearLogger logger = AeroGearLogger.getInstance(AdmPushNotificationSender.class);

@Override
public void sendPushMessage(Variant variant, Collection<String> clientIdentifiers, UnifiedPushMessage pushMessage, NotificationSenderCallback senderCallback) {
AdmService admService = ADM.newService();

PayloadBuilder builder = ADM.newPayload();

//flatten the "special keys"
builder.dataField("alert", pushMessage.getMessage().getAlert());
builder.dataField("sound", pushMessage.getMessage().getSound());
builder.dataField("badge", "" + pushMessage.getMessage().getBadge());

// if present, apply the time-to-live metadata:
int ttl = pushMessage.getConfig().getTimeToLive();
if (ttl != -1) {
builder.expiresAfter(ttl);
}

//dirty hack for cordova,
//TODO should be removed once we have our clients SDKs
builder.dataField("message","useless payload");

//Handle consolidation key
builder.consolidationKey("my other app");

// iterate over the missing keys:
Set<String> keys = pushMessage.getMessage().getUserData().keySet();
for (String key : keys) {
builder.dataField(key, pushMessage.getMessage().getUserData().get(key));
}
System.out.println(builder.build());


final AdmVariant admVariant = (AdmVariant) variant;
for(String token : clientIdentifiers) {
try {
admService.sendMessageToDevice(token, admVariant.getClientId(), admVariant.getClientSecret(), builder.build());
senderCallback.onSuccess();
} catch (Exception e) {
logger.severe("Error sending payload to ADM server", e);
senderCallback.onError(e.getMessage());
}
}
}
}

0 comments on commit 16881b9

Please sign in to comment.