From 3e9bd30b00901f0f184119deef2ae6ab41957daa Mon Sep 17 00:00:00 2001 From: Corey Hulen Date: Tue, 12 Sep 2017 17:49:42 -0700 Subject: PLT-7502 fixing clearing of push notifications for iOS (#7429) * PLT-7502 fixing clearing of push notificaitons for iOS * PLT-7502 fixing clearing of push notificaitons for iOS --- app/notification.go | 55 ++++++++++++++++++++++++++++++----------------------- 1 file changed, 31 insertions(+), 24 deletions(-) (limited to 'app/notification.go') diff --git a/app/notification.go b/app/notification.go index 255e8c399..b0a6a02e2 100644 --- a/app/notification.go +++ b/app/notification.go @@ -649,32 +649,39 @@ func (a *App) sendPushNotification(post *model.Post, user *model.User, channel * return nil } -func (a *App) ClearPushNotification(userId string, channelId string) *model.AppError { - sessions, err := a.getMobileAppSessions(userId) - if err != nil { - return err - } - - msg := model.PushNotification{} - msg.Type = model.PUSH_TYPE_CLEAR - msg.ChannelId = channelId - msg.ContentAvailable = 0 - if badge := <-a.Srv.Store.User().GetUnreadCount(userId); badge.Err != nil { - msg.Badge = 0 - l4g.Error(utils.T("store.sql_user.get_unread_count.app_error"), userId, badge.Err) - } else { - msg.Badge = int(badge.Data.(int64)) - } - - l4g.Debug(utils.T("api.post.send_notifications_and_forget.clear_push_notification.debug"), msg.DeviceId, msg.ChannelId) +func (a *App) ClearPushNotification(userId string, channelId string) { + go func() { + // Sleep is to allow the read replicas a chance to fully sync + // the unread count for sending an accurate count. + // Delaying a little doesn't hurt anything and is cheaper than + // attempting to read from master. + time.Sleep(time.Second * 5) + + sessions, err := a.getMobileAppSessions(userId) + if err != nil { + l4g.Error(err.Error()) + return + } + + msg := model.PushNotification{} + msg.Type = model.PUSH_TYPE_CLEAR + msg.ChannelId = channelId + msg.ContentAvailable = 0 + if badge := <-a.Srv.Store.User().GetUnreadCount(userId); badge.Err != nil { + msg.Badge = 0 + l4g.Error(utils.T("store.sql_user.get_unread_count.app_error"), userId, badge.Err) + } else { + msg.Badge = int(badge.Data.(int64)) + } - for _, session := range sessions { - tmpMessage := *model.PushNotificationFromJson(strings.NewReader(msg.ToJson())) - tmpMessage.SetDeviceIdAndPlatform(session.DeviceId) - go a.sendToPushProxy(tmpMessage, session) - } + l4g.Debug(utils.T("api.post.send_notifications_and_forget.clear_push_notification.debug"), msg.DeviceId, msg.ChannelId) - return nil + for _, session := range sessions { + tmpMessage := *model.PushNotificationFromJson(strings.NewReader(msg.ToJson())) + tmpMessage.SetDeviceIdAndPlatform(session.DeviceId) + go a.sendToPushProxy(tmpMessage, session) + } + }() } func (a *App) sendToPushProxy(msg model.PushNotification, session *model.Session) { -- cgit v1.2.3-1-g7c22