Skip to content
This repository has been archived by the owner on Apr 17, 2023. It is now read-only.

Commit

Permalink
Merge pull request #629 from matzew/BadgeOverride
Browse files Browse the repository at this point in the history
send badge payload only when needed, to avoid a hard reset
  • Loading branch information
matzew committed Aug 13, 2015
2 parents 31acb06 + 8a232eb commit 7ac7422
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -103,14 +103,18 @@ public void sendPushMessage(final Variant variant, final Collection<String> toke
PayloadBuilder builder = APNS.newPayload()
// adding recognized key values
.alertBody(message.getAlert()) // alert dialog, in iOS or Safari
.badge(message.getBadge()) // little badge icon update;
.sound(message.getSound()) // sound to be played by app
.alertTitle(apns.getTitle()) // The title of the notification in Safari and Apple Watch
.alertAction(apns.getAction()) // The label of the action button, if the user sets the notifications to appear as alerts in Safari.
.urlArgs(apns.getUrlArgs())
.category(apns.getActionCategory()) // iOS8: User Action category
.localizedTitleKey(apns.getLocalizedTitleKey()); //iOS8 : Localized Title Key

// was a badge included?
if (message.getBadge() >= 0) {
builder.badge(message.getBadge()); // only set badge if needed
}

//this kind of check should belong in java-apns
if(apns.getLocalizedTitleArguments() != null) {
builder .localizedArguments(apns.getLocalizedTitleArguments()); //iOS8 : Localized Title Arguments;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@
package org.jboss.aerogear.unifiedpush.message.sender;


import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.anyString;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
Expand All @@ -25,12 +30,18 @@
import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import com.notnoop.apns.ApnsService;
import org.jboss.aerogear.unifiedpush.api.iOSVariant;
import org.jboss.aerogear.unifiedpush.message.UnifiedPushMessage;
import org.jboss.aerogear.unifiedpush.message.cache.AbstractServiceCache.ServiceConstructor;
import org.jboss.aerogear.unifiedpush.message.cache.ApnsServiceCache;
import org.junit.Test;
import org.mockito.ArgumentCaptor;

public class APNsPushNotificationSenderTest {

Expand All @@ -49,7 +60,69 @@ public void callbackOnError() throws Exception {
verify(callback).onError("Error sending payload to APNs server: Invalid hex character: t");
}

@Test
public void noBadge() throws Exception {
final ApnsService apnsService = mock(ApnsService.class);
final APNsPushNotificationSender sender = mockSender(apnsService);
final List<String> tokens = Collections.singletonList("token");

sender.sendPushMessage(iosVariant(), tokens, new UnifiedPushMessage(), "123", mockCallback());

final ArgumentCaptor<String> captor = ArgumentCaptor.forClass(String.class);
verify(apnsService).push(eq(tokens), captor.capture(), any(Date.class));
assertFalse("JSON Message should NOT contain a badge property.", captor.getValue().contains("\"badge\":0"));
}

@Test
public void badgeZero() throws Exception {
final ApnsService apnsService = mock(ApnsService.class);
final APNsPushNotificationSender sender = mockSender(apnsService);
final List<String> tokens = Collections.singletonList("token");
final UnifiedPushMessage unifiedPushMessage = new UnifiedPushMessage();
unifiedPushMessage.getMessage().setBadge(0);

sender.sendPushMessage(iosVariant(), tokens, unifiedPushMessage, "123", mockCallback());

final ArgumentCaptor<String> captor = ArgumentCaptor.forClass(String.class);
verify(apnsService).push(eq(tokens), captor.capture(), any(Date.class));
assertTrue("JSON Message should contain a badge property.", captor.getValue().contains("\"badge\":0"));
}

@Test
public void badgeGreaterThanZero() throws Exception {
final ApnsService apnsService = mock(ApnsService.class);
final APNsPushNotificationSender sender = mockSender(apnsService);
final List<String> tokens = Collections.singletonList("token");
final UnifiedPushMessage unifiedPushMessage = new UnifiedPushMessage();
unifiedPushMessage.getMessage().setBadge(9);

sender.sendPushMessage(iosVariant(), tokens, unifiedPushMessage, "123", mockCallback());

final ArgumentCaptor<String> captor = ArgumentCaptor.forClass(String.class);
verify(apnsService).push(eq(tokens), captor.capture(), any(Date.class));
assertTrue("JSON Message should contain a badge property with value 9: ", captor.getValue().contains("\"badge\":9"));
}

private iOSVariant iosVariant() throws Exception {
final iOSVariant iosVariant = mock(iOSVariant.class);
when(iosVariant.getVariantID()).thenReturn(UUID.randomUUID().toString());
when(iosVariant.getCertificate()).thenReturn(readCertificate());
when(iosVariant.getPassphrase()).thenReturn("123456");
return iosVariant;
}

private NotificationSenderCallback mockCallback() {
return mock(NotificationSenderCallback.class);
}

private APNsPushNotificationSender mockSender(final ApnsService apnsService) {
final ApnsServiceCache serviceCache = mock(ApnsServiceCache.class);
when(serviceCache.dequeueOrCreateNewService(anyString(),
anyString(),
any(ServiceConstructor.class))).thenReturn(apnsService);
final APNsPushNotificationSender sender = new APNsPushNotificationSender(serviceCache);
return sender;
}

/**
* The store read by this method was copied from
Expand Down

0 comments on commit 7ac7422

Please sign in to comment.