sms
Flutter 애플리케이션용 SMS 라이브러리입니다. SMS 메시지 전송, 수신, SMS 전달 상태 조회 및 연락처 정보 조회를 가능하게 합니다. Flutter에서 완전히 기능하는 SMS 앱을 개발하기 위한 간편하고 친숙한 API를 제공합니다.
플러터용 SMS 라이브러리
{"sdk":"flutter"}아래는 영문 원문 스냅샷입니다. 최신 내용은 GitHub에서 확인하세요.
xxhdpi
This is an SMS library for flutter.
It only support Android for now (I can't do it for iOS because I don't own Mac).
For help getting started with Flutter, view our online documentation.
For help on editing plugin code, view the documentation.
Once you're familiar with Flutter you may install this package adding sms (0.1.4 or higher) to the dependencies list
of the pubspec.yaml file as follow:
dependencies:
flutter:
sdk: flutter
sms: ^0.2.0
Then run the command flutter packages get on the console.
Add the import statement for sms and create an instance of the SmsQuery class:
import 'package:sms/sms.dart';
void main() {
SmsQuery query = new SmsQuery();
}
List<SmsMessage> messages = await query.getAllSms;
Note: the use of await keyword means that getAllSms is resolved asynchronously
and a Future is retorned.
The method querySms from the SmsQuery class returns a list of sms depending of the supplied parameters. For example,
for querying all the sms messages sent and received write the followed 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()
});
With SmsQuery you can also get the entire list of conversations:
List<SmsThread> threads = await query.getAllThreads;
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/contact.dart' into your dart file:
import 'package:sms/contact.dart';
void main() {
...
Contact contact = threads.first.contact;
print(contact.address);
}
You can also query a contact by its address (phone number):
import 'package:sms/contact.dart';
void main() {
ContactQuery contacts = new ContactQuery();
Contact contact = await contacts.queryContact(someAddress());
print(contact.fullName);
}
String getAddress() {...}
You can retrieve the photo of the contact (full size or thumbnail):
...
Uint8List fullSize = contact.photo.bytes;
Uint8List thumbnail = contact.thumbnail.bytes;
Some times it is useful to request basic info of the phone owner, like the contact photo, addresses, etc.
import 'package:sms/contact.dart';
UserProfileProvider provider = new UserProfileProvider();
UserProfile profile = await provider.getUserProfile();
print(profile.fullName);
What about sending a SMS? All you have to do is to create an instance of the SmsSender class:
import 'package:sms/sms.dart';
void main() {
SmsSender sender = new SmsSender();
String address = someAddress();
...
sender.sendSms(new SmsMessage(address, 'Hello flutter!'));
}
To be notified when the message is sent and/or delivered, you must add a listener to your message:
import 'package:sms/sms.dart';
void main() {
SmsSender sender = new SmsSender();
String address = someAddress();
...
SmsMessage message = new SmsMessage(address, 'Hello flutter!');
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().
If you want to be notified for incoming new messages you must subscribe to an instance of the SmsReceiver class:
import 'package:sms/sms.dart';
void main() {
SmsReceiver receiver = new SmsReceiver();
receiver.onSmsReceived.listen((SmsMessage msg) => print(msg.body));
}
Designed and created by Imrul kayes
Any contribution is welcome.