Как получить контактный номер WhatsApp с помощью NotificationListenerService android? - PullRequest
0 голосов
/ 02 августа 2020

Я пытаюсь разработать приложение android, которое обнаруживает входящие звонки по телефонии и Whatsapp и извлекает контакт / номер телефона вызывающего абонента. Я могу обнаруживать телефонные звонки с помощью BroadcastReceiver.

public class MyPhoneReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        Log.w("Caller", "Called");
        Bundle extras = intent.getExtras();
        if (extras != null) {
            String state = extras.getString(TelephonyManager.EXTRA_STATE);
            if (state.equals(TelephonyManager.EXTRA_STATE_RINGING)) {
                String phoneNumber = extras
                        .getString(TelephonyManager.EXTRA_INCOMING_NUMBER);
                Log.w("Caller", phoneNumber);
            }
        }
    }
}

Проблема в том, что я не могу получать звонки Whatsapp с помощью этого кода. Я пробовал NotificationListenerService. При каждом входящем звонке в Whatsapp отправляется уведомление. Я могу обнаруживать входящие звонки, но могу получить контактную информацию из отправленного уведомления. Вот мой код для NotificationListenerService

public class NotificationReceiver extends NotificationListenerService {

@Override
public void onNotificationPosted(StatusBarNotification sbn) {
    if(sbn.getPackageName().equals("com.gbwhatsapp"))
    {
        Log.e("Called", "Sbn Key : "+ sbn.getKey());
        Bundle bundle = sbn.getNotification().extras;
        if (bundle != null) {
            for (String key : bundle.keySet()) {
                Log.e("Called", key + " : " + (bundle.get(key) != null ? bundle.get(key) : "NULL"));
            }
        }

    }
}

}

1 Ответ

0 голосов
/ 18 августа 2020

это работает для меня:

//"com.whatsapp"; // Pessoal    
//"com.whatsapp.w4b"; // Bussines

if (sbn.getPackageName().equals("com.whatsapp")){ 
    String title = sbn.getNotification().extras.getString(Notification.EXTRA_TITLE);
    String text = sbn.getNotification().extras.getCharSequence(Notification.EXTRA_TEXT).toString();    
}
...