SwiftKickMobile / SwiftMessages

A very flexible message bar for UIKit and SwiftUI.
MIT License
7.24k stars 741 forks source link

Messages stop showing after user interacts (taps or dismisses) one of them #458

Closed SethForsgren closed 3 years ago

SethForsgren commented 3 years ago

I'm using SwiftMessages to alert the user to a new message from a coworker. The code below is called each time a user receives a notification. It works great until the user interacts with the SwiftMessage. Once they either tap on it, or dismiss it by dragging it up, then no more SwiftMessages are presented until the user navigates to a different view within our app itself.

I'd like new SwiftMessages to keep showing up even if the user dismisses or taps on one in their current view. Any ideas on how to achieve this?

How I'm calling show() currently:

public func showInAppMessageNotification(content: UNMutableNotificationContent) {
  // Hide the previous message if any is being shown currently
  SwiftMessages.hide(animated: false)

  // Ensure no delay in showing subsequent messages
  SwiftMessages.pauseBetweenMessages = 0.0

  // configure messages to show if same user repeatedly sends messages
  var config = SwiftMessages.Config()
  config.ignoreDuplicates = false

  // Instantiate a message view from the centeredview layout in resources
  let view = MessageView.viewFromNib(layout: .centeredView)

  // Set message title, body
  view.configureContent(title: content.title, body: content.body)

  // Increase the external margin around the card
  view.layoutMarginAdditions = UIEdgeInsets(top: 8, left: 72, bottom: 0, right: 72)

  // Reduce the corner radius
  (view.backgroundView as? CornerRoundingView)?.cornerRadius = 14

  // React when message view is tapped by navigating to the right channel view in our app
  view.tapHandler = {
    _ in

    let fromChannelID = content.userInfo["channelID"] as! String
    transitionToTappedChannel(fromChannelID: fromChannelID)

    SwiftMessages.hide()
  }

  // Show the message
  SwiftMessages.show(config: config, view: view)
}
wtmoose commented 3 years ago

There was a bug in 8.0.4 that could cause this issue. What version are you on? #444

SethForsgren commented 3 years ago

thanks for the quick reply. I'm running 9.0.0 currently.

I'll try and take a look at any differences between 8.0.4 and 8.0.5 perhaps. Lmk if other details could help debug.

SethForsgren commented 3 years ago

I created a test app that demonstrates this issue: https://github.com/SethForsgren/SM-testing

It has 2 buttons on the main screen:

When tapping "Show Message Once" => message always appears

When tapping "Show Message Once, and again in 3 seconds" => if you dismiss the first message within 3 seconds by tapping on it or dragging it away, then the second message does not present on screen. If you don't interact with the first message, then the second one will appear.

Ideally both messages will show whenever tapping "Show Message Once, and again in 3 seconds"

SethForsgren commented 3 years ago

An interesting finding is that if the user taps anywhere on the app screen after dismissing the first message, then the subsequent message will show. The subsequent message is missed if the user interacts with the first message, and then does nothing on the app until the subsequent message is supposed to show up.

SethForsgren commented 3 years ago

One more note in case it helps, I went ahead and tried that sample app with previous versions of SwiftMessages below. The app builds and reproduces the same issue with each.

9.0.0

Commit: 4408c09c863b1c6af4d053fe17fd532a4f96b237

8.0.5

Commit: 243fa7596818e3a96a7f0563453b3f35a674a5fb

8.0.4

Commit: f7297cc4e9acfaf6710cada80a396d58ff8de5b3

wtmoose commented 3 years ago

Thanks for making this so easy to troubleshoot. Could you try out the branch work/9.0.1?

Basically, in cases where the message is displayed in a separate window, that window may become the key window, like if you interact with it. iOS doesn't automatically restore the previous key window when you dismiss the message. So, the WindowViewController class in SM tries to automatically restore the key window for you, but that logic wasn't particularly robust. Hopefully my fix covers all of the cases.

SethForsgren commented 3 years ago

That branch works perfectly, thank you for the quick update!

I will use that work/9.0.1 branch now unless you want to merge it into main.

For reference, I swapped 9.0.0 for work/9.0.1 commit: eeb685398f4cea6f8c54e63bd87a0d81393cc515

wtmoose commented 3 years ago

Don't merge into main. I have a major 10.0.0 under development on main (forgot to put it on a work branch).

I'll release a 9.0.1 patch after I get chance to see if there are any other small changes I need to get in.

SethForsgren commented 3 years ago

Sounds good. I will keep an eye out for the patch.

wtmoose commented 3 years ago

9.0.1 is released