Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

First push notification disappears after few seconds

My app first notification disappears after few seconds itself when app is in background even when the user has not taken any action to that notification. After that every notification remains in place.

Does setting application badge number to 0 or cancelling local notifications has any effect on this.(I tried commenting those parts but it did not fix).

`func application(application: UIApplication, 
 didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) 
 -> Bool {


    Fabric.with([Crashlytics.self])

    registerForPushNotifications(application)
    application.registerForRemoteNotifications()

    //Firebase push notification
    FIRApp.configure()



    // Add observer for InstanceID token refresh callback.
    NSNotificationCenter.defaultCenter().addObserver(self,
                                                    selector: #selector(self.tokenRefreshNotification),
                                                    name: kFIRInstanceIDTokenRefreshNotification,
                                                    object: nil)

  UIApplication.sharedApplication().applicationIconBadgeNumber = 0



    }


}`

  func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) {

    guard  let aps = userInfo["aps"] as? [String: AnyObject] else{
        return
    }

    let state = UIApplication.sharedApplication().applicationState
    if state == .Background || state == .Inactive{
       //do some background work
   }

}

Any help would be greatly appreciated.

like image 952
Daemank404 Avatar asked Oct 29 '25 17:10

Daemank404


1 Answers

The issue is due to manually setting the badge number. I had code in my app to manually set the badge number on application launch (either to 0 or to a value saved in NSUserDefaults, depending on the situation) and found that doing either one of these would cause the notification banner to disappear and the sound would cut off mid-notification. To solve it, I checked the applicationState and if it was UIApplicationStateBackground I did not touch the badge number.

So, based on your code above, either remove the line in didFinishLaunching to set the badge to 0, or ensure the application state is not background before setting to 0.

I posted a similar answer here: https://stackoverflow.com/a/53381819/2788608

like image 136
Stonz2 Avatar answered Oct 31 '25 08:10

Stonz2