在背景上推送显示整个接收内容的通知

时间:2015-12-20 15:37:07

标签: ios swift notifications apple-push-notifications

我正在开发一个聊天应用。我正在使用苹果推送通知服务在收到新消息时通知用户。有两种情况。 第一个当用户聊天并接收消息时,不应该通知用户(意味着不应该显示通知),并且当应用程序处于后台时我想要提醒用户消息。一切都很好,除了当app在后台时,通知显示客户端正在接收的整个JSON对象。

这个想法是忽略视觉通知,如果它的背景显示本地通知。

这就是我实施通知设置的方式

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
                    {

                    }
                }
            }
        }
    }
}

我将UIUserNotificationType设置为NONE。默认情况下,通知不显示任何内容?

我也读过其他一些帖子,但我找不到任何解决问题的方法。

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

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

任何帮助都将不胜感激。

1 个答案:

答案 0 :(得分:1)

如果应用程序已关闭或处于后台状态,则不会调用应用程序didReceiveRemoteNotification,因此您将无法创建本地通知。因此,您需要将要显示的文本传递到与警报键关联的aps词典中。

如果要为活动状态案例传递更多信息,则应使用自定义键将它们添加到推送字典中。 例如:

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