我正在尝试从我的应用程序读取IM应用程序的通知。
我已经正确实施了NotificationListenerService
。它为我提供了几乎所有IM应用程序(Whatsapp,Line,Hike,Viber,Instagram,Facebook Lite甚至Gmail邮件)的通知日志。但是由于某些原因,当我在环聊中收到新消息的通知时,onNotificationPosted
未触发。
这是我的服务和清单文件的相关代码:
.
.
public TrackMyNotificationsService() {
}
@Override
public IBinder onBind(Intent intent) {
// TODO: Return the communication channel to the service.
IBinder mIBinder = super.onBind(intent);
Log.i(TAG, "onBind");
return mIBinder;
}
@Override
public void onNotificationPosted(StatusBarNotification sbn) {
super.onNotificationPosted(sbn);
if (sbn != null) {
Bundle extras = sbn.getNotification().extras;
String key = sbn.getKey();
String appName = getAppName(sbn.getPackageName());
//Here goes logic for saving and logging based on application name
.....
}
}
@Override
public boolean onUnbind(Intent intent) {
Log.i(TAG, "onUnBind");
return super.onUnbind(intent);
}
@Override
public void onNotificationRemoved(StatusBarNotification sbn) {
super.onNotificationRemoved(sbn);
}
public String getAppName(String packageName){
String appName = packageName;
try{
ApplicationInfo applicationInfo = getPackageManager().getApplicationInfo(packageName, PackageManager.GET_META_DATA);
appName = (String) getPackageManager().getApplicationLabel(applicationInfo);
}catch(Exception e){
e.printStackTrace();
}
return appName;
}
清单:
<service
android:name=".TrackMyNotificationsService"
android:enabled="true"
android:exported="true"
android:permission="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE">
<intent-filter>
<action android:name="android.service.notification.NotificationListenerService" />
</intent-filter>
</service>
我尝试重新启动设备。环聊通知已开启。 (我将它们放入设备中),但是没有onNotificationPosted
被调用。有帮助吗?