From 456160b845c656cc3359425375edb88b7070eac0 Mon Sep 17 00:00:00 2001 From: Brandon Stalnaker <33703490+BrandonStalnaker@users.noreply.github.com> Date: Thu, 14 Mar 2024 14:35:08 -0400 Subject: [PATCH] fix: Correct threading around user notification log (#261) --- mParticle-Apple-SDK/MPBackendController.m | 56 ++++++++++++----------- 1 file changed, 29 insertions(+), 27 deletions(-) diff --git a/mParticle-Apple-SDK/MPBackendController.m b/mParticle-Apple-SDK/MPBackendController.m index 42f1c997..a7f94224 100644 --- a/mParticle-Apple-SDK/MPBackendController.m +++ b/mParticle-Apple-SDK/MPBackendController.m @@ -1960,39 +1960,41 @@ - (void)handleDeviceTokenNotification:(NSNotification *)notification { } - (void)logUserNotification:(MParticleUserNotification *)userNotification { - NSMutableDictionary *messageInfo = [@{kMPPushNotificationStateKey:userNotification.state, - kMPPushMessageProviderKey:kMPPushMessageProviderValue, - kMPPushMessageTypeKey:userNotification.type} - mutableCopy]; - - NSString *tokenString = [MPIUserDefaults stringFromDeviceToken:[MPNotificationController deviceToken]]; - if (tokenString) { - messageInfo[kMPDeviceTokenKey] = tokenString; - } - - if (userNotification.redactedUserNotificationString) { - messageInfo[kMPPushMessagePayloadKey] = userNotification.redactedUserNotificationString; - } - - if (userNotification.actionTitle) { - messageInfo[kMPPushNotificationActionTitleKey] = userNotification.actionTitle; - } + [MParticle executeOnMessage:^{ + NSMutableDictionary *messageInfo = [@{kMPPushNotificationStateKey:userNotification.state, + kMPPushMessageProviderKey:kMPPushMessageProviderValue, + kMPPushMessageTypeKey:userNotification.type} + mutableCopy]; + + NSString *tokenString = [MPIUserDefaults stringFromDeviceToken:[MPNotificationController deviceToken]]; + if (tokenString) { + messageInfo[kMPDeviceTokenKey] = tokenString; + } + + if (userNotification.redactedUserNotificationString) { + messageInfo[kMPPushMessagePayloadKey] = userNotification.redactedUserNotificationString; + } + + if (userNotification.actionTitle) { + messageInfo[kMPPushNotificationActionTitleKey] = userNotification.actionTitle; + } - if (userNotification.actionIdentifier) { - messageInfo[kMPPushNotificationActionIdentifierKey] = userNotification.actionIdentifier; - } + if (userNotification.actionIdentifier) { + messageInfo[kMPPushNotificationActionIdentifierKey] = userNotification.actionIdentifier; + } - if (userNotification.behavior > 0) { - messageInfo[kMPPushNotificationBehaviorKey] = @(userNotification.behavior); - } + if (userNotification.behavior > 0) { + messageInfo[kMPPushNotificationBehaviorKey] = @(userNotification.behavior); + } - MPMessageBuilder *messageBuilder = [[MPMessageBuilder alloc] initWithMessageType:MPMessageTypePushNotification session:_session messageInfo:messageInfo]; + MPMessageBuilder *messageBuilder = [[MPMessageBuilder alloc] initWithMessageType:MPMessageTypePushNotification session:self.session messageInfo:messageInfo]; #ifndef MPARTICLE_LOCATION_DISABLE - [messageBuilder location:[MParticle sharedInstance].stateMachine.location]; + [messageBuilder location:[MParticle sharedInstance].stateMachine.location]; #endif - MPMessage *message = [messageBuilder build]; + MPMessage *message = [messageBuilder build]; - [self saveMessage:message updateSession:(_session != nil)]; + [self saveMessage:message updateSession:(self.session != nil)]; + }]; } #endif