@@ -11,7 +11,6 @@ import androidx.work.OneTimeWorkRequestBuilder
1111import androidx.work.PeriodicWorkRequest
1212import androidx.work.WorkManager
1313import com.google.gson.Gson
14- import org.commcare.android.database.connect.models.PushNotificationRecord
1514import org.commcare.connect.ConnectConstants.CCC_DEST_DELIVERY_PROGRESS
1615import org.commcare.connect.ConnectConstants.CCC_DEST_LEARN_PROGRESS
1716import org.commcare.connect.ConnectConstants.CCC_DEST_OPPORTUNITY_SUMMARY_PAGE
@@ -26,7 +25,6 @@ import org.commcare.pn.workers.NotificationsSyncWorker.Companion.ACTION
2625import org.commcare.pn.workers.NotificationsSyncWorker.Companion.NOTIFICATION_PAYLOAD
2726import org.commcare.pn.workers.NotificationsSyncWorker.Companion.SyncAction
2827import org.commcare.utils.FirebaseMessagingUtil.cccCheckPassed
29- import org.commcare.utils.PushNotificationApiHelper.convertPNRecordsToPayload
3028import java.util.concurrent.TimeUnit
3129
3230/* *
@@ -112,91 +110,103 @@ class NotificationsSyncWorkerManager(
112110 delay : Long = 0,
113111 ) {
114112 val notificationSyncWorkerManager =
115- NotificationsSyncWorkerManager (context, null , false , true )
116- notificationSyncWorkerManager.startPersonalIdNotificationsWorker(
117- emptyMap() ,
118- false ,
119- delay,
120- )
113+ NotificationsSyncWorkerManager (
114+ context,
115+ showNotification = false ,
116+ syncNotification = true ,
117+ )
118+ notificationSyncWorkerManager.startPersonalIdNotificationsWorker(emptyMap(), false , delay )
121119 }
122120 }
123121
124- lateinit var notificationsPayload: ArrayList <Map <String , String >>
125-
126122 private var showNotification = false
127123 private var syncNotification = false
128-
129- var signaling = false
124+ private var isNotificationSyncScheduled = false
130125
131126 /* *
132127 * This can receive the push notification data payload from FCM and notification API.
128+ * @param context - android context
129+ * @param showNotification - decide whether to show a notification or not after sync is successful.
130+ * @param syncNotification - decide to pull / sync any pending notifications or not.
133131 */
134132 constructor (
135133 context: Context ,
136- notificationsPayload: ArrayList <Map <String , String >>,
137134 showNotification: Boolean ,
138- syncNotification: Boolean = false ,
135+ syncNotification: Boolean ,
139136 ) : this (context) {
140- this .notificationsPayload = notificationsPayload
141- this .showNotification = showNotification
142137 this .syncNotification = syncNotification
143- }
144-
145- constructor (
146- context: Context ,
147- pnsRecords: List <PushNotificationRecord >? ,
148- showNotification: Boolean ,
149- syncNotification: Boolean = false ,
150- ) : this (context) {
151- this .notificationsPayload = convertPNRecordsToPayload(pnsRecords)
152138 this .showNotification = showNotification
153- this .syncNotification = syncNotification
154139 }
155140
156- /* *
157- * This method will start Api sync for received PNs either through FCM or notification API
158- * @return whether a sync was scheduled as part of this call
159- */
160- fun startPNApiSync (): Boolean = startSyncWorker()
161-
162- private fun startSyncWorker (): Boolean {
163- var isNotificationSyncScheduled = false
164- for (notificationPayload in notificationsPayload) {
141+ private fun parseAndStartNotificationWorker (notificationPayload : Map <String , String >? ): Boolean {
142+ var notificationHandled = false
143+ notificationPayload?.let {
165144 when (notificationPayload[REDIRECT_ACTION ]) {
166145 CCC_MESSAGE -> {
167146 startPersonalIdNotificationsWorker(notificationPayload)
147+ notificationHandled = true
168148 isNotificationSyncScheduled = true
169149 }
170150
171151 CCC_DEST_PAYMENTS -> {
172152 startOpportunitiesSyncWorker(notificationPayload)
173153 startDeliverySyncWorker(notificationPayload)
154+ notificationHandled = true
174155 }
175156
176157 CCC_PAYMENT_INFO_CONFIRMATION -> {
177158 startOpportunitiesSyncWorker(notificationPayload)
159+ notificationHandled = true
178160 }
179161
180162 CCC_DEST_OPPORTUNITY_SUMMARY_PAGE -> {
181163 startOpportunitiesSyncWorker(notificationPayload)
164+ notificationHandled = true
182165 }
183166
184167 CCC_DEST_LEARN_PROGRESS -> {
185168 startOpportunitiesSyncWorker(notificationPayload)
186169 startLearningSyncWorker(notificationPayload)
170+ notificationHandled = true
187171 }
188172
189173 CCC_DEST_DELIVERY_PROGRESS -> {
190174 startOpportunitiesSyncWorker(notificationPayload)
191175 startDeliverySyncWorker(notificationPayload)
176+ notificationHandled = true
192177 }
193178 }
194179 }
180+ return notificationHandled
181+ }
182+
183+ /* *
184+ * This method will start sync for received payload from FCM
185+ * @return whether the notification was handled in this call
186+ */
187+ fun startSyncWorker (notificationPayload : Map <String , String >? ): Boolean {
188+ val notificationHandled = parseAndStartNotificationWorker(notificationPayload)
189+ syncNotificationIfRequired()
190+ return notificationHandled
191+ }
192+
193+ /* *
194+ * This method will start sync for received payloads from notification API
195+ */
196+ fun startSyncWorkers (notificationsPayload : ArrayList <Map <String , String >>? ) {
197+ notificationsPayload?.let {
198+ for (notificationPayload in notificationsPayload) {
199+ parseAndStartNotificationWorker(notificationPayload)
200+ }
201+ }
202+ syncNotificationIfRequired()
203+ }
204+
205+ // we want to get info on pending notifications irrespective of whether there are notification related FCMs or not
206+ private fun syncNotificationIfRequired () {
195207 if (syncNotification && ! isNotificationSyncScheduled) {
196- // we want to get info on pending notifications irrespective of whether there are notification related FCMs or not
197208 startPersonalIdNotificationsWorker(emptyMap(), false )
198209 }
199- return signaling
200210 }
201211
202212 private fun startPersonalIdNotificationsWorker (
@@ -281,6 +291,5 @@ class NotificationsSyncWorkerManager(
281291 ExistingWorkPolicy .KEEP ,
282292 syncWorkRequest,
283293 )
284- signaling = true
285294 }
286295}
0 commit comments