-
Notifications
You must be signed in to change notification settings - Fork 2.3k
fix: [Firebase message][Android] issue open old notify #4203
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
d00de61
[Firebase message] - Fix issue open old notify
beb1ee4
- fix store firebase remote message
ffe22a6
- removed un-used import
e1f7755
+ update logic limit size of notifications
64360a6
- update logic store and remove old remote message
vannt1991 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
129 changes: 129 additions & 0 deletions
129
packages/messaging/android/src/main/java/io/invertase/firebase/messaging/JsonConvert.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
package io.invertase.firebase.messaging; | ||
|
||
import com.facebook.react.bridge.Arguments; | ||
import com.facebook.react.bridge.ReadableArray; | ||
import com.facebook.react.bridge.ReadableMap; | ||
import com.facebook.react.bridge.ReadableMapKeySetIterator; | ||
import com.facebook.react.bridge.ReadableType; | ||
import com.facebook.react.bridge.WritableArray; | ||
import com.facebook.react.bridge.WritableMap; | ||
|
||
import org.json.JSONArray; | ||
import org.json.JSONException; | ||
import org.json.JSONObject; | ||
|
||
import java.util.Iterator; | ||
|
||
public abstract class JsonConvert { | ||
public static JSONObject reactToJSON(ReadableMap readableMap) throws JSONException { | ||
JSONObject jsonObject = new JSONObject(); | ||
ReadableMapKeySetIterator iterator = readableMap.keySetIterator(); | ||
while(iterator.hasNextKey()){ | ||
String key = iterator.nextKey(); | ||
ReadableType valueType = readableMap.getType(key); | ||
switch (valueType){ | ||
case Null: | ||
jsonObject.put(key,JSONObject.NULL); | ||
break; | ||
case Boolean: | ||
jsonObject.put(key, readableMap.getBoolean(key)); | ||
break; | ||
case Number: | ||
try { | ||
jsonObject.put(key, readableMap.getInt(key)); | ||
} catch(Exception e) { | ||
jsonObject.put(key, readableMap.getDouble(key)); | ||
} | ||
break; | ||
case String: | ||
jsonObject.put(key, readableMap.getString(key)); | ||
break; | ||
case Map: | ||
jsonObject.put(key, reactToJSON(readableMap.getMap(key))); | ||
break; | ||
case Array: | ||
jsonObject.put(key, reactToJSON(readableMap.getArray(key))); | ||
break; | ||
} | ||
} | ||
|
||
return jsonObject; | ||
} | ||
|
||
public static JSONArray reactToJSON(ReadableArray readableArray) throws JSONException { | ||
JSONArray jsonArray = new JSONArray(); | ||
for(int i=0; i < readableArray.size(); i++) { | ||
ReadableType valueType = readableArray.getType(i); | ||
switch (valueType){ | ||
case Null: | ||
jsonArray.put(JSONObject.NULL); | ||
break; | ||
case Boolean: | ||
jsonArray.put(readableArray.getBoolean(i)); | ||
break; | ||
case Number: | ||
try { | ||
jsonArray.put(readableArray.getInt(i)); | ||
} catch(Exception e) { | ||
jsonArray.put(readableArray.getDouble(i)); | ||
} | ||
break; | ||
case String: | ||
jsonArray.put(readableArray.getString(i)); | ||
break; | ||
case Map: | ||
jsonArray.put(reactToJSON(readableArray.getMap(i))); | ||
break; | ||
case Array: | ||
jsonArray.put(reactToJSON(readableArray.getArray(i))); | ||
break; | ||
} | ||
} | ||
return jsonArray; | ||
} | ||
|
||
public static WritableMap jsonToReact(JSONObject jsonObject) throws JSONException { | ||
WritableMap writableMap = Arguments.createMap(); | ||
Iterator iterator = jsonObject.keys(); | ||
while(iterator.hasNext()) { | ||
String key = (String) iterator.next(); | ||
Object value = jsonObject.get(key); | ||
if (value instanceof Float || value instanceof Double) { | ||
writableMap.putDouble(key, jsonObject.getDouble(key)); | ||
} else if (value instanceof Number) { | ||
writableMap.putInt(key, jsonObject.getInt(key)); | ||
} else if (value instanceof String) { | ||
writableMap.putString(key, jsonObject.getString(key)); | ||
} else if (value instanceof JSONObject) { | ||
writableMap.putMap(key,jsonToReact(jsonObject.getJSONObject(key))); | ||
} else if (value instanceof JSONArray){ | ||
writableMap.putArray(key, jsonToReact(jsonObject.getJSONArray(key))); | ||
} else if (value == JSONObject.NULL){ | ||
writableMap.putNull(key); | ||
} | ||
} | ||
|
||
return writableMap; | ||
} | ||
|
||
public static WritableArray jsonToReact(JSONArray jsonArray) throws JSONException { | ||
WritableArray writableArray = Arguments.createArray(); | ||
for(int i=0; i < jsonArray.length(); i++) { | ||
Object value = jsonArray.get(i); | ||
if (value instanceof Float || value instanceof Double) { | ||
writableArray.pushDouble(jsonArray.getDouble(i)); | ||
} else if (value instanceof Number) { | ||
writableArray.pushInt(jsonArray.getInt(i)); | ||
} else if (value instanceof String) { | ||
writableArray.pushString(jsonArray.getString(i)); | ||
} else if (value instanceof JSONObject) { | ||
writableArray.pushMap(jsonToReact(jsonArray.getJSONObject(i))); | ||
} else if (value instanceof JSONArray){ | ||
writableArray.pushArray(jsonToReact(jsonArray.getJSONArray(i))); | ||
} else if (value == JSONObject.NULL){ | ||
writableArray.pushNull(); | ||
} | ||
} | ||
return writableArray; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 11 additions & 0 deletions
11
...roid/src/main/java/io/invertase/firebase/messaging/ReactNativeFirebaseMessagingStore.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package io.invertase.firebase.messaging; | ||
|
||
import com.google.firebase.messaging.RemoteMessage; | ||
|
||
public interface ReactNativeFirebaseMessagingStore { | ||
void storeFirebaseMessage(RemoteMessage remoteMessage); | ||
|
||
RemoteMessage getFirebaseMessage(String remoteMessageId); | ||
|
||
void clearFirebaseMessage(String remoteMessageId); | ||
} |
23 changes: 23 additions & 0 deletions
23
...rc/main/java/io/invertase/firebase/messaging/ReactNativeFirebaseMessagingStoreHelper.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package io.invertase.firebase.messaging; | ||
|
||
public class ReactNativeFirebaseMessagingStoreHelper { | ||
|
||
private ReactNativeFirebaseMessagingStore messagingStore; | ||
|
||
private ReactNativeFirebaseMessagingStoreHelper() { | ||
messagingStore = new ReactNativeFirebaseMessagingStoreImpl(); | ||
} | ||
|
||
private static ReactNativeFirebaseMessagingStoreHelper _instance; | ||
|
||
public static ReactNativeFirebaseMessagingStoreHelper getInstance() { | ||
if (_instance == null) { | ||
_instance = new ReactNativeFirebaseMessagingStoreHelper(); | ||
} | ||
return _instance; | ||
} | ||
|
||
public ReactNativeFirebaseMessagingStore getMessagingStore() { | ||
return messagingStore; | ||
} | ||
} |
86 changes: 86 additions & 0 deletions
86
.../src/main/java/io/invertase/firebase/messaging/ReactNativeFirebaseMessagingStoreImpl.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
package io.invertase.firebase.messaging; | ||
|
||
import com.facebook.react.bridge.WritableMap; | ||
import com.google.firebase.messaging.RemoteMessage; | ||
|
||
import org.json.JSONException; | ||
import org.json.JSONObject; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
|
||
import io.invertase.firebase.common.UniversalFirebasePreferences; | ||
|
||
import static io.invertase.firebase.messaging.JsonConvert.jsonToReact; | ||
import static io.invertase.firebase.messaging.JsonConvert.reactToJSON; | ||
import static io.invertase.firebase.messaging.ReactNativeFirebaseMessagingSerializer.remoteMessageFromReadableMap; | ||
import static io.invertase.firebase.messaging.ReactNativeFirebaseMessagingSerializer.remoteMessageToWritableMap; | ||
|
||
public class ReactNativeFirebaseMessagingStoreImpl implements ReactNativeFirebaseMessagingStore { | ||
|
||
private static final String S_KEY_ALL_NOTIFICATION_IDS = "all_notification_ids"; | ||
private static final int MAX_SIZE_NOTIFICATIONS = 100; | ||
private final String DELIMITER = ","; | ||
|
||
@Override | ||
public void storeFirebaseMessage(RemoteMessage remoteMessage) { | ||
try { | ||
String remoteMessageString = reactToJSON(remoteMessageToWritableMap(remoteMessage)).toString(); | ||
// Log.d("storeFirebaseMessage", remoteMessageString); | ||
UniversalFirebasePreferences preferences = UniversalFirebasePreferences.getSharedInstance(); | ||
preferences.setStringValue(remoteMessage.getMessageId(), remoteMessageString); | ||
// save new notification id | ||
String notifications = preferences.getStringValue(S_KEY_ALL_NOTIFICATION_IDS, ""); | ||
notifications += remoteMessage.getMessageId() + DELIMITER; // append to last | ||
|
||
// check and remove old notifications message | ||
List<String> allNotificationList = convertToArray(notifications); | ||
if (allNotificationList.size() > MAX_SIZE_NOTIFICATIONS) { | ||
String firstRemoteMessageId = allNotificationList.get(0); | ||
preferences.remove(firstRemoteMessageId); | ||
notifications = removeRemoteMessage(firstRemoteMessageId, notifications); | ||
} | ||
preferences.setStringValue(S_KEY_ALL_NOTIFICATION_IDS, notifications); | ||
} catch (JSONException e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
|
||
@Override | ||
public RemoteMessage getFirebaseMessage(String remoteMessageId) { | ||
String remoteMessageString = UniversalFirebasePreferences.getSharedInstance().getStringValue(remoteMessageId, null); | ||
if (remoteMessageString != null) { | ||
// Log.d("getFirebaseMessage", remoteMessageString); | ||
try { | ||
WritableMap readableMap = jsonToReact(new JSONObject(remoteMessageString)); | ||
readableMap.putString("to", remoteMessageId);//fake to | ||
return remoteMessageFromReadableMap(readableMap); | ||
} catch (JSONException e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
return null; | ||
} | ||
|
||
@Override | ||
public void clearFirebaseMessage(String remoteMessageId) { | ||
UniversalFirebasePreferences preferences = UniversalFirebasePreferences.getSharedInstance(); | ||
preferences.remove(remoteMessageId); | ||
// check and remove old notifications message | ||
String notifications = preferences.getStringValue(S_KEY_ALL_NOTIFICATION_IDS, ""); | ||
if (!notifications.isEmpty()) { | ||
notifications = removeRemoteMessage(remoteMessageId, notifications); // remove from list | ||
preferences.setStringValue(S_KEY_ALL_NOTIFICATION_IDS, notifications); | ||
} | ||
} | ||
|
||
private String removeRemoteMessage(String remoteMessageId, String notifications) { | ||
return notifications.replace(remoteMessageId + DELIMITER, ""); | ||
} | ||
|
||
private List<String> convertToArray(String string) { | ||
return new ArrayList<>(Arrays.asList(string.split(DELIMITER))); | ||
} | ||
|
||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.