Listen to incoming Whatsapp messages/notifications

前端 未结 5 1003
谎友^
谎友^ 2020-12-07 15:09

I\'m working on a notification based app, for which I need to listen to incoming notifications. I\'ve been able to listen to incoming calls, SMS, mail etc. I have no clue ho

5条回答
  •  情书的邮戳
    2020-12-07 15:33

    See below example to catch whatsapp notifications:

    public class Notifier extends AccessibilityService {
    
    
    @Override
    public void onCreate(){
        //Toast.makeText(this,"Oncreate", Toast.LENGTH_LONG).show();
    
    }
    
    @Override
    protected void onServiceConnected() {
        // Set the type of events that this service wants to listen to.  Others
        // won't be passed to this service.
        Toast.makeText(this,"Service connected", Toast.LENGTH_LONG).show();
        AccessibilityServiceInfo info = new AccessibilityServiceInfo();
        info.feedbackType = AccessibilityServiceInfo.FEEDBACK_ALL_MASK;;
        info.eventTypes = AccessibilityEvent.TYPE_NOTIFICATION_STATE_CHANGED ;
    
        // If you only want this service to work with specific applications, set their
        // package names here.  Otherwise, when the service is activated, it will listen
        // to events from all applications.
        info.packageNames = new String[] {"com.whatsapp"};
        info.notificationTimeout = 100;
    
        setServiceInfo(info);
    
    }
    
    @Override
    public void onAccessibilityEvent(AccessibilityEvent event) {
    
        if(event.getEventType() == AccessibilityEvent.TYPE_NOTIFICATION_STATE_CHANGED) {
    
                Toast.makeText(this,"Notification Catched", Toast.LENGTH_LONG).show();
            }
    
        }
    }
    

    And don't forget to set permission from settings>Accessibility in order to access the system events. Allow permission from settings .

    check this link

    accessibility service is not started

提交回复
热议问题