搜索插件、代码仓与 APP 分类

插件详情v1.1.0

sms_advanced

这是一个适用于 Flutter 的短信库。它可在 Android 上实现发送、接收、查询短信消息、短信送达状态以及查询联系人信息,而在 iOS 和 Web 上仅支持发送短信。

AndroidiOSWeb
近 30 天下载
74
喜欢数
100
Pub 分数
140

安装插件

flutter pub add sms_advanced

插件介绍

基于插件公开信息、社区数据与项目使用情况整理的选型参考。

sms_advanced 是一个 Flutter 包,可在 Android、iOS 和 Web 平台上实现短信功能。在 Android 上,它支持发送、接收、查询消息、跟踪送达状态以及获取联系人信息。在 iOS 和 Web 上,仅支持发送短信。该包提供统一的接口来处理短信操作,适用于需要基本短信集成的应用程序。它依赖于原生平台功能,并且在 Android 上需要适当的权限。源代码公开托管在 GitHub 上,以确保透明度和可定制性。

适合场景

  • 从 Flutter 应用发送短信
  • 在 Android 上接收和管理短信
  • 查询消息传递状态
  • 集成联系信息检索
  • 构建双向短信通信

主要特点

  • 跨平台短信支持
  • 消息发送和接收(仅限 Android)
  • 配送状态跟踪
  • 联系信息查询
  • 公共 GitHub 源代码

适合对象

  • 需要短信验证的应用
  • 客服聊天机器人与短信
  • 双向短信消息系统
  • 面向 Android 的应用程序,具有短信需求
  • 想要开源短信工具的开发者

使用建议

  • iOS 和网页端短信接收受限
  • 需要 Android 权限以实现完整功能
  • 平台特定的行为差异
  • 无内置短信解析或过滤
  • 依赖于原生设备功能

原始 README

以下为关联源码仓库的 README 原文快照,最新内容请访问源码仓库。

sms_advanced

Buy Me A Coffee

This is an SMS library for Flutter.

Getting Started

Installation and Usage

Once you're familiar with Flutter you may install this package adding sms_advanced (1.0.1 or higher) to the dependencies list of the pubspec.yaml file as follow:

...
dependencies:
  flutter:
    sdk: flutter

  sms_advanced: ^1.0.1
...

Then run the command flutter packages get on the console.

Querying SMS messages

Add the import statement for sms_advanced and create an instance of the SmsQuery class:

import 'package:sms_advanced/sms_advanced.dart';

void main() {
  SmsQuery query = new SmsQuery();
}

Getting all SMS messages

List<SmsMessage> messages = await query.getAllSms;

Note: the use of await keyword means that getAllSms is resolved asynchronously and a Future is retorned. To use await, you must be in a function marked async.

Filtering SMS messages

The method querySms from the SmsQuery class returns a list of filtered SMS messages that match the supplied parameters. For example, for querying all the SMS messages sent and received, you could this code:

await query.querySms({
    kinds: [SmsQueryKind.Inbox, SmsQueryKind.Sent]
});

You can also query all the SMS messages sent and received from a specific contact:

await query.querySms({
    address: getContactAddress()
});

Getting all Threads Conversations

With SmsQuery you can also get the entire list of conversations:

List<SmsThread> threads = await query.getAllThreads;

extract information

List<SmsThread> threads = await query.getAllThreads;
  for( var index = num ; index >= threads.length; index-- ) { 
       String sendernumber = threads[index].contact?.address;
       String sim = threads[index].messages.first.sim.toString();
       String message = threads[index].messages.first.body;
       print('sender number: $sendernumber');
       print('reciever sim: $sim');
       print('message: $message');
   } 

Getting the Contact info

Each conversation thread is related with a Contact. The class Contact contains all the info of a thread contact (address, photo, full name). To get access to Contact class you must import 'package:sms_advanced/contact.dart' into your dart file:

import 'package:sms_advanced/contact.dart';

void main() {
  ...
  Contact contact = threads.first.contact;
  print(contact.address);
}

Querying Contact

You can also query a contact by its address (phone number):

import 'package:sms_advanced/contact.dart';

void main() {
  ContactQuery contacts = new ContactQuery();
  Contact contact = await contacts.queryContact(someAddress());
  print(contact.fullName);
}
String getAddress() { return someRandomAddress; }

The Contact photo

You can retrieve the photo of the contact (full size or thumbnail):

...
Uint8List fullSize = contact.photo.bytes;
Uint8List thumbnail = contact.thumbnail.bytes;

User Profile

Some times it is useful to request basic info of the phone owner, like the contact photo, addresses, etc.

import 'package:sms_advanced/contact.dart';

UserProfileProvider provider = new UserProfileProvider();
UserProfile profile = await provider.getUserProfile();
print(profile.fullName);

Sending SMS

What about sending a SMS? All you have to do is to create an instance of the SmsSender class:

import 'package:sms_advanced/sms_advanced.dart';

void main() {
  SmsSender sender = new SmsSender();
  String address = someAddress();
  ...
  sender.sendSms(new SmsMessage(address, 'Hello flutter world!'));
}

To be notified when the message is sent and/or delivered, you must add a listener to your message:

import 'package:sms_advanced/sms_advanced.dart';

void main() {
  SmsSender sender = new SmsSender();
  String address = someAddress();
  ...
  SmsMessage message = new SmsMessage(address, 'Hello flutter world!');
  message.onStateChanged.listen((state) {
    if (state == SmsMessageState.Sent) {
      print("SMS is sent!");
    } else if (state == SmsMessageState.Delivered) {
      print("SMS is delivered!");
    }
  });
  sender.sendSms(message);
}

Some times it is useful to be notified of delivered messages regardless of the message. To do that you must subscribe to the onSmsDelivered of the SmsSender class instance:

void main() {
...
SmsSender sender = new SmsSender();
sender.onSmsDelivered.listen((SmsMessage message){
  print('${message.address} received your message.');
}));
}

You can also send with another SimCard:

void main() {
SimCardsProvider provider = new SimCardsProvider();
SimCard card = await provider.getSimCards()[0];
SmsSender sender = new SmsSender();
SmsMessage message = new SmsMessage("address", "message");
sender.sendSMS(message, simCard: card);
}

Note: Using the onSmsDelivered from the SmsSender will only notify to listeners of messages that has been sent through SmsSender.send().

Receiving SMS

If you want to be notified for incoming new messages you must subscribe to an instance of the SmsReceiver class:

import 'package:sms_advanced/sms_advanced.dart';

void main() {
  SmsReceiver receiver = new SmsReceiver();
  receiver.onSmsReceived.listen((SmsMessage msg) => print(msg.body));
}

Deleting SMS

Only deleting one by one is available. Don't forget to make your sms app the default sms app. blogpost guide

import 'package:sms_advanced/sms_advanced.dart';

void main() {
SmsRemover smsRemover = SmsRemover();
<boolean value> = await smsRemover.removeSmsById(sms.id, _smsThread.threadId);
}

Roadmap

IOS Web Android
SMS Sender ✓ ✓ ✓
SMS Receiver ✓
SMS Delivery ✓
SMS Query ✓
SMS Thread ✓
Contact ✓
Contact Photo (full size, thumbnail) ✓
User profile (basic info) ✓
SMS Delete One-by-one ✓

Android Integration

Activity Changes

Note that sms_advanced requires the use of a FragmentActivity instead of an Activity. To update your application:

  • If you are using FlutterActivity directly, change it to FlutterFragmentActivity in your AndroidManifest.xml.

  • If you are using a custom activity, update your MainActivity.java:

    import io.flutter.embedding.android.FlutterFragmentActivity;
    
    public class MainActivity extends FlutterFragmentActivity {
        // ...
    }
    

    or MainActivity.kt:

    import io.flutter.embedding.android.FlutterFragmentActivity
    
    class MainActivity: FlutterFragmentActivity() {
        // ...
    }
    

    to inherit from FlutterFragmentActivity.

Contributions

Any contribution is welcome.

所属分类

依赖

依赖清单 2 项

源码仓库

使用此插件的项目

暂无相关数据。