Kindleデバイスでの通知のサポートに取り組んでいます。トピックを含む式stingをサーバーから受け取ります(トピックは一重引用符で囲みます)。デバイスがトピックにサブスクライブされているかどうかを確認する必要があります。サブスクライブされている場合は、論理式のトピックを「1」に置き換える必要があります。それ以外の場合は「0」に置き換える必要があります。以下は、論理式がどのように見えるかの例です。
('18' in topics || '4' in topics) && ('REG_92779' in topics && 'REG_91212' in topics)
デバイスがトピック「18」、「4」にサブスクライブされており、「REG_92779」および「REG_91212」にサブスクライブされていないと想定します。式は次のようになります
('1' || '1') && ('0' && '0')
その後、スペースと引用符を削除する必要があります。式は次のようになります
(1||1)&&(0&0)
以下はコードです
private boolean checkNotificationValid(String leString) {
// Remove the 'in topics' string. examples of logical expression as below
// ('4' in topics && '50000' in topics) || ('60' in topics || '3' in topics)
// ('4' in topics && ('50000' in topics || '60' in topics))
leString = leString.replaceAll("in topics", "");
Log.d(TAG, "Notifications : ADM : LE : Logical Expression received : " + leString);
boolean result = false;
// Find the topics in the logical expression and check whether the device is subscribed to
// the topic. If device is subscribed to the topic, replace the topic with 1 or replace the
// topic with 0. Below is the example.
// Assume device is subscribed to topics 3 and 4.
// Expression : ('4' && '50000') || ('60' || '3')
// After this block of code, it becomes : ('1' && '0') || ('0' || '1')
Pattern p = Pattern.compile("'(.*?)'");
Matcher m = p.matcher(leString);
while(m.find()) {
Log.d(TAG, "Notifications : ADM : LE : " + m.group(1));
// Check whether the device is subscribed to the topic
if(NotificationUtils.isTopicExistInSharedPref(m.group(1))) {
leString = leString.replaceFirst(m.group(1), "1");
} else {
leString = leString.replaceFirst(m.group(1), "0");
}
}
// Remove the quotes and spaces from the string, replace '||' with '|', replace '&&' with '&'
// ('1' && '0') || ('0' || '1') -> (1&0)|(0|1)
leString = leString.replaceAll("[' ]", "");
leString = leString.replaceAll("\\|\\|", "|");
leString = leString.replaceAll("&&", "&");
return result;
}
コードは正常に機能しています。ただし、トピックが「1」または「0」の場合、失敗しています。以下は例です、
('18' in topics || '4' in topics) && ('1' in topics && '1' in topics)
最初の2つのトピック(18と4)の検証後、文字列は次のようになります。
('1' || '1') && ('1' && '1')
トピック「1」について、0に置き換えようとすると(デバイスがサブスクライブされていないため)、最初の「1」が置き換えられ、3番目の「1」が置き換えられます。以下のようになります。
('0' || '1') && ('1' && '1')
なるはずです
('1' || '1') && ('0' && '1')
トピック「0」でも同じ問題が発生しています。
これは、replaceFirst()メソッドを使用したために発生しています。replace()を使おうとすると、特定のトピックを置き換えるのではなく、文字列内のすべてのオカレンスを置き換えます。誰かがこの問題を解決する方法を教えてもらえますか?
注:最初にデバイスのサブスクリプションに基づいて1または0に置き換えましたが、トピックを1または0の引用符に置き換える方法がわからなかったため、後で引用符( '4'-> '1'-> 1)を削除しました( '4'-> 1)。
代わりに使用するreplaceFirst
のString
は、使用することができますStringBuffer
し、追加置換appendReplacement
する方法をMatcher
。
コードは次のようになります。
private boolean checkNotificationValid(String leString) {
leString = leString.replaceAll("in topics", "");
Log.d(TAG, "Notifications : ADM : LE : Logical Expression received : " + leString);
boolean result = false;
StringBuffer buffer = new StringBuffer();
Pattern p = Pattern.compile("'(.*?)'");
Matcher m = p.matcher(leString);
while(m.find()) {
Log.d(TAG, "Notifications : ADM : LE : " + m.group(1));
if(NotificationUtils.isTopicExistInSharedPref(m.group(1))) {
m.appendReplacement(buffer, "1");
} else {
m.appendReplacement(buffer, "0");
}
}
m.appendTail(buffer);
String finalStr = buffer.toString();
finalStr = finalStr.replaceAll("[' ]", "");
finalStr = finalStr.replaceAll("\\|\\|", "|");
finalStr = finalStr.replaceAll("&&", "&");
return result;
}
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加