java - 在 Android 上触摸通知时如何导航到特定的 React 组件?
问题描述
我目前有一个用 Java 编写的本机模块,通过HeadlessJS调用:
index.js:
import { AppRegistry } from 'react-native';
import App from './App';
import { name as appName } from './app.json';
import backgroundNotificationHandler from './src/services/backgroundNotificationListener';
import firebase from '@react-native-firebase/app';
AppRegistry.registerComponent(appName, () => App);
firebase.messaging().setBackgroundMessageHandler(backgroundNotificationHandler);
背景通知处理程序.js:
import { NativeModules } from 'react-native';
const backgroundNotificationHandler = async message => {
NativeModules.ActivityStarter.navigateToExample();
return Promise.resolve();
}
export default backgroundNotificationHandler;
ActivityStarter
创建一个通知,按下时导航到应用程序。我想更改它,以便它导航到特定的反应组件。我正在使用 react-native-router-flux 进行导航。
ActivityStarterModule.java:
public class ActivityStarterModule extends ReactContextBaseJavaModule {
ActivityStarterModule(ReactApplicationContext reactContext) {
super(reactContext);
}
@Nonnull
@Override
public String getName() {
return "ActivityStarter";
}
@ReactMethod
void navigateToExample(String notificationMessage, int notificationID, String contentText) {
ReactApplicationContext context = getReactApplicationContext();
NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
NotificationCompat.Builder builder;
Resources res = context.getResources();
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
String CHANNEL_ID = "channel_ID_0";
NotificationChannel channel = new NotificationChannel(CHANNEL_ID, "channel",
NotificationManager.IMPORTANCE_HIGH);
channel.setDescription("channel description");
manager.createNotificationChannel(channel);
builder = new NotificationCompat.Builder(context, CHANNEL_ID);
} else {
builder = new NotificationCompat.Builder(context);
}
Intent activityIntent = new Intent(context, MainActivity.class);
activityIntent.putExtra("FromNotification", true);
PendingIntent action = PendingIntent.getActivity(context, 0, activityIntent, PendingIntent.FLAG_CANCEL_CURRENT);
builder.setLargeIcon(BitmapFactory.decodeResource(res, R.drawable.ic_announcement_black_24dp))
.setSmallIcon(R.drawable.ic_announcement_black_24dp).setTicker("Large text!").setAutoCancel(true)
.setContentTitle(notificationMessage).setPriority(NotificationCompat.PRIORITY_HIGH)
.setCategory(NotificationCompat.CATEGORY_CALL).setContentText(contentText)
.setFullScreenIntent(action, true);
Notification notification = builder.build();
int notificationCode = notificationID;
manager.notify(notificationCode, notification);
}
}
怎么可能做到这一点?TIA。
解决方案
您可以在收到通知时传递一个标志,并基于该标志您可以从登陆屏幕导航到特定屏幕。
推荐阅读
- android - 如何使用 KGraphQL 连接到托管在 AWS 上的 Graph QL 服务器
- r - R - 基于度数计算的模块条件
- movesense - ECG 数据的测量单位
- alienvault - Alien Vault OSSIM登录时出现空白屏幕
- javascript - 触摸屏上的 HTML 拖放
- apache-kafka - 安全地从一个容器(以随机顺序消费消息)切换到另一个(按指定顺序)
- c++ - /usr/bin/ld: 尝试进行安装时找不到 -lusb-1.0
- c++ - 在 gcc 8.2 但不是 MSVC 19 中编译的模板代码
- php - 使用htaccess url更改子域而不创建子域重写php
- reactjs - 无法从 Marvel API 获取数据 - 传递的 API 密钥不允许传递的引荐来源网址