How to communicate with parent application while it's in background?

196 Views Asked by At

I'm trying to develop watchOS 3 with Xamarin. My watch is communicating with parent application while it's active. When iOS app is killed or in background state, my watch doesn't receive any updated data. I'm sending request from the watch every 10 seconds in order to get updated data. I'm using WCSession for connection. The question is: is it possible to activate parent application from watch extension? My functions for connectivity:

 public void StartSession()
    {
        if (session != null)
        {
            session.Delegate = this;
            session.ActivateSession();
            Console.WriteLine($"Started Watch Connectivity Session on {Device}");
        }
    }
public override void SessionReachabilityDidChange(WCSession session)
    {
        Console.WriteLine($"Watch connectivity Reachable:{(session.Reachable ? '✓' : '✗')} from {Device}");
        // handle session reachability change
        if (session.Reachable)
        {
            // great! continue on with Interactive Messaging
        }
        else {
            //  prompt the user to unlock their iOS device
        }
    }

    #region Application Context Methods

    public void UpdateApplicationContext(Dictionary<string, object> applicationContext)
    {
        // Application context doesnt need the watch to be reachable, it will be received when opened
        if (validSession != null)
        {
            try
            {
                var NSValues = applicationContext.Values.Select(x => new NSString(JsonConvert.SerializeObject(x))).ToArray();
                var NSKeys = applicationContext.Keys.Select(x => new NSString(x)).ToArray();
                var NSApplicationContext = NSDictionary<NSString, NSObject>.FromObjectsAndKeys(NSValues, NSKeys);
                NSError error;
                var sendSuccessfully = validSession.UpdateApplicationContext(NSApplicationContext, out error);
                if (sendSuccessfully)
                {
                    Console.WriteLine($"Sent App Context from {Device} \nPayLoad: {NSApplicationContext.ToString()} \n");
                }
                else
                {
                    Console.WriteLine($"Error Updating Application Context: {error.LocalizedDescription}");
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine($"Exception Updating Application Context: {ex.Message}");
            }
        }
    }

    public override void DidReceiveApplicationContext(WCSession session, NSDictionary<NSString, NSObject> applicationContext)
    {
        Console.WriteLine($"Receiving Message on {Device}");
        if (ApplicationContextUpdated != null)
        {
            var keys = applicationContext.Keys.Select(k => k.ToString()).ToArray();
            var values = applicationContext.Values.Select(v => JsonConvert.DeserializeObject(v.ToString())).ToArray();
            var dictionary = keys.Zip(values, (k, v) => new { Key = k, Value = v })
                                 .ToDictionary(x => x.Key, x => x.Value);

            ApplicationContextUpdated(session, dictionary);
        }
    }

    #endregion
0

There are 0 best solutions below