如何使用任何通知消息启动活动,例如" ABC"

时间:2014-03-02 10:36:12

标签: android broadcastreceiver notificationmanager

我想在收到通知消息“abc”时打开我的应用程序。我可以使用SMSReceiver执行此操作,但只能获取短信。我想做这个whatsapp消息。英国人不好意思。

@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();
    }

}

此代码为smsreceiver和toast message。

1 个答案:

答案 0 :(得分:0)

经过一些调查后,我意识到自从Facebook收购了Whatsapp后,他们关闭了所有公开的Whatsapp API,现在不可能创建Whatsapp消息接收器,但是有一件事你可以做。

  1. 创建AccessibilityService

    公共类MyAccessibilityService扩展了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. 创建serviceconfig.xml文件:

    <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. 在清单中注册服务:

    <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>

  4. 总而言之:你将能够听到所有传入的whatsapp消息,你必须尝试使用​​onAccessibilityEvent方法中的代码来从消息中检索信息。