DavidBalas
DavidBalas

Reputation: 333

Display only whatsapp notifications in application

I have an application that runs a service listens to notifications but it is showing all notification, I want to show only whatsapp notifications so I check if my package equals to com.whatsapp but then it's not showing nothing, code:

    private BroadcastReceiver onNotice= new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String pack = intent.getStringExtra("package");
            String title = intent.getStringExtra("title");
            String text = intent.getStringExtra("text");
        if (pack == "com.whatsapp") { 
                TableRow tr = new TableRow(getApplicationContext());
                tr.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.WRAP_CONTENT));
                TextView textview = new TextView(getApplicationContext());
                textview.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT, 1.0f));
                textview.setTextSize(20);
                textview.setTextColor(Color.parseColor("#0B0719"));
                textview.setText(Html.fromHtml(pack + "<br><b>" + title + " : </b>" + text));
                tr.addView(textview);
                tab.addView(tr);
            }
        }
    };
}

am I checking if it equals wrong?

If needed, service code:

@Override

public void onNotificationPosted(StatusBarNotification sbn) {
    String pack = sbn.getPackageName();
    String ticker = sbn.getNotification().tickerText.toString();
    Bundle extras = sbn.getNotification().extras;
    String title = extras.getString("android.title");
    String text = extras.getCharSequence("android.text").toString();
    Log.i("Package",pack);
    Log.i("Ticker",ticker);
    Log.i("Title",title);
    Log.i("Text",text);
    Intent msgrcv = new Intent("Msg");
    msgrcv.putExtra("package", pack);
    msgrcv.putExtra("ticker", ticker);
    msgrcv.putExtra("title", title);
    msgrcv.putExtra("text", text);
    LocalBroadcastManager.getInstance(context).sendBroadcast(msgrcv);
}

NOTE : when I remove the if statement,everything is working fine and it shows com.whatsapp as the package. thank you guys

Upvotes: 0

Views: 167

Answers (2)

Munir khan
Munir khan

Reputation: 5

check for package name before sending your broadcast.

if ("com.whatsapp".equals(pack)) {
  LocalBroadcastManager.getInstance(context).sendBroadcast(msgrcv);
}

Upvotes: 0

Floern
Floern

Reputation: 33904

You have to use the equals() method to compare Strings. == only compares their references, not their values.

if ("com.whatsapp".equals(pack)) {
    // ...
}

Upvotes: 1

Related Questions