首页 > 解决方案 > 在 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。

标签: javaandroidreact-nativefirebase-cloud-messaging

解决方案


您可以在收到通知时传递一个标志,并基于该标志您可以从登陆屏幕导航到特定屏幕。


推荐阅读