Android push (1): SMS push

Source: Internet
Author: User

In order to save power and network traffic, a mobile terminal no longer collects data by means of a training server, but notifies the terminal when the server has data to be sent to the terminal. We call this mechanism push ). The terminal receives the notification message. If the client application runs normally, it connects to the server to receive data. Even if the client program is not running, it can start the client program to receive the statement, you can also notify the user server of data changes in a certain way.

SMS push and IP push are two common push methods.

This article describes how to use SMS push.

 

Principle of Sms push:

SMS push refers to sending a binary text message to a mobile terminal to notify the terminal. The client intercepts such text messages, analyzes the data of the text message PDU, and then performs corresponding operations. Push SMS messages are carried by wap push. The content includes the header and data. The header should contain the destination port and the original port number (similar to IP packets ).

Because sending text messages requires support from the carrier, such as the mobile directmail Gateway (gegw: GPRS email gateway), it is highly dependent.

Android client intercepts SMS processing:

The client registers a broadcast receiver statically. Even if the application is not running, the broadcast receiver can intercept text messages.

 <receiver android:name=".service.receiver.MyBroadcastReceiver">
<intent-filter android:priority="1000">
<action android:name="android.intent.action.DATA_SMS_RECEIVED" />
<data android:scheme="sms" android:host="localhost" android:port="16001" />
</intent-filter>

In this way, when the push text message is sent (Listening to the data on port 16001), the system initiates intent to activate the broadcast receiver, and the broadcast receiver processes it in its Event Callback Function.

@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (TextUtils.equals(action, "android.intent.action.DATA_SMS_RECEIVED")) {
handleDataSmsReceived(context, intent);
}
}

In the handledatasmsreceived processing function, the client can obtain and parse the PDU data to learn about the changes to the server data.

The simple implementation code is as follows:

Private void handledatasmsreceived (context, intent ){
Uri uri = intent. getdata ();
Int Port = URI. getport (); // accept port 16001
SmsMessage [] msgs = getMessagesFromIntent (intent );
String senderNumber = msgs [0]. getOriginatingAddress (); // the phone number of the sender of the text message
Int type = parse (msgs) // parse the business data of the pdu and parse the data according to the protocol used to interact with the server.
Switch (type ){
// Process different situations by category
}
}

How to obtain the SMS

private static final SmsMessage[] getMessagesFromIntent(Intent intent) {
Object[] messages = (Object[]) intent.getSerializableExtra("pdus");
byte[][] pduObjs = new byte[messages.length][];

for (int i = 0; i < messages.length; i++) {
pduObjs[i] = (byte[]) messages[i];
}
byte[][] pdus = new byte[pduObjs.length][];
int pduCount = pdus.length;
SmsMessage[] msgs = new SmsMessage[pduCount];
for (int i = 0; i < pduCount; i++) {
pdus[i] = pduObjs[i];
msgs[i] = SmsMessage.createFromPdu(pdus[i]);
}
return msgs;
}

The Pdu format is not discussed in this article. You can find relevant information on the Internet. The android source code class SmsMessage also has code implementation.

Summary:

The key to android SMS push is to intercept SMS messages. Once the SMS is intercepted, you can use the features of android to complete subsequent operations, such as Push notification, Toast, and server connection requests, when the client application is offline, you can send an Intent to start the application and then process it.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.