1

Ok so my push notification work like a charm when the app is running in the foreground. But when ever I enter into the background, the app never receives the push notification. Its like the notification falls on deaf ears. So this is what happening. When the app is first started, I can received notification. When I close and reopen the app I can receive notification. But when the app is closed in the background I cannot receive notification. I print off when the app goes into the background and when the app becomes active so I know that its not closing I think. Because its printing that its going into the background, so it should be running. So this is what I have for the app deligate class:

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

 //OTHER THINGS
   //Open the Push note page
    if launchOptions != nil
    {
        print(launchOptions)
        let storyboard = UIStoryboard(name: "Main", bundle: nil)
        let vc = storyboard.instantiateViewControllerWithIdentifier(myPage)
        self.window?.rootViewController = vc
    }

    //handel push note if app is closed
    //Sends it to the regular handler for push notifcaiton
    //didrecivepushnotificaiton
    if let remoteNotification = launchOptions?[UIApplicationLaunchOptionsRemoteNotificationKey] as? NSDictionary
    {
        print("Got A Push Note when I was closed")
        self.application(application, didReceiveRemoteNotification: remoteNotification as [NSObject : AnyObject])
    }

 }

  func application( application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken
    deviceToken: NSData ) {
    print("DEVICE TOKEN = \(deviceToken)")

    //convert the device token into a string
    let tokenChars = UnsafePointer<CChar>(deviceToken.bytes)
    var token = ""

    for i in 0..<deviceToken.length {
        token += String(format: "%02.2hhx", arguments: [tokenChars[i]])
    }
    print("token: " + token)
    //store the user device token for apns push notification
    loginInformation.setObject(token, forKey: "token")
    self.loginInformation.synchronize()

}

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

    print("Recived Push Notificaion")

    var myMess :String = ""
    var url : String = ""
    var myTitle : String = ""

    if var alertDict = userInfo["aps"] as? Dictionary<String, String> {

        print("Alert Dict")
        print(alertDict)

        url = alertDict["url"]!
        myMess = alertDict["alert"]!
        myTitle = alertDict["mytitle"]!
        //store the url for the push control view
        loginInformation.setObject(url, forKey: "URL")
        loginInformation.setObject(myMess, forKey: "Message")
        loginInformation.setObject(myTitle, forKey: "Title")
        self.loginInformation.synchronize()


    }else{print("No go")}
    application.applicationIconBadgeNumber = 0
    //post notification.
    NSNotificationCenter.defaultCenter().postNotificationName("PushReceived", object: nil, userInfo: userInfo)


    if myTitle == ""
    {
        myTitle = “New Title“
    }
    if myMess == ""
    {
        myMess = “All Hail Gus“
    }

    let alert = UIAlertController(title: myTitle, message: myMess, preferredStyle: UIAlertControllerStyle.Alert)
    //Close Button
     alert.addAction(UIAlertAction(title: "次回", style: UIAlertActionStyle.Default, handler: nil))
     self.window?.rootViewController?.presentViewController(alert, animated: true, completion: nil)


}

func registrationHandler(registrationToken: String!, error: NSError!) {

}

// [START receive_apns_token_error]
func application( application: UIApplication, didFailToRegisterForRemoteNotificationsWithError
    error: NSError ) {
    print(error)
}

I think I have all the right setting on this too. But I am not too sure now. The push notifications did work but I made a lot of changes and haven't tested them in a while.

enter image description here enter image description here enter image description here

And this is an example of the payload

{"aps":{"alert":"Gus Message.","badge":"1", "url":"https://www.gus.com","mytitle":"Gus Title"}}
5
  • Do you want to show ALERT if the app is in Background and receives a Push? Commented Aug 31, 2016 at 8:37
  • I want the little banner thingy to pop up and tell them that there is some fun stuff going on up in here :) Commented Aug 31, 2016 at 8:46
  • I don't know what a silent push notification is. I just went off raywenderlich.com/123862/push-notifications-tutorial this tutorial and added the stuff I needed to get the information out of it Commented Aug 31, 2016 at 8:49
  • i think you confuse between foreground and background, generally we receives push notification only in background, and it ignores the notification in foreground. Commented Aug 31, 2016 at 8:50
  • I use the alert when the app is in the foreground to let the user know there was a push note. Commented Aug 31, 2016 at 8:52

1 Answer 1

2

To fully implement APNS servie, u have to handle three cases:

  1. inactive
  2. foreground
  3. background

the inactive mode should be handled in didFinishLaunchingWithOptions method

//receive apns when app in inactive mode, remaining message hint display task could be sum up by the code in applicationwillenterforeground
    if let options = launchOptions {
        if options[UIApplicationLaunchOptionsRemoteNotificationKey] != nil {
            let userInfo: NSDictionary = options[UIApplicationLaunchOptionsRemoteNotificationKey] as! NSDictionary
            let apsInfo: NSDictionary = userInfo["aps"] as! NSDictionary
            //parse notification body message ...

                    NSNotificationCenter.defaultCenter().postNotificationName(APP_NOTIF_RECEIVE_REMOTE_NOTIF, object: userInfo)
                    APService.handleRemoteNotification(userInfo as! [NSObject : AnyObject])

                    //Update badge number

                    let badgeIndex = apsInfo["badge"] as! Int
                    UIApplication.sharedApplication().applicationIconBadgeNumber = badgeIndex
                }
            }
        } else if options[UIApplicationLaunchOptionsURLKey] != nil {
            if let url = launchOptions?[UIApplicationLaunchOptionsURLKey] as? NSURL {
                print(url)
            }
        }
    }

the remaining two cases should be handle in didReceiveRemoteNotification method

//receive apns when app in background mode
    let apsInfo: NSDictionary = userInfo["aps"] as! NSDictionary
    if UIApplication.sharedApplication().applicationState != UIApplicationState.Active{

        //TODO: temporary method, need further update
        //judge notification type
        if let _ = userInfo["inviterName"] as? String {
            //notification for group invite
        }else{
            //Update badge number
            if let badgeInt = apsInfo["badge"] as? Int {
                UIApplication.sharedApplication().applicationIconBadgeNumber = badgeInt > 0 ? badgeInt: 1
            }else{
                UIApplication.sharedApplication().applicationIconBadgeNumber = 1
            }
            //turn on trigger to enable message hint btn on recorder vc when it appear
            NSUserDefaults.standardUserDefaults().setBool(true, forKey: REMOTE_NOTIF_REMAINING)
        }
    }
   //receive apns when app in forground mode
    else{
        //TODO: temporary method, need further update
        //judge notification type
        if let _ = userInfo["inviterName"] as? String {
            //notification for group invite
            NSNotificationCenter.defaultCenter().postNotificationName(APP_NOTIF_RECEIVE_GROUP_NOTIF, object:nil)
        }else{
            //notificate recorder vc to display message hint directly
            NSNotificationCenter.defaultCenter().postNotificationName(APP_NOTIF_RECEIVE_REMOTE_NOTIF, object: userInfo)
        }
    }
    APService.handleRemoteNotification(userInfo)
    completionHandler(UIBackgroundFetchResult.NewData)
Sign up to request clarification or add additional context in comments.

6 Comments

I think mine does this already. I have the didRecieveRemoteNotification method and it crates an alert for the user. Is there a better way of doing this than how I did?
seems background case handler missing. In didReceiveRemoteNotification method, u have to firstly check application status by UIApplication.sharedApplication().applicationState property. In UIApplicationState.Active case, its foreground. if not, its background(missing in ur code).
@ChenWei. how can i identify this userInfo["aps"] as! NSDictionary
@UmaMadhavi. In iOS9 earlier func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any]) {let aps_dic : NSDictionary = userInfo["aps"] as! NSDictionary ...}
@UmaMadhavi. In iOS10, didReceiveRemoteNotification method deprecated, u should use func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) { let content: UNNotificationContent = response.notification.request.content let userInfo = content.userInfo} ...
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.