PushManager extends AirshipComponent { @Override protected void init() { super.init(); this.migratePushEnabledSettings(); airshipChannel.addChannelRegistrationPayloadExtender(new AirshipChannel.ChannelRegistrationPayloadExtender() { @NonNull @Override public ChannelRegistrationPayload.Builder extend(@NonNull ChannelRegistrationPayload.Builder builder) { return extendChannelRegistrationPayload(builder); } }); analytics.addHeaderDelegate(new Analytics.AnalyticsHeaderDelegate() { @NonNull @Override public Map<String, String> onCreateAnalyticsHeaders() { return createAnalyticsHeaders(); } }); notificationChannelRegistry.createDeferredNotificationChannels(R.xml.ua_default_channels); String pushDeliveryType = preferenceDataStore.getString(PUSH_DELIVERY_TYPE, null); if (pushProvider == null) { preferenceDataStore.remove(PUSH_DELIVERY_TYPE); preferenceDataStore.remove(PUSH_TOKEN_KEY); } else if (!pushProvider.getDeliveryType().equals(pushDeliveryType)) { preferenceDataStore.remove(PUSH_TOKEN_KEY); preferenceDataStore.put(PUSH_DELIVERY_TYPE, pushProvider.getDeliveryType()); } dispatchUpdatePushTokenJob(); } PushManager(@NonNull Context context, @NonNull PreferenceDataStore preferenceDataStore,
                       @NonNull AirshipConfigOptions configOptions, @Nullable PushProvider pushProvider,
                       @NonNull AirshipChannel airshipChannel, @NonNull Analytics analytics); @VisibleForTesting  PushManager(@NonNull Context context, @NonNull PreferenceDataStore preferenceDataStore,
                @NonNull AirshipConfigOptions configOptions, PushProvider provider,
                @NonNull AirshipChannel airshipChannel, @NonNull Analytics analytics,
                @NonNull JobDispatcher dispatcher); @Override @RestrictTo(RestrictTo.Scope.LIBRARY_GROUP) @AirshipComponentGroups.Group int getComponentGroup(); @Override @RestrictTo(RestrictTo.Scope.LIBRARY_GROUP) void onComponentEnableChange(boolean isEnabled); @WorkerThread @JobInfo.JobResult @RestrictTo(RestrictTo.Scope.LIBRARY_GROUP) int onPerformJob(@NonNull UAirship airship, @NonNull JobInfo jobInfo); void setPushEnabled(boolean enabled); boolean isPushEnabled(); void setUserNotificationsEnabled(boolean enabled); boolean getUserNotificationsEnabled(); void setNotificationProvider(@Nullable NotificationProvider provider); @Nullable NotificationProvider getNotificationProvider(); @NonNull NotificationChannelRegistry getNotificationChannelRegistry(); @Deprecated boolean isSoundEnabled(); @Deprecated void setSoundEnabled(boolean enabled); @Deprecated boolean isVibrateEnabled(); @Deprecated void setVibrateEnabled(boolean enabled); @Deprecated boolean isQuietTimeEnabled(); @Deprecated void setQuietTimeEnabled(boolean enabled); @Deprecated boolean isInQuietTime(); @Deprecated @Nullable Date[] getQuietTimeInterval(); @Deprecated void setQuietTimeInterval(@NonNull Date startTime, @NonNull Date endTime); boolean isPushAvailable(); boolean isOptIn(); boolean areNotificationsOptedIn(); boolean isPushTokenRegistrationEnabled(); void setPushTokenRegistrationEnabled(boolean enabled); @Nullable String getLastReceivedMetadata(); void setNotificationListener(@Nullable NotificationListener listener); void addPushListener(@NonNull PushListener listener); void removePushListener(@NonNull PushListener listener); void addPushTokenListener(@NonNull PushTokenListener listener); void removePushTokenListener(@NonNull PushTokenListener listener); @RestrictTo(RestrictTo.Scope.LIBRARY_GROUP) void addInternalNotificationListener(@NonNull InternalNotificationListener listener); @Nullable NotificationListener getNotificationListener(); void addNotificationActionButtonGroup(@NonNull String id, @NonNull NotificationActionButtonGroup group); void addNotificationActionButtonGroups(@NonNull Context context, @XmlRes int resId); void removeNotificationActionButtonGroup(@NonNull String id); @Nullable NotificationActionButtonGroup getNotificationActionGroup(@Nullable String id); @Nullable String getPushToken(); @Nullable @RestrictTo(RestrictTo.Scope.LIBRARY_GROUP) PushProvider getPushProvider(); @RestrictTo(RestrictTo.Scope.LIBRARY_GROUP)
    @NonNull
    static final String ACTION_NOTIFICATION_RESPONSE; @RestrictTo(RestrictTo.Scope.LIBRARY_GROUP)
    @NonNull
    static final String ACTION_NOTIFICATION_DISMISSED; @RestrictTo(RestrictTo.Scope.LIBRARY_GROUP)
    @NonNull
    static final String EXTRA_NOTIFICATION_ID; @RestrictTo(RestrictTo.Scope.LIBRARY_GROUP)
    @NonNull
    static final String EXTRA_NOTIFICATION_TAG; @RestrictTo(RestrictTo.Scope.LIBRARY_GROUP)
    @NonNull
    static final String EXTRA_PUSH_MESSAGE_BUNDLE; @RestrictTo(RestrictTo.Scope.LIBRARY_GROUP)
    @NonNull
    static final String EXTRA_NOTIFICATION_BUTTON_ID; @RestrictTo(RestrictTo.Scope.LIBRARY_GROUP)
    @NonNull
    static final String EXTRA_NOTIFICATION_BUTTON_FOREGROUND; @RestrictTo(RestrictTo.Scope.LIBRARY_GROUP)
    @NonNull
    static final String EXTRA_NOTIFICATION_CONTENT_INTENT; @RestrictTo(RestrictTo.Scope.LIBRARY_GROUP)
    @NonNull
    static final String EXTRA_NOTIFICATION_DELETE_INTENT; @RestrictTo(RestrictTo.Scope.LIBRARY_GROUP)
    @NonNull
    static final String EXTRA_NOTIFICATION_ACTION_BUTTON_DESCRIPTION; @RestrictTo(RestrictTo.Scope.LIBRARY_GROUP)
    @NonNull
    static final String EXTRA_NOTIFICATION_BUTTON_ACTIONS_PAYLOAD; }
@Test public void testInit() { pushManager.init(); verify(mockDispatcher).dispatch(Mockito.argThat(new ArgumentMatcher<JobInfo>() { @Override public boolean matches(JobInfo jobInfo) { return jobInfo.getAction().equals(PushManager.ACTION_UPDATE_PUSH_REGISTRATION); } })); verifyNoMoreInteractions(mockDispatcher); }
@Test public void testChannelRegistrationExtenderOptedOut() { ArgumentCaptor<AirshipChannel.ChannelRegistrationPayloadExtender> argument = ArgumentCaptor.forClass(AirshipChannel.ChannelRegistrationPayloadExtender.class); pushManager.init(); verify(mockAirshipChannel).addChannelRegistrationPayloadExtender(argument.capture()); AirshipChannel.ChannelRegistrationPayloadExtender extender = argument.getValue(); assertNotNull(extender); ChannelRegistrationPayload.Builder builder = new ChannelRegistrationPayload.Builder(); ChannelRegistrationPayload payload = extender.extend(builder).build(); ChannelRegistrationPayload expected = new ChannelRegistrationPayload.Builder() .setBackgroundEnabled(false) .setOptIn(false) .build(); assertEquals(expected, payload); }
@Test public void testDeliveryTypeAndroidPlatform() throws PushProvider.RegistrationException { ArgumentCaptor<AirshipChannel.ChannelRegistrationPayloadExtender> argument = ArgumentCaptor.forClass(AirshipChannel.ChannelRegistrationPayloadExtender.class); pushManager.init(); verify(mockAirshipChannel).addChannelRegistrationPayloadExtender(argument.capture()); AirshipChannel.ChannelRegistrationPayloadExtender extender = argument.getValue(); assertNotNull(extender); when(mockPushProvider.isAvailable(any(Context.class))).thenReturn(true); when(mockPushProvider.getPlatform()).thenReturn(UAirship.ANDROID_PLATFORM); when(mockPushProvider.getDeliveryType()).thenReturn(PushProvider.FCM_DELIVERY_TYPE); when(mockPushProvider.getRegistrationToken(any(Context.class))).thenReturn("token"); ChannelRegistrationPayload.Builder builder = new ChannelRegistrationPayload.Builder(); ChannelRegistrationPayload payload = extender.extend(builder).build(); ChannelRegistrationPayload expected = new ChannelRegistrationPayload.Builder() .setPushAddress("token") .setDeliveryType(PushProvider.FCM_DELIVERY_TYPE) .setBackgroundEnabled(true) .build(); assertEquals(expected, payload); }
@Test public void testAnalyticHeaders() { ArgumentCaptor<Analytics.AnalyticsHeaderDelegate> captor = ArgumentCaptor.forClass(Analytics.AnalyticsHeaderDelegate.class); pushManager.init(); verify(mockAnalytics).addHeaderDelegate(captor.capture()); Analytics.AnalyticsHeaderDelegate delegate = captor.getValue(); assertNotNull(delegate); Map<String, String> expectedHeaders = new HashMap<>(); expectedHeaders.put("X-UA-Channel-Opted-In", "false"); expectedHeaders.put("X-UA-Channel-Background-Enabled", "false"); Map<String, String> headers = delegate.onCreateAnalyticsHeaders(); assertEquals(expectedHeaders, headers); }