Android中监听通知消息的实现

由于自动化测试需要,我们希望能够监听Android手机中的推送消息来实现发送推送后的客户端自动检查。下面就来看看我们是如何实现客户端统计消息接收的。

大致思路是在Android手机上安装一个程序,这个程序可以监听手机接收到的消息,然后打印出来,比如打印到logcat。经查,如果要实现这个功能,就要用到NotificationListenerService。

先看主程序:

package com.xingshulin.xsltestcontroller;

import android.support.v7.app.ActionBarActivity;
import android.text.TextUtils;
import android.content.ComponentName;
import android.content.Intent;
import android.os.Bundle;
import android.provider.Settings;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        // 通知栏监控器开关
        Button notificationMonitorOnBtn = (Button)findViewById(R.id.notification_monitor_on_btn);
        notificationMonitorOnBtn.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                // 
                if (!isEnabled()) {
                    startActivity(new Intent("android.settings.ACTION_NOTIFICATION_LISTENER_SETTINGS"));
                } else {
                    Toast toast = Toast.makeText(getApplicationContext(), "监控器开关已打开", Toast.LENGTH_SHORT);
                    toast.show();
                }
            }
        });
        
        Button notificationMonitorOffBtn = (Button)findViewById(R.id.notification_monitor_off_btn);
        notificationMonitorOffBtn.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                // 
                if (isEnabled()) {
                    startActivity(new Intent("android.settings.ACTION_NOTIFICATION_LISTENER_SETTINGS"));
                } else {
                    Toast toast = Toast.makeText(getApplicationContext(), "监控器开关已关闭", Toast.LENGTH_SHORT);
                    toast.show();
                }
            }
        });
    }
    
    // 判断是否打开了通知监听权限
    private boolean isEnabled() {
        String pkgName = getPackageName();
        final String flat = Settings.Secure.getString(getContentResolver(), "enabled_notification_listeners");
        if (!TextUtils.isEmpty(flat)) {
            final String[] names = flat.split(":");
            for (int i = 0; i < names.length; i++) {
                final ComponentName cn = ComponentName.unflattenFromString(names[i]);
                if (cn != null) {
                    if (TextUtils.equals(pkgName, cn.getPackageName())) {
                        return true;
                    }
                }
            }
        }
        return false;
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

}

主程序很简单,就是安排两个按键,一个用来打开通知消息监听,一个用来关闭。点击打开监听按键后,如果之前并没有为APP打开过通知消息监听的权限,会跳转到通知消息监听权限界面,有个按钮会让你能打开权限。

Screenshot_2016-09-18-14-49-29.jpeg

实现就是程序中的:

if (!isEnabled()) { 
       startActivity(new Intent("android.settings.ACTION_NOTIFICATION_LISTENER_SETTINGS"));
 }

isEnabled()方法是用来判断通知消息监听权限是否已经打开了。方法中的"enabled_notification_listeners"为数据库字段,我们可以通过它的值来判断我们的APP是否有通知使用权。

下面是关键的NotificationListenerService 的实现:

package com.xingshulin.xsltestcontroller;

import android.annotation.SuppressLint;
import android.app.Notification;
import android.os.Bundle;
import android.service.notification.NotificationListenerService;
import android.service.notification.StatusBarNotification;
import android.util.Log;

@SuppressLint("NewApi")
public class NotificationMonitorService extends NotificationListenerService {

    // 在收到消息时触发
    @Override
    public void onNotificationPosted(StatusBarNotification sbn) {
        // TODO Auto-generated method stub
        Bundle extras = sbn.getNotification().extras;
        // 获取接收消息APP的包名
        String notificationPkg = sbn.getPackageName();
        // 获取接收消息的抬头
        String notificationTitle = extras.getString(Notification.EXTRA_TITLE);
        // 获取接收消息的内容
        String notificationText = extras.getString(Notification.EXTRA_TEXT);
        Log.i("XSL_Test", "Notification posted " + notificationTitle + " & " + notificationText);
    }
    
    // 在删除消息时触发
    @Override
    public void onNotificationRemoved(StatusBarNotification sbn) {
        // TODO Auto-generated method stub
        Bundle extras = sbn.getNotification().extras;
        // 获取接收消息APP的包名
        String notificationPkg = sbn.getPackageName();
        // 获取接收消息的抬头
        String notificationTitle = extras.getString(Notification.EXTRA_TITLE);
        // 获取接收消息的内容
        String notificationText = extras.getString(Notification.EXTRA_TEXT);
        Log.i("XSL_Test", "Notification removed " + notificationTitle + " & " + notificationText);
    }
}

有两个需要Override的方法,onNotificationPosted和onNotificationRemoved。
onNotificationPosted是在接收到通知消息时触发
onNotificationRemoved是在通知消息被删除时触发,比如通知栏中左划或右划消息。
在方法中我们可以获取几乎所有消息的信息,如APP的包名,抬头和消息内容等。这些信息恰恰是我们做测试时需要判断的。

最后是AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.xingshulin.xsltestcontroller"
    android:versionCode="1"
    android:versionName="1.0" >
    
    <uses-permission android:name="android.permission.WAKE_LOCK" />
    <uses-permission android:name="android.permission.DISABLE_KEYGUARD"/>

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="21" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
       
        <service android:name=".NotificationMonitorService" 
            android:label="NotificationMonitor"
            android:permission="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE">
            <intent-filter>
                <action android:name="android.service.notification.NotificationListenerService" />
            </intent-filter>
        </service>
        
    </application>
</manifest>

这样,我们就可以通过这个APP获取通知消息的动作了,当然,我们同样也就可以只获得我们需要APP的消息了(通过在NotificationMonitorService 中过滤接收消息APP的包名):

1.png

 

  • 2
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
Android,可以通过监听状态栏通知来获取通知的相关信息。在实现这个功能时,需要创建一个继承自NotificationListenerService的类,并重写onNotificationPosted和onNotificationRemoved方法。其,onNotificationPosted方法用于在通知被发布时触发,而onNotificationRemoved方法用于在通知被移除时触发。 在具体的实现,可以通过StatusBarNotification对象来获取通知的详细信息,例如通过sbn.getNotification()可以获取通知内容。 为了使应用具备监听状态栏通知的功能,还需要在AndroidManifest.xml文件进行相应的配置。需要在<manifest>标签内添加一个<service>标签,并设置android:name为你创建的NotificationListenerService的类名。同时,需要添加一个<intent-filter>标签,并设置action为"android.service.notification.NotificationListenerService"。最后,还需要添加android:permission="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE"以获取相应的权限。 通过以上的步骤,你就可以在Android监听状态栏通知并获取相应的信息了。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [android监听Notification](https://blog.csdn.net/zhaicaixiansheng/article/details/47279251)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *2* *3* [Android 4.4 KitKat NotificationManagerService使用详解与原理分析(一)__使用详解](https://blog.csdn.net/yihongyuelan/article/details/40977323)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值