Maui SMS

[删除(380066935@qq.com或微信通知)]

更好的阅读体验请查看原文:https://learn.microsoft.com/zh-cn/dotnet/maui/platform-integration/communication/sms?view=net-maui-7.0&tabs=android

本文介绍如何使用 .NET 多平台应用 UI (.NET MAUI) ISms 接口打开默认短信应用,并使用消息和收件人预加载它。

接口的默认实现 ISms 可通过 Sms.Default 属性获得。 ISms接口和 Sms 类都包含在 命名空间中Microsoft.Maui.ApplicationModel.Communication

入门

若要访问 SMS 功能,需要以下特定于平台的设置。

如果项目的目标 Android 版本设置为 Android 11 (R API 30) 或更高版本,则必须使用使用 Android 包可见性要求的查询更新 Android 清单

Platforms/Android/AndroidManifest.xml 文件中,在 节点manifest中添加以下queries/intent节点:

<queries>
  <intent>
    <action android:name="android.intent.action.VIEW" />
    <data android:scheme="smsto"/>
  </intent>
</queries>

创建消息

SMS 功能的工作原理是 SmsMessage 创建新的 对象并调用 ComposeAsync 方法。 可以选择包含一封邮件以及零个或多个收件人。

if (Sms.Default.IsComposeSupported)
{
    string[] recipients = new[] { "000-000-0000" };
    string text = "Hello, I'm interested in buying your vase.";

    var message = new SmsMessage(text, recipients);

    await Sms.Default.ComposeAsync(message);
}