2

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 enter image description here

enter image description here

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");
}
}
Cafer Elgin
  • 393
  • 2
  • 12
PrincEVergil
  • 89
  • 3
  • 11

1 Answers1

0

Actually, that's not a hard task to do. But, that's a long progress to understand as a beginner (as you you said you are).

I'll lead you to a sqlite database tutorial in android [tutorial link].

Basically you can use any other database, but I suggest starting from sqlite.

What the tutorial explains is how to make some kind of a wrapper (the helper class) for the sql connection.

public class DatabaseHelper extends SQLiteOpenHelper {

// Database Version
private static final int DATABASE_VERSION = 1;

// Database Name
private static final String DATABASE_NAME = "notes_db";


public DatabaseHelper(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
}

// Creating Tables
@Override
public void onCreate(SQLiteDatabase db) {

    // create notes table
    db.execSQL(Note.CREATE_TABLE);
}

// Upgrading database
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    // Drop older table if existed
    db.execSQL("DROP TABLE IF EXISTS " + Note.TABLE_NAME);

    // Create tables again
    onCreate(db);
} }

This helper class will help you interact with the local sqlite database.

Community
  • 1
  • 1
FlyingNades
  • 432
  • 3
  • 16
  • Hi @PrincEVergil if this or any answer has solved your question please consider accepting it by clicking the check-mark. This indicates to the wider community that you've found a solution and gives some reputation to both the answerer and yourself. There is no obligation to do this. – FlyingNades Apr 07 '18 at 10:14
  • hi @FlyingNades i need some more help as u say use sqlite db i try to do... but now i am facing new issue how to store Bitmap image to sqlite database [link](https://stackoverflow.com/questions/49715427/how-to-implement-sqllite-database-to-store-bitmap-image-and-text) can u plz help me – PrincEVergil Apr 08 '18 at 08:37
  • @PrincEVergil I'm at work right now, try posting a new question about your question. you can send me a message if you want me to answer there later. – FlyingNades Apr 08 '18 at 13:24