通知监听的实现

目标:本应用想监听其他应用弹出的通知信息
实现:

  1. 继承NotificationListenerService
package com.itant.payhelper;

import android.app.Notification;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.os.IBinder;
import android.service.notification.NotificationListenerService;
import android.service.notification.StatusBarNotification;
import android.util.Log;

/**
 * 使用注意事项:你可以直接debug运行,然后可以监听到通知。但是,如果你是先run,然后开启通知权限,然后再debug,就监听不到了。
 */
public class AlipayNotificationListenerService extends NotificationListenerService {
    public AlipayNotificationListenerService() {

    }

    @Override
    public void onNotificationRemoved(StatusBarNotification sbn) {
        super.onNotificationRemoved(sbn);
    }

    @Override
    public IBinder onBind(Intent intent) {
        return super.onBind(intent);
    }

    @Override
    public void onNotificationPosted(StatusBarNotification sbn) {
        super.onNotificationPosted(sbn);
        // 这里可以拿到包名,可以按照需要判断。
        String packageName = sbn.getPackageName().toLowerCase();
        Notification notification = sbn.getNotification();
        if (notification == null) {
            return;
        }
        if (Build.VERSION.SDK_INT >= 19 && packageName.contains("alipay")) {
            Bundle extras = notification.extras;
            if (extras != null) {
                // 这里是具体的title和content,可以从中提取金额
                String title = extras.getString(Notification.EXTRA_TITLE, "");
                String content = extras.getString(Notification.EXTRA_TEXT, "");
                // todo 发送请求给服务器,告诉服务器,当前列表里的用户支付成功,发送推送
            }
        }
    }

    @Override
    public void onListenerConnected() {
        Log.e("shiwushu_alipay","失物树已连接监听");
    }

    @Override
    public void onListenerDisconnected() {
        super.onListenerDisconnected();
    }
}
  1. MainActivity里判断是否拥有通知权限,没有的话,必须要先授权
package com.itant.payhelper;

import android.content.Intent;
import android.os.Bundle;
import android.provider.Settings;
import android.support.v7.app.AppCompatActivity;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    private TextView tv_test;
    private boolean payListenerStarted;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        tv_test = findViewById(R.id.tv_test);
        if (Settings.Secure.getString(this.getContentResolver(), "enabled_notification_listeners").contains(getApplicationContext().getPackageName())) {
            //Add the code to launch the NotificationService Listener here.
            tv_test.setText("已经拥有获取通知的权限");
            if (!payListenerStarted) {
                startService(new Intent(this, AlipayNotificationListenerService.class));
                payListenerStarted = true;
            }
        } else {
            //Launch notification access in the settings...
            tv_test.setText("请设置通知权限");
            startActivity(new Intent("android.settings.ACTION_NOTIFICATION_LISTENER_SETTINGS"));
        }

    }

    @Override
    protected void onResume() {
        super.onResume();

    }
}
  1. 最后别忘了在清单文件配置service
<service
            android:name=".AlipayNotificationListenerService"
            android:permission="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE">
            <intent-filter>
                <action android:name="android.service.notification.NotificationListenerService" />
            </intent-filter>
        </service>

猜你喜欢

转载自blog.csdn.net/ithouse/article/details/82979529