I want to open my application when I get notification message "abc". I can do this with SMSReceiver but only get sms message. I want do this whatsapp message. sory for bad English.

@Override
public void onReceive(Context context, Intent intent) {

    String hangiNumaradan = "";
    String neYazmis = "";

    Bundle bundle = intent.getExtras();
    SmsMessage[] msgs = null;


    if (bundle != null) {

        Object[] pdus = (Object[]) bundle.get("pdus");
        msgs = new SmsMessage[pdus.length];
        for (int i = 0; i < msgs.length; i++) {
            msgs[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);
            hangiNumaradan += msgs[i].getOriginatingAddress();
            neYazmis += msgs[i].getMessageBody().toString();
        }


        Toast.makeText(context, hangiNumaradan + " gelen mesaj " + neYazmis, Toast.LENGTH_LONG).show();
    }

}

This code is smsreceiver and toast message.

有帮助吗?

解决方案

After doing some investigation I realized that since Facebook bought Whatsapp, they closed all public Whatsapp API and right now it's impossible to create Whatsapp message receiver, BUT there is one thing that you can do.

  1. Create AccessibilityService:

    public class MyAccessibilityService extends AccessibilityService {

    @Override
    public void onAccessibilityEvent(AccessibilityEvent event) {
        //here you can implement your reaction for incoming notification
        //here you can find some tags that could be helpful for you
        String helpful = event.getContentDescription();
    
        //In code below you could also retrieve some helpful info from Notification
        AccessibilityNodeInfo source = event.getSource();
            if (source == null) {
                return;
            }
    
            // Grab the parent of the view that fired the event.
            AccessibilityNodeInfo rowNode = getListItemNodeInfo(source);
            if (rowNode == null) {
                return;
            }
    
            // Using this parent, get references to both child nodes, the label and the checkbox.
            AccessibilityNodeInfo labelNode = rowNode.getChild(0);
            if (labelNode == null) {
                rowNode.recycle();
                return;
            }
    
            AccessibilityNodeInfo completeNode = rowNode.getChild(1);
            if (completeNode == null) {
                rowNode.recycle();
                return;
            }
    
            // Determine what the task is and whether or not it's complete, based on
            // the text inside the label, and the state of the check-box.
            if (rowNode.getChildCount() < 2 || !rowNode.getChild(1).isCheckable()) {
                rowNode.recycle();
                return;
            }
    
            CharSequence anotherInfoFromNotification = labelNode.getText();
    
    }
    
    @Override
    public void onInterrupt() {
    }
    

    }

  2. Create serviceconfig.xml file:

    <accessibility-service android:accessibilityEventTypes="typeViewClicked|typeViewFocused" android:packageNames="com.whatsapp" android:accessibilityFeedbackType="feedbackSpoken" android:notificationTimeout="100" android:settingsActivity="com.example.android.your.way.to.activity" android:canRetrieveWindowContent="true"/>

  3. Register service in manifest:

    <service android:name=".MyAccessibilityService"> <intent-filter> <action android:name="android.accessibilityservice.AccessibilityService" /> </intent-filter> <meta-data android:name="android.accessibilityservice" android:resource="@xml/serviceconfig" /> </service>

So to summ it up: you will be able to get listener to all incoming whatsapp messages, you have to try little bit more with code in onAccessibilityEvent method to retrive info from message.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top