I am building an android application which will listen to whatsapp notification and store it and display it... i have used notificationlistenerService
to do it... i am able to listen all whatsapp notifications my problem is when i restart my application all data from the list is lost ;( so i want to save it in db... and how can i run my app in background i am new(noob) any help any advice appreciated
o/p
public class NotificationService extends NotificationListenerService {
Context context;
@Override
public void onCreate() {
super.onCreate();
context = getApplicationContext();
}
@Override
public void onNotificationPosted(StatusBarNotification sbn) {
String pack = sbn.getPackageName();
// String ticker = sbn.getNotification().tickerText.toString();
String ticker ="";
if(sbn.getNotification().tickerText !=null) {
ticker = sbn.getNotification().tickerText.toString();
}
Bundle extras = sbn.getNotification().extras;
String title = extras.getString("android.title");
// String text = extras.getCharSequence("android.text").toString();
int id1 = extras.getInt(Notification.EXTRA_SMALL_ICON);
Bitmap id = sbn.getNotification().largeIcon;
String text = null;
if (extras.getCharSequence("android.text") != null) {
text = extras.getCharSequence("android.text").toString();
}
if (text == null) {
if (extras.get("android.textLines") != null) {
CharSequence[] charText = (CharSequence[]) extras
.get("android.textLines");
if (charText.length > 0) {
text = charText[charText.length - 1].toString();
}
}
}
// Log.i("Package",pack);
// Log.i("Ticker",ticker);
// Log.i("Title",title);
// Log.i("Text",text);
if(pack.equals("com.whatsapp")) {
Intent msgrcv = new Intent("Msg");
// msgrcv.putExtra("package", pack);
msgrcv.putExtra("ticker", ticker);
msgrcv.putExtra("title", title);
msgrcv.putExtra("text", text);
if (id !=null) {
// if (text.equals("This message was deleted")){
// text="some messages may be deleted";
// msgrcv.putExtra("text", text);
// }
ByteArrayOutputStream stream = new ByteArrayOutputStream();
id.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();
msgrcv.putExtra("icon", byteArray);
LocalBroadcastManager.getInstance(context).sendBroadcast(msgrcv);
}
} else{
return;
}
}
@Override
public void onNotificationRemoved(StatusBarNotification sbn) {
Log.i("Msg","Notification Removed");
}
}