2014-09-01 173 views
11

我正在编写一个辅助功能应用程序,帮助用户使用语音控件和通过外部辅助工具提供的控件混合使用Android进行导航。它使用MonkeyTalk Java API来完成更重的工作。
为了帮助用户了解正在发生的事情,我们还使用辅助功能服务,该服务读取通知,以便用户更快地采取行动。Facebook Messenger访问

我被告知,他们得不到任何音频线索时,一条消息在Facebook信使到达,并检查日志,我所看到的是:

D/NotificationService(2665): package com.facebook.orcaText: [] 

event.getText().size()返回0(上AccessibilityEvent事件)。
现在他们必须打开应用程序,并获取文本读取给他们,这是2个语音命令更多...
我得到所有其他通知正确。我试图从Facebook寻找关于他们对可访问性的立场的文档,但我什么也没找到。
有什么办法从他们的通知中获取文本吗?

回答

2

你可以试试这个,看看它是否与Facebook Messenger通知工作。即使这样做有效,我也会建议你等待更好的解决方案。

从API 19及以上版本开始,Notification对象携带捆绑extras - 当Notification第一次创建时,输入传递给Notification.Builder。因此,使用Notification.EXTRAS_XXXX形式的密钥可以从此Bundle中提取诸如title,context,summary等的信息。钥匙可以在这里找到:Link

在重写onAccessibilityEvent(AccessibilityEvent event)方法:

@Override 
public void onAccessibilityEvent(AccessibilityEvent event) { 
    Parcelable data = event.getParcelableData(); 

    if (data != null && data instanceof Notification) { 
     Log.i("", "We have a notification to parse"); 

     Notification notification = (Notification) data; 

     // For API 19 and above, `Notifications` carry an `extras` bundle with them 
     // From this bundle, you can extract info such as: 

     //  `EXTRA_TITLE`  - as supplied to setContentTitle(CharSequence) 
     //  `EXTRA_TEXT `  - as supplied to setContentText(CharSequence) 
     //  `EXTRA_INFO_TEXT` - as supplied to setContentInfo(CharSequence) 
     //  ... more at: http://developer.android.com/reference/android/app/Notification.html 

     Bundle b = noti.extras; 
     Log.i("Notification", "Title: " + b.get(Notification.EXTRA_TITLE)); 
     Log.i("Notification", "Text: " + b.get(Notification.EXTRA_TEXT)); 
     Log.i("Notification", "Info Text: " + b.get(Notification.EXTRA_INFO_TEXT)); 

     ///////////////////////////////////////////////////////////////// 

     // For API 18 and under: 

     // Pass `notification` to a method that parses a Notification object - See link below 

     List<String> notificationText = extractTextFromNotification(notification); 
     .... 
     .... 
    } 
} 

extractTextFromNotification(Notification)可以是一个方法从这里:Link。毋庸置疑,这是一种解决方法,并且需要进行相当多的测试才能确保其按要求运行。

+0

现在我们使用api19 +,所以它确实没关系。在未来,我们将支持旧手机,也许那时我会使用充气视图技巧,我有点喜欢! – sokie 2014-09-08 12:17:02

+1

额外的领域证明工作完美!我现在正在使用它来处理其他通知消息。谢谢! – sokie 2014-09-10 08:35:54

+0

@sokie'我现在正在使用它来处理其他通知消息。“太棒了!没想到建议你这样做。 – Vikram 2014-09-13 19:38:30

0

您可以使用getActiveNotifications方法NotificationListenerService来获取当前用户可见的通知数组。 结果是StatusBarNotification数组,这样你就可以getPackageName阅读PACKAGENAME,如果它与你正在寻找匹配的门,然后从该对象的通知内容(与getNotification)...

可以扩展NotificationListenerService类并将其注册为一个服务:

<service android:name=".NotificationListener" 
     android:label="@string/service_name" 
     android:permission="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE"> 
<intent-filter> 
    <action android:name="android.service.notification.NotificationListenerService" /> 
</intent-filter> 
</service> 

您也可以实现onNotificationPosted方法得到通知,甚至与cancelNotification法取消通知。

使用该服务的一个例子: https://github.com/kpbird/NotificationListenerService-Example

注意: 用户必须实现从 “设置>安全>通知使用权” 通知权限。 您可以打开与设置:

Intent intent=new Intent("android.settings.ACTION_NOTIFICATION_LISTENER_SETTINGS"); 
startActivity(intent); 
+0

这仍然返回空的facebook Messenger。 – sokie 2014-09-03 12:29:20

+0

请发布您使用的代码。 – semsamot 2014-09-03 12:46:28

+0

你试过这个代码与Facebook的信使?我访问任何其他通知没有问题,唯一返回空的是facebook messenger。 – sokie 2014-09-04 08:50:48