如何通过通知点击在应用程序启动时发出事件?
How can I emit event on app launch from notification tap?
我正在为我的 android 应用程序使用 React Native。我有一个监听剪贴板事件的后台服务。如果应用程序感兴趣的内容在剪贴板中,应用程序会向用户发送推送通知。如果应用程序在后台 运行,我可以发出一个事件并且它运行完美,但是如果应用程序关闭并通过点击通知启动,则不会发出任何内容。反应上下文也是空的。我如何在通知启动时发出此事件?
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...");
}
}
}
}
以防有人对此感到疑惑。这是为 react native 0.42.0.
回答的问题
android activity lifecycles and React Context 在 onCreate 和 onStart 生命周期方法上不可用/实例化,但在 onResume 上实例化。我们需要 React Context,因为这是我们可以向我们的 React 本机前端发出事件的方式。因此,要成功发出事件,您必须创建一个处理程序任务,将其延迟 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);
}
}
我正在为我的 android 应用程序使用 React Native。我有一个监听剪贴板事件的后台服务。如果应用程序感兴趣的内容在剪贴板中,应用程序会向用户发送推送通知。如果应用程序在后台 运行,我可以发出一个事件并且它运行完美,但是如果应用程序关闭并通过点击通知启动,则不会发出任何内容。反应上下文也是空的。我如何在通知启动时发出此事件?
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...");
}
}
}
}
以防有人对此感到疑惑。这是为 react native 0.42.0.
回答的问题android activity lifecycles and React Context 在 onCreate 和 onStart 生命周期方法上不可用/实例化,但在 onResume 上实例化。我们需要 React Context,因为这是我们可以向我们的 React 本机前端发出事件的方式。因此,要成功发出事件,您必须创建一个处理程序任务,将其延迟 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);
}
}