我需要实现一个小的文本UI,以显示来自Unity中收件箱的OTP MSM消息。
有人知道如何在Unity中检查或发送SMS消息而无需打开默认SMS应用程序吗?
我找到了以下链接,用于在不启动默认SMS应用程序的情况下发送消息。我想阅读SMS消息,以便为注册过程自动填写OTP。可以用Unity吗?
https://gist.github.com/rmdwirizki/87f9e68c7ef6ef809a777eb25f12c3b2
答案 0 :(得分:2)
在Android平台上:
演示项目 here
创建一个AAR plug-ins and Android Libraries。 (您可以选择jar或aar来实现该插件,唯一的区别是aar插件包含Android资源。在这种情况下,我们需要向AndroidManifiedt.xml中添加权限,该权限可以放在aar中,并在以后由Unity自动合并)
遵循此说明Create an Android library。
在aar插件中,实现用于处理SMS接收意图的BroadcastReceiver
。
SmsListener.java
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.telephony.SmsMessage;
import com.unity3d.player.UnityPlayer;
public class SmsListener extends BroadcastReceiver {
private final String UnityCallbackObject = "UnitySMSReceiver";
private final String UnityCallbackMethod = "OnSMSReceive";
@Override
public void onReceive(Context context, Intent intent) {
StringBuilder builder = new StringBuilder();
Object[] objects = (Object[]) intent.getExtras().get("pdus");
SmsMessage message = null;
for (int i = 0; i < objects.length; i++) {
message = SmsMessage.createFromPdu((byte[]) objects[i]);
builder.append(message.getDisplayMessageBody());
}
UnityPlayer.UnitySendMessage(UnityCallbackObject, UnityCallbackMethod, builder.toString());
}
}
AndroidManifiedt.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="your.package.name.here">
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<uses-permission android:name="android.permission.READ_SMS" />
<application>
<receiver android:name="your.package.name.here.SmsListener" android:enabled="true">
<intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
</application>
</manifest>
在Unity端,将生成的aar文件放入Assets/Plugins/Android
中。
创建一个名为UnitySMSReceiver
的游戏对象,并在场景中附加脚本实现功能OnSMSReceive
。
public void OnSMSReceive (string message) {
// do something with your SMS message here.
}
我的环境:
Unity 2017.2.0f3
Android Studio 3.2.1
参考文献:
Android testing sms from emulator
Android – Listen For Incoming SMS Messages
How can I read SMS messages programmatically in Android
Android: Programmatically receiving SMS messags
Android SMS Receiver not working
https://blog.gradle.org/introducing-compile-only-dependencies