get ApplicationContext on react-native

I have a watchKit app connected with a react-native iOS app with react-native-watch-connectivity.

I want to use the applicationContext to communicate between the devices.

From react-native, I use watch.updateApplicationContext({ dataFromRN: "data" }) to define it and I can get it in iWatch side.

But when I use updateApplicationContext(["data":"data"]) in iWatch side, an updated context event is catch by react-native but the data is not updated.

// iWatch
try session?.updateApplicationContext(["dataFromWatch":"data"])
print(session?.applicationContext ?? "")

["dataFromWatch": "data"]

but in react-native, I have the following output for this event:

// react-native iOS
receiveApplicationContext(err, applicationContext) {
  console.log("receiveApplicationContext()", applicationContext)

receiveApplicationContext() {dataFromRN: "data"}

dataFromRN is a previous applicationContext defined from react-native side.

Event if react-native catch an event, the applicationContext is not updated.

(react-native-watch-connectivity has react version defined to 15.4.2 but I'm using react 16.2.0. Is something can be breaking changed between this versions ?)

I guess I have to update something in react-native-watch-connectivity, but I would like to know where.

If you have any element to fix this issue...

Thx

iOS side (with react-native)

import React, { Component } from "react";
import {
  Platform,
  StyleSheet,
  Text,
  View,
  ScrollView,
  AsyncStorage,
  TouchableHighlight
} from "react-native";
import * as watch from "react-native-watch-connectivity";

type Props = {};
export default class App extends Component<Props> {
  constructor(props) {
    super(props);
    this.state = { match: [] };
    this.receiveUserInfo = this.receiveUserInfo.bind(this);
    this.subscribeToApplicationContext = this.subscribeToApplicationContext.bind(this);
    this.subscribeToWatchEvents = this.subscribeToWatchEvents.bind(this);
  }

  receiveUserInfo(err, userInfo) {
    if (!err) {
      if (userInfo.currentMatch !== undefined) {
        console.log("receiveUserInfo()", userInfo);
      }
    }
  }

  receiveApplicationContext(err, applicationContext) {
    if (!err) {
      console.log("receiveApplicationContext()", applicationContext);
        watch.getApplicationContext().then(context => {
        console.log("getApplicationContext()", context);
      });
    }
  }

  subscribeToWatchEvents() {
    this.subscriptions = [
      watch.subscribeToUserInfo(this.receiveUserInfo),
      watch.subscribeToApplicationContext(this.receiveApplicationContext)
    ];
  }

  componentDidMount() {
    this.subscribeToWatchEvents();
  }

  componentWillUnmount() {}

  render() {
    return (
      <View/>
    );
  }
}

iWatch side (swift)

import WatchKit
import Foundation
import WatchConnectivity

class MainController: WKInterfaceController, WCSessionDelegate {

  var session: WCSession?

  override func awake(withContext context: Any?) {
    super.awake(withContext: context)
    if WCSession.isSupported() {
      self.session = WCSession.default
      self.session?.delegate = self
      self.session?.activate()
    }
  }

  func session(_ session: WCSession, activationDidCompleteWith activationState: WCSessionActivationState, error: Error?) {
    print("activationDidCompleteWith", activationState)
  }

  func session(_ session: WCSession, didReceiveApplicationContext applicationContext: [String : Any]) {
    print("didReceiveApplicationContext", applicationContext)
  }

  func sendUserInfo() {
    session?.transferUserInfo(["data":"data"])
    do {
      try session?.updateApplicationContext(["data":"data"])
    } catch {
      print("updateApplicationContext error")
    }
    print(session?.applicationContext ?? "")
  }

  @IBAction func point() {
    sendUserInfo()
  }

}

There doesn't seem to be any issue in your MainController InterfaceController but if you send the same message via updateApplicationContext:, it's ignored on the other counterpart app by default.

Basically, do not to send the same applicationContext.

Check this, for test purpose, we just add a random number to the applicationContext payload so it is different for successive calls.

func sendUserInfo() {
    do {
        try session?.updateApplicationContext(["from" : "watch \(arc4random()"])
    }
    catch {
        print(error)
    }
}

Comments

Popular posts from this blog

Meaning of `{}` for return expression

Get current scroll position of ScrollView in React Native

flutter websocket connection issue