2014-09-01 172 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