Ad

Push Notification Displaying Whole Receiving Content When Is On Background

I'm developing a chat app. I'm using apple push notification service to notify user when he receives new messages. There are two scenarios. The first when user is chatting and receiving a message, the user shouldn't be notified (meaning that notification shouldn't be shown) and when the app is in background i want to alert user for the messages. Everything is ok except that when app is on background the notification shows the whole JSON object the client is receiving.

The idea is ignore visually notification and if its on background show a local Notification.

This is how i have implemented the notification settings

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject : AnyObject]?) -> Bool {
        let types: UIUserNotificationType = [UIUserNotificationType.None]
        let settings: UIUserNotificationSettings = UIUserNotificationSettings(forTypes: types, categories: nil)
        application.registerUserNotificationSettings(settings)
        application.registerForRemoteNotifications()

        return true
    }

func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject])
{
    //App handle notifications in background state
    if application.applicationState == UIApplicationState.Background {

        var login_user = LoginUser();
        login_user.loadData();

        var username:String!;
        var message:String!;

        if let msg = userInfo["aps"]as? Dictionary<String,AnyObject>
        {
            if let alert = msg["alert"] as? String{
                if let data = alert.dataUsingEncoding(NSUTF8StringEncoding)
                {
                    do
                    {
                        let jsonObject = try NSJSONSerialization.JSONObjectWithData(data,options: [])
                        username = jsonObject["senderUserName"] as! String;
                        message = jsonObject["content"] as! String!;
                        DatabaseOperations().insert(DatabaseOperations().STRING_VALUE_CHATING_USERNAME, value: username);
                        NSNotificationCenter.defaultCenter().postNotificationName("push_notification", object: self)
                    }
                    catch
                    {

                    }
                }
            }
        }

        let localNotification: UILocalNotification = UILocalNotification()

        switch(login_user.privacyLevelId)
        {
        case 1:
            localNotification.alertBody = username + ":" + message;
            break;
        case 2:
            localNotification.alertBody = username;
            break;
        case 3:
            localNotification.alertBody = "New Message";
            break;
        default:
            localNotification.alertBody = "New Message";
            break;
        }

        localNotification.alertAction = "Message"
        localNotification.fireDate = NSDate(timeIntervalSinceNow: 5)
        localNotification.soundName = UILocalNotificationDefaultSoundName
        UIApplication.sharedApplication().scheduleLocalNotification(localNotification)


    }
    //App is shown and active
    else
    {

        if let msg = userInfo["aps"]as? Dictionary<String,AnyObject>
        {
            if let alert = msg["alert"] as? String
            {
                if let data = alert.dataUsingEncoding(NSUTF8StringEncoding)
                {
                    do
                    {
                        let jsonObject = try NSJSONSerialization.JSONObjectWithData(data,options: [])
                        let sender:String = jsonObject["senderUserName"] as! String;
                        DatabaseOperations().insert(DatabaseOperations().STRING_VALUE_CHATING_USERNAME, value: sender);
                        NSNotificationCenter.defaultCenter().postNotificationName("push_notification", object: self)
                    }
                    catch
                    {

                    }
                }
            }
        }
    }
}

I set UIUserNotificationType to NONE. Shouldn't by default the notification shows nothing?

I also have read some other posts, but i couldn't find anything to solve the problem.

Why does UIUserNotificationType.None return true in the current settings when user permission is given?

Hide, do not display remote notification from code (swift)

Any help would be appreciated.

Ad

Answer

application didReceiveRemoteNotification won't be called if the app is closed or in the background state, so you won't be able to create a local notification. So you need to pass the text you want to display in the aps dictionnary, associated with the alert key.

If you want to pass more information for the active state case, you should add them with a cuext you want to display in the aps dictionnary, associated with the alert key.

If you want to pass more information for the active state case, you should add them with a custom key to the push dictionnary. For example :

{"aps": {         
    "badge": 1,
    "alert": "Hello World!",
    "sound": "sound.caf"},
 "task_id": 1}
Ad
source: stackoverflow.com
Ad