Monday, July 19, 2010

SMS notifications in Android

SMSNotify.java
================

public class SMSNotify extends Activity {

public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);

NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
nm.cancel(R.string.app_name); // cancel notification
}
}
================================================
public class SMSNotifyExample extends IntentReceiver {
/** TAG used for Debug-Logging */
private static final String LOG_TAG = “SMSReceiver”;
// public static final int LENGTH_LONG = 10;
/* A Random IDs used for the Notification */
public static final int NOTIFICATION_ID_RECEIVED = 0×1221;

/** The Action fired by the Android-System when a SMS was received.
* We are using the Default Package-Visibility */
static final String ACTION = “android.provider.Telephony.SMS_RECEIVED”;
private CharSequence from = null;
private CharSequence tickerMessage = null;
public void onReceiveIntent(Context context, Intent intent) {

NotificationManager nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
if (intent.getAction().equals(ACTION)) {
// if(message starts with SMStretcher recognize BYTE)
StringBuilder sb = new StringBuilder();

Bundle bundle = intent.getExtras();
if (bundle != null) {
/* Get all messages contained in the Intent*/
SmsMessage[] messages =
Telephony.Sms.Intents.getMessagesFromIntent(intent);

/* Feed the StringBuilder with all Messages found. */
for (SmsMessage currentMessage : messages){
sb.append(“Received compressed SMS\nFrom: “);
/* Sender-Number */
sb.append(currentMessage.getDisplayOriginatingAddress());
// set
from = currentMessage.getDisplayOriginatingAddress();
sb.append(“\n—-Message—-\n”);
/* Actual Message-Content */
sb.append(currentMessage.getDisplayMessageBody());
}
}
/* Logger Debug-Output */
Log.i(LOG_TAG, “[SMSApp] onReceiveIntent: ” + sb);

this.abortBroadcast();
/* Start the Main-Activity */
Intent i = new Intent(context, SMSNotifyActivity.class);
i.setLaunchFlags(Intent.NEW_TASK_LAUNCH);
context.startActivity(i);

// CharSequence from = currentMessage.getDisplayOriginatingAddress();
CharSequence appName = “SMSNotifyExample”;
tickerMessage = sb.toString();
Long theWhen = System.currentTimeMillis();

Intent appIntent = new Intent(Intent.VIEW_ACTION, Sms.Inbox.CONTENT_URI);

Notification notif = new Notification(
context, // our context
R.drawable.incoming, // the icon for the status bar
tickerMessage, // the text to display in the ticker
theWhen, // the timestamp for the notification
from, // the title for the notification
tickerMessage, // the details to display in the notification
i, // the contentIntent (see above)
R.drawable.chat, // the app icon
appName, // the name of the app
appIntent); // intent that shows the inbox when you click on icon

notif.vibrate = new long[] { 100, 250, 100, 500};

nm.notify(R.string.alert_message, notif);

}
}
}
AndroidManifest.xml
=====================




No comments:

Post a Comment