Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Push notifications disappear if received while app not running #338

Closed
DavideCan opened this issue Feb 16, 2018 · 13 comments
Closed

Push notifications disappear if received while app not running #338

DavideCan opened this issue Feb 16, 2018 · 13 comments

Comments

@DavideCan
Copy link

Push notifications disappear if received when the app is closed by ios (you can simulate this status by stopping the app from xcode) in the background and then comes a push with content-available to 1. In this case iOS calls application:didFinishLaunchingWithOptions before calling application:didReceiveRemoteNotification:fetchCompletionHandler:.

In application:didFinishLaunchingWithOptions, as suggested by your documentation, I call OneSignal.initWithLaunchOptions, which in turn calls UIApplication.registerForRemoteNotifications which will make the notification disappear as soon as it arrives.

see this answer in Stack Overflow

@Nightsd01
Copy link
Contributor

Hi David,

Calling UIApplication.shared.registerForRemoteNotifications() should not and does not clear previous/current notifications.

Can you give me some information about your setup, ie. what version of the iOS SDK are you using, how are you adding it as a dependency, what kind of code are you executing in the handleNotificationReceived block (passed in with OneSignal.init) or AppDelegate's didReceiveRemoteNotification method?

@Nightsd01
Copy link
Contributor

Also @DavideCan , can you use Command+Shift+F and search your entire project to see if you use removeAllDeliveredNotifications() or removeAllPendingNotificationRequests() UNUserNotificationCenter methods anywhere in your project?

@DavideCan
Copy link
Author

hi @Nightsd01,
I have version 2.7.0 of OneSignal and I have commented out all my code passed to OneSignal.initWithLaunchOptions. Only by removing OneSignal.initWithLaunchOptions does the problem no longer occur.

my code is currently this

        let handleNotificationReceived: OSHandleNotificationReceivedBlock = {  (notification) in
        }

        let handleNotificationAction: OSHandleNotificationActionBlock = { (notificationOpenedResult) in
            
        }

        let onesignalInitSettings = [kOSSettingsKeyAutoPrompt: false,
                                     kOSSettingsKeyInAppLaunchURL: false]

        OneSignal.initWithLaunchOptions(launchOptions, appId: AppDataProvider.shared.oneSignalAppId, handleNotificationReceived: handleNotificationReceived, handleNotificationAction: handleNotificationAction, settings: onesignalInitSettings)
        OneSignal.inFocusDisplayType = OSNotificationDisplayType.none

To reproduce the problem, run the app with XCode, stop the app with XCode (with the stop button ◼︎) and send a push with content-available set to 1 and also with some text. The notification will appear in the lock-screen and will disappear after a few moments

@Nightsd01
Copy link
Contributor

Nightsd01 commented Feb 17, 2018

@DavideCan This does not appear to be an issue caused by the SDK, calling UIApplication.shared.registerForRemoteNotifications() does not clear/cancel notifications. I suspect there is something else in your project that is clearing the notification.

  1. Do you have any code getting executed in the AppDelegate's didReceiveRemoteNotification method?

  2. Can you search your project for any usage of the method removeAllDeliveredNotifications()?

  3. Lastly, if you send your device a few notifications with content-available set to false, and then send a notification with content-available set to true, does it not only clear the new notification but also all previous notifications? Or does it only clear the most recent notification?

@irzadhuzaen
Copy link

@DavideCan Me to have the same problem not solved yet.

@DavideCan
Copy link
Author

DavideCan commented Feb 19, 2018

Hi @Nightsd01, you're right, registerForRemoteNotifications does not remove notifications.

I created an empty project with only the OneSignal init in the application:didFinishLaunchingWithOptions and the problem still occurs.

But I managed to figure out what exactly is causing the problem: resetting the badge count.

So as soon as a notification arrives that sets both the badge and content-available (always, in the case of my backend) and the app has been previously closed by iOS the problem occurs.

My momentary fix is to set OneSignal_disable_badge_clearing in the info.plist and insert in AppDelegate

    func applicationWillEnterForeground(_ application: UIApplication) {
        application.applicationIconBadgeNumber = 0
    }

I hope I managed to make everyone understand how to reproduce the problem.

@DavideCan
Copy link
Author

Why was the issue closed? I think at least a paragraph should be entered on troubleshooting-ios to explain how to solve.

@Maqix
Copy link

Maqix commented Mar 13, 2018

Hi, I have the same problem using the Cordova SDK for iOS (which in turn uses this SDK)... Any solution/workaround? My customers are losing push notifications for this.

@Nightsd01
Copy link
Contributor

We have updated the documentation to explain why this happens and how to prevent it if you don't want this behavior - most developers do but some don't.

https://documentation.onesignal.com/v5.0/docs/troubleshooting-ios#section-previous-push-notifications-disappear

@Maqix
Copy link

Maqix commented Mar 14, 2018

Who in this world wants this behavior (please see the attached GIF)???

ezgif com-optimize

@Nightsd01
Copy link
Contributor

@Maqix The SDK would not be causing that behavior. If you can, you can zip up your project and I can take a look.

@Nightsd01
Copy link
Contributor

@Maqix Also...are you setting the badge count to 0 in this latest push notification?

iOS will automatically clear notifications if you set the badge count to 0.

@DhirenBharadava143
Copy link

Push notifications disappear if received while app not running

Hello everyone please try this below snippet

func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
        completionHandler([.sound, .badge, .banner,.list])
}

Note :- just need to add .list in this method so you will get disappear push notification

Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants