在 Android 上触摸通知时如何导航到特定的 React 组件?

Posted

技术标签:

【中文标题】在 Android 上触摸通知时如何导航到特定的 React 组件?【英文标题】:How to navigate to a specific React component when touching a notification on Android? 【发布时间】:2020-06-16 08:26:07 【问题描述】:

我目前有一个用 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);

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。

【问题讨论】:

【参考方案1】:

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

【讨论】:

以上是关于在 Android 上触摸通知时如何导航到特定的 React 组件?的主要内容,如果未能解决你的问题,请参考以下文章

当应用程序被杀死时,无法在推送通知单击时导航到特定屏幕(Flutter android)

Swiftui:当用户点击推送通知时,如何导航到特定的 NavigationLink?

如何在 Xamarin Forms iOS 中的特定页面上导航?

在 ionic 3 中点击 One Signal 推送通知时如何导航到特定页面?

当应用程序未运行状态时点击通知时如何导航到特定的目标页面?

Android 11 SystemUI(状态/导航栏)-状态栏下拉时图标的隐藏与通知面板的半透黑色背景