如何使用NFC重定向到或打开我的渐进式Web应用程序?
问题描述:
我有一个渐进式的网络应用程序,app.example.com
使用Chrome上的Android的“添加到主屏幕”按钮创建。如何使用NFC重定向到或打开我的渐进式Web应用程序?
我有一个NFC标签,当它被点击时,通常会在Chrome中打开app.example.com/nfc_app
。
我该如何做到这一点,使NFC标签在点击时打开app.example.com/nfc_app
PWA而不是Chrome?
答
添加辅助应用程序作为一个变通:
package com.something;
import android.content.Intent;
import android.net.Uri;
import android.nfc.NfcAdapter;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
private void handleNfcIntent(@Nullable Intent intent) {
if (intent != null && NfcAdapter.ACTION_NDEF_DISCOVERED.equals(intent.getAction())) {
//clear the intent so it doesn't come back
intent.setAction("");
Uri uri = intent.getData();
if (uri != null) {
Intent uriOnlyIntent = new Intent(Intent.ACTION_VIEW, uri);
//call the progressive web app registered to view the uri
startActivity(uriOnlyIntent);
}
}
}
@Override
protected void onNewIntent(final Intent intent) {
super.onNewIntent(intent);
setIntent(intent);
}
@Override
protected void onResume() {
super.onResume();
//https://stackoverflow.com/a/36942185 provided the insight for handling this way
//always called after onNewIntent and onCreate allowing a tag reward for a closed app
handleNfcIntent(getIntent());
}
}
您的AndroidManifest.xml监听你的目标网址:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.something">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
<intent-filter>
<action android:name="android.nfc.action.NDEF_DISCOVERED"/>
<category android:name="android.intent.category.DEFAULT"/>
<data android:host="something.com" android:scheme="https" />
</intent-filter>
</activity>
</application>
</manifest>
现在,当您扫描标签,该NFC意向会由安装的应用程序处理,该应用程序转发到您的渐进式Web应用程序注册的查看意向。
对我来说,这只适用于Android 8,而不是Android 6,因为通过点击Chrome中的链接显然打开PWA也是不受支持的。
一个不幸的解决方法,所以我希望能看到更好的答案。
+0
有关清单的更多信息,请参阅Google开发者帖子:https://developers.google.com/web/updates/2017/02/improved-add-to-home-screen #android_intent_filters –
我有同样的问题。当从其他网站选择链接时,我的PWA打开,但NFC意图打开至Chrome。像素上的Android 8和LG Pheonix 2上的Android 6 –