A senior Chinese official said on Friday that Beijing has reached an agreement with Vietnam on how to manage their South China Sea dispute ahead of a visit by President Xi Jinping next week that will see him competing with the United States for influence.

Xi will attend the Asia-Pacific Economic Cooperation summit in Da Nang along with other world leaders, including US President Donald Trump.

Hanoi and Beijing have competing claims in the waters and the dispute flared up in June when the Vietnamese government refused a Chinese demand to halt drilling by a subsidiary of Spanish company Repsol in the area.

A meeting between their foreign ministers at a regional summit in Manila in August was cancelled, although the two sides have been working to mend relations in recent weeks.

Assistant foreign minister Chen Xiaodong said the two countries had shared frank exchanges over the matter and reached a consensus.

“Both sides will uphold the principle of friendly consultations and dialogue to jointly manage and control maritime disputes, and protect the bigger picture of developing Sino-Vietnam relations and stability in the South China Sea,” he said.

In a meeting with Foreign Minister Wang Yi in Hanoi on Thursday, Vietnam’s Deputy Prime Minister Pham Binh Minh said both sides should avoid actions that would complicate the dispute.

Meanwhile, China’s Vice Commerce Minister Wang Shouwen said on Friday that China would seek to deepen economic cooperation with Vietnam, adding that its investments in the country had reached US$150 billion this year.

Despite Vietnam’s deep suspicions towards Beijing regarding security, it has maintained close economic ties. The Trump administration’s decision to withdraw from the Trans-Pacific Partnership trade deal, of which Vietnam would have been a prime beneficiary, has prompted renewed efforts to improve trade with China.

Trump, who is preparing to visit five countries in Asia, including China and Vietnam, is expected to make clear what role he thinks the US should play in the region after a year of uncertainty over what his “America First” policies will mean.

The US administration has started using the term “Indo-Pacific” which suggests a focus on a far wider area than just China and its neighbours.

Trump’s speech at the Da Nang summit would “include his vision for a free and open Indo-Pacific region as well as the US relationship with countries in the region,” Trump’s National Security Adviser HR McMaster said on Thursday.

“I think the main message is that the United States looks forward to strengthening our relationship with Vietnam,” McMaster said.

He also said that Trump was committed to freedom of navigation in the disputed waters, where Brunei, Malaysia, the Philippines and Taiwan also have claims.