-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
function-MEITUAN-module:Android原生向JavaScript发送消息1:通过createNativeModul…
…s创建HomeMessageEventModule用于原生端向服务端发送实时消息。注意:发送消息主要通过ReactContext的实例类完成,理论上能提高该实例的地方都具备通知JavaScript的功能 Signed-off-by: chenyabin1 <“[email protected]”>
- Loading branch information
chenyabin1
committed
Dec 11, 2019
1 parent
1741069
commit 8edaf1e
Showing
7 changed files
with
222 additions
and
99 deletions.
There are no files selected for viewing
This file contains 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
97 changes: 97 additions & 0 deletions
97
android/app/src/main/java/com/common/react/HomeMessageEventModule.java
This file contains 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,97 @@ | ||
package com.common.react; | ||
|
||
|
||
import com.facebook.react.bridge.Arguments; | ||
import com.facebook.react.bridge.ReactApplicationContext; | ||
import com.facebook.react.bridge.ReactContextBaseJavaModule; | ||
import com.facebook.react.bridge.WritableArray; | ||
import com.facebook.react.bridge.WritableMap; | ||
import com.facebook.react.modules.core.DeviceEventManagerModule; | ||
|
||
import org.jetbrains.annotations.Nullable; | ||
|
||
import java.util.Map; | ||
|
||
/** | ||
* Created by Edwin,CHEN on 2019/12/11. | ||
*/ | ||
|
||
public class HomeMessageEventModule extends ReactContextBaseJavaModule { | ||
|
||
private static ReactApplicationContext reactApplicationContext; | ||
|
||
private static HomeMessageEventModule homeMessageEventModule; | ||
|
||
private String eventName; | ||
|
||
private WritableMap params = Arguments.createMap(); | ||
|
||
public static HomeMessageEventModule newInstance() { | ||
return new HomeMessageEventModule(reactApplicationContext); | ||
} | ||
|
||
public HomeMessageEventModule(ReactApplicationContext reactContext) { | ||
super(reactContext); | ||
this.reactApplicationContext = reactContext; | ||
} | ||
|
||
/** | ||
* 发送本地时间给JS,相应的JavaScript代码要做好监听 | ||
* <p> | ||
* WritableMap params = Arguments.createMap(); | ||
* params.putString("eventProperty", "someValue"); | ||
* String eventName = "EventReminder"; | ||
* sendEvent(reactContext, eventName, params); | ||
* </p> | ||
* @param eventName | ||
* @param params | ||
*/ | ||
public void sendEventToJs(String eventName, | ||
@Nullable WritableMap params) { | ||
if (reactApplicationContext != null) { | ||
reactApplicationContext | ||
.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class) | ||
.emit(eventName, params); | ||
} | ||
} | ||
|
||
public void sendEventToJs() { | ||
if (reactApplicationContext != null) { | ||
reactApplicationContext | ||
.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class) | ||
.emit(eventName, params); | ||
} | ||
} | ||
|
||
|
||
public HomeMessageEventModule putEventName(String eventName) { | ||
this.eventName = eventName; | ||
return this; | ||
} | ||
|
||
public HomeMessageEventModule putParam(String key, Object value) { | ||
if (key != null) { | ||
if (value instanceof String) { | ||
params.putString(key, (String) value); | ||
} else if (value instanceof Integer) { | ||
params.putInt(key,(Integer) value); | ||
} else if (value instanceof Double || value instanceof Long) { | ||
params.putDouble(key,(Double) value); | ||
} else if (value instanceof WritableArray) { | ||
params.putArray(key, (WritableArray) value); | ||
} else if (value instanceof Boolean) { | ||
params.putBoolean(key, (Boolean) value); | ||
} else if (value instanceof Map) { | ||
params.putMap(key, (WritableMap) value); | ||
} else { | ||
params.putNull(key); | ||
} | ||
} | ||
return this; | ||
} | ||
|
||
@Override | ||
public String getName() { | ||
return HomeMessageEventModule.class.getSimpleName(); | ||
} | ||
} |
This file contains 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 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 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 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 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