0

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.

Kara
  • 6,115
  • 16
  • 50
  • 57
Ahmet Atıcı
  • 13
  • 1
  • 4
  • If I clearly understand you then you want to open activity when you click on your notification in notification bar, right ? – bgplaya Mar 02 '14 at 11:00
  • you are not right. my phone starts my program automatically when it take notification. for example. phone takes whatsapp message in notification, automatically starts my activity. thanks for your comment – Ahmet Atıcı Mar 02 '14 at 11:58
  • Okey, but I'm still missing what do you want to achive, if you provide little bit more information, then maybe I could help you. – bgplaya Mar 02 '14 at 12:02
  • okay :) you sent whatsapp message to me "brother" my phone look notification, if your whatsapp message is brother, open my activity and write "brother" in edittext. did you get it ? – Ahmet Atıcı Mar 02 '14 at 12:42
  • A got you, read the answer in 5mins. – bgplaya Mar 02 '14 at 13:37

1 Answers1

0

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.

bgplaya
  • 1,105
  • 15
  • 27