我正在使用本机为我的Android应用程序。我有一个后台服务,侦听剪贴板事件。如果应用程序感兴趣的东西在剪贴板中,应用程序会向用户发送推送通知。如果应用程序在后台运行,我可以发出一个事件并且它工作正常但是如果应用程序关闭并通过点击通知启动,则不会发出任何内容。反应上下文也是空的。如何在通知发布时发出此事件?
import android.app.Application;
import android.content.Intent;
import android.view.View;
import android.util.Log;
import android.os.Bundle;
import android.app.NotificationManager;
import android.support.v4.content.LocalBroadcastManager;
import com.facebook.react.modules.core.DeviceEventManagerModule;
import com.facebook.react.bridge.ReactContext;
import com.facebook.react.ReactActivity;
import com.facebook.react.common.LifecycleState;
import com.facebook.react.ReactInstanceManager;
import com.facebook.react.shell.MainReactPackage;
public class MainActivity extends ReactActivity {
/**
* Returns the name of the main component registered from JavaScript.
* This is used to schedule rendering of the component.
*/
@Override
protected String getMainComponentName() {
return "PurchaseButton";
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
onNewIntent(getIntent());
}
@Override
public void onNewIntent(Intent intent) {
setIntent(intent);
Bundle bundle = intent.getExtras();
if (bundle != null) {
String result = bundle.getString("response");
int nid = bundle.getInt("nid");
NotificationManager mNotifyMgr = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
mNotifyMgr.cancel(nid);
ReactContext ctx = getReactInstanceManager().getCurrentReactContext();
if (ctx != null) {
ctx.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class).emit("my.custom.event", result);
Log.d("PB", "sent broadcast..");
} else {
Log.d("PB", "react context is null...");
}
}
}
}
答案 0 :(得分:1)
android有activity lifecycles且React Context在onCreate和onStart生命周期方法上不可用/实例化但是onResume。我们需要React Context,因为这是我们如何向我们的反应原生前端发出一个事件。因此,成功发出事件的方法,你必须创建一个处理程序任务,延迟它5秒,当它运行时,你将能够发出事件。值得注意的是,在我的情况下,如果你延迟甚至1秒,你得到上下文,但前端还没有准备好听事件。这是一个示例代码:
package com.purchasebutton;
import android.app.Application;
import android.content.Intent;
import android.view.View;
import android.util.Log;
import android.os.Bundle;
import android.os.Handler;
import android.app.NotificationManager;
import android.support.v4.content.LocalBroadcastManager;
import com.facebook.react.modules.core.DeviceEventManagerModule;
import com.facebook.react.bridge.ReactContext;
import com.facebook.react.ReactActivity;
import com.facebook.react.common.LifecycleState;
import com.facebook.react.ReactInstanceManager;
import com.facebook.react.shell.MainReactPackage;
public class MainActivity extends ReactActivity {
/**
* Returns the name of the main component registered from JavaScript.
* This is used to schedule rendering of the component.
*/
@Override
protected String getMainComponentName() {
return "PurchaseButton";
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
onNewIntent(getIntent());
}
@Override
protected void onResume() {
super.onResume();
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
ReactContext ctx = getReactInstanceManager().getCurrentReactContext();
if (ctx != null) {
Intent intent = getIntent();
Bundle bundle = intent.getExtras();
if (bundle != null) {
String result = bundle.getString("response");
int nid = bundle.getInt("nid");
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationManager.cancel(nid);
ctx.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class).emit("product.changed", result);
}
}
}
}, 5000);
}
@Override
public void onNewIntent(Intent intent) {
super.onNewIntent(intent);
setIntent(intent);
}
}