@Override public void onReceiveNotification(ApptentiveNotification notification) { checkConversationQueue(); if (notification.hasName(NOTIFICATION_INTERACTIONS_SHOULD_DISMISS)) { dispatchOnMainQueue(new DispatchTask() { @Override protected void execute() { dismissActivity(); } }); } else if (notification.hasName(NOTIFICATION_CONVERSATION_STATE_DID_CHANGE)) { final Conversation conversation = notification.getUserInfo(NOTIFICATION_KEY_CONVERSATION, Conversation.class); Assert.assertNotNull(conversation, "Conversation expected to be not null"); if (conversation != null && !conversation.hasActiveState()) { dispatchOnMainQueue(new DispatchTask() { @Override protected void execute() { dismissActivity(); } }); } } }
@Override public void onReceiveNotification(ApptentiveNotification notification) { addResult(String.format("%s: %s {%s}", name, notification.getName(), StringUtils.toString(notification.getUserInfo()))); } }
@Override public void onReceiveNotification(ApptentiveNotification notification) { checkConversationQueue(); if (notification.hasName(NOTIFICATION_ACTIVITY_STARTED) || notification.hasName(NOTIFICATION_ACTIVITY_RESUMED)) { final Activity activity = notification.getRequiredUserInfo(NOTIFICATION_KEY_ACTIVITY, Activity.class); setCurrentForegroundActivity(activity); } else if (notification.hasName(NOTIFICATION_APP_ENTERED_FOREGROUND)) { appWentToForeground(); } else if (notification.hasName(NOTIFICATION_APP_ENTERED_BACKGROUND)) { setCurrentForegroundActivity(null); appWentToBackground(); } else if (notification.hasName(NOTIFICATION_PAYLOAD_WILL_START_SEND)) { final PayloadData payload = notification.getRequiredUserInfo(NOTIFICATION_KEY_PAYLOAD, PayloadData.class); if (payload.getType().equals(PayloadType.message)) { resumeSending(); } } else if (notification.hasName(NOTIFICATION_PAYLOAD_DID_FINISH_SEND)) { final boolean successful = notification.getRequiredUserInfo(NOTIFICATION_KEY_SUCCESSFUL, Boolean.class); final PayloadData payload = notification.getRequiredUserInfo(NOTIFICATION_KEY_PAYLOAD, PayloadData.class); final Integer responseCode = notification.getRequiredUserInfo(NOTIFICATION_KEY_RESPONSE_CODE, Integer.class); final JSONObject responseData = successful ? notification.getRequiredUserInfo(NOTIFICATION_KEY_RESPONSE_DATA, JSONObject.class) : null; if (responseCode == -1) { pauseSending(SEND_PAUSE_REASON_NETWORK); } if (payload.getType().equals(PayloadType.message)) { onSentMessage(payload.getNonce(), responseCode, responseData); } } }
/** * Creates a notification with a given name and user info and posts it to the receiver. */ public synchronized void postNotification(final String name, final Map<String, Object> userInfo) { ApptentiveLog.v(NOTIFICATIONS, "Post notification: name=%s userInfo={%s}", name, StringUtils.toString(userInfo)); final ApptentiveNotificationObserverList list = findObserverList(name); if (list != null) { list.notifyObservers(new ApptentiveNotification(name, userInfo)); } }
checkConversationQueue(); if (notification.hasName(NOTIFICATION_CONVERSATION_STATE_DID_CHANGE)) { Conversation conversation = notification.getRequiredUserInfo(NOTIFICATION_KEY_CONVERSATION, Conversation.class); if (conversation.hasActiveState()) { updateConversationAdvertiserIdentifier(conversation); } else if (notification.hasName(NOTIFICATION_CONVERSATION_WILL_LOGOUT)) { Conversation conversation = notification.getRequiredUserInfo(NOTIFICATION_KEY_CONVERSATION, Conversation.class); conversation.addPayload(new LogoutPayload()); } else if (notification.hasName(NOTIFICATION_AUTHENTICATION_FAILED)) { String conversationIdOfFailedRequest = notification.getUserInfo(NOTIFICATION_KEY_CONVERSATION_ID, String.class); Apptentive.AuthenticationFailedReason authenticationFailedReason = notification.getUserInfo(NOTIFICATION_KEY_AUTHENTICATION_FAILED_REASON, Apptentive.AuthenticationFailedReason.class); notifyAuthenticationFailedListener(authenticationFailedReason, conversationIdOfFailedRequest); } else if (notification.hasName(NOTIFICATION_INTERACTION_MANIFEST_FETCHED)) { String manifest = notification.getRequiredUserInfo(NOTIFICATION_KEY_MANIFEST, String.class); storeManifestResponse(appContext, manifest); } else if (notification.hasName(NOTIFICATION_APP_ENTERED_FOREGROUND)) { onAppEnterForeground(); } else if (notification.hasName(NOTIFICATION_APP_ENTERED_BACKGROUND)) { onAppEnterBackground(); } else if (notification.hasName(NOTIFICATION_CONFIGURATION_FETCH_DID_FINISH)) { Configuration configuration = notification.getUserInfo(NOTIFICATION_KEY_CONFIGURATION, Configuration.class); if (configuration == null) { return;
public <T> T getRequiredUserInfo(String key, Class<? extends T> valueClass) { final T userInfo = getUserInfo(key, valueClass); // TODO: Why was this assert here? Not all requests will have response data. //Assert.assertNotNull(userInfo, "Missing required user info '%s' for '%s' notification", key, name); return userInfo; }
@Test public void testRemoveObservers() { ApptentiveNotificationObserverList list = new ApptentiveNotificationObserverList(); Observer o1 = new Observer("observer1"); Observer o2 = new Observer("observer2"); list.addObserver(o1, WEAK_REFERENCE); list.addObserver(o2, STRONG_REFERENCE); list.notifyObservers(new ApptentiveNotification("notification", new HashMap<String, Object>())); assertResult("observer1", "observer2"); assertEquals(2, list.size()); list.removeObserver(o1); assertEquals(1, list.size()); list.notifyObservers(new ApptentiveNotification("notification", new HashMap<String, Object>())); assertResult("observer2"); list.removeObserver(o2); list.notifyObservers(new ApptentiveNotification("notification", new HashMap<String, Object>())); assertResult(); assertEquals(0, list.size()); }
@Test public void testWeakReferences() { ApptentiveNotificationObserverList list = new ApptentiveNotificationObserverList(); // begin of the scope { Observer o1 = new Observer("observer1"); Observer o4 = new Observer("observer4"); list.addObserver(o1, WEAK_REFERENCE); // this reference won't be lost until the end of the current scope list.addObserver(new Observer("observer2"), WEAK_REFERENCE); // this reference would be lost right away list.addObserver(new Observer("observer3"), STRONG_REFERENCE); // this reference won't be lost list.addObserver(o4, STRONG_REFERENCE); // force GC so the weak reference becomes null System.gc(); list.notifyObservers(new ApptentiveNotification("notification", new HashMap<String, Object>())); assertResult("observer1", "observer3", "observer4"); assertEquals(3, list.size()); o1 = o4 = null; // this step is necessary for a proper GC } // end of the scope // force GC so the weak reference becomes null System.gc(); list.notifyObservers(new ApptentiveNotification("notification", new HashMap<String, Object>())); assertResult("observer3", "observer4"); assertEquals(2, list.size()); }
checkConversationQueue(); if (notification.hasName(NOTIFICATION_CONVERSATION_STATE_DID_CHANGE)) { final Conversation conversation = notification.getUserInfo(NOTIFICATION_KEY_CONVERSATION, Conversation.class); } else if (notification.hasName(NOTIFICATION_APP_ENTERED_FOREGROUND)) { appInBackground = false; } else if (notification.hasName(NOTIFICATION_APP_ENTERED_BACKGROUND)) { appInBackground = true;
@Test public void testThrowingException() { final ApptentiveNotificationObserverList list = new ApptentiveNotificationObserverList(); final Observer o1 = new Observer("observer1"); final Observer o2 = new Observer("observer2"); list.addObserver(o1, STRONG_REFERENCE); list.addObserver(new ApptentiveNotificationObserver() { @Override public void onReceiveNotification(ApptentiveNotification notification) { addResult("error"); throw new RuntimeException("Error"); } }, STRONG_REFERENCE); list.addObserver(o2, STRONG_REFERENCE); list.notifyObservers(new ApptentiveNotification("notification", new HashMap<String, Object>())); assertResult("observer1", "error", "observer2"); }
@Test public void testConcurrentModification() { final ApptentiveNotificationObserverList list = new ApptentiveNotificationObserverList(); final Observer o1 = new Observer("observer1"); final Observer o2 = new Observer("observer2"); list.addObserver(new ApptentiveNotificationObserver() { @Override public void onReceiveNotification(ApptentiveNotification notification) { list.removeObserver(o1); addResult("anonymous-observer1"); } }, STRONG_REFERENCE); list.addObserver(o1, WEAK_REFERENCE); list.addObserver(new ApptentiveNotificationObserver() { @Override public void onReceiveNotification(ApptentiveNotification notification) { list.removeObserver(o2); addResult("anonymous-observer2"); } }, STRONG_REFERENCE); list.addObserver(o2, STRONG_REFERENCE); list.notifyObservers(new ApptentiveNotification("notification", new HashMap<String, Object>())); assertResult("anonymous-observer1", "observer1", "anonymous-observer2", "observer2"); assertEquals(2, list.size()); list.notifyObservers(new ApptentiveNotification("notification", new HashMap<String, Object>())); assertResult("anonymous-observer1", "anonymous-observer2"); assertEquals(2, list.size()); }
@Test public void testAddObservers() { ApptentiveNotificationObserverList list = new ApptentiveNotificationObserverList(); Observer o1 = new Observer("observer1"); Observer o2 = new Observer("observer2"); list.addObserver(o1, WEAK_REFERENCE); list.addObserver(o2, STRONG_REFERENCE); assertEquals(2, list.size()); // trying to add duplicates list.addObserver(o1, WEAK_REFERENCE); list.addObserver(o1, STRONG_REFERENCE); assertEquals(2, list.size()); list.addObserver(o2, WEAK_REFERENCE); list.addObserver(o2, STRONG_REFERENCE); assertEquals(2, list.size()); list.notifyObservers(new ApptentiveNotification("notification", new HashMap<String, Object>())); assertResult("observer1", "observer2"); }