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

huangapple 未分类评论67阅读模式
英文:

How to navigate to a specific React component when touching a notification on Android?

问题

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;

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);
    }
}

如何实现这个?先行致谢。

英文:

I currently have a native module written in Java that is called via 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 creates a notification that when pressed navigates to the app. I would like to change it so that it navigates to a specific react component. I am using react-native-router-flux for navigation.

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);
    }
}

How is it possible to to do this? TIA.

答案1

得分: 0

你可以在收到通知时传递一个标志,根据该标志,你可以从主屏幕导航到特定屏幕。

英文:

You can pass a flag when notification is received, and based on that flag you can navigate to Specific Screen from Landing Screen.

huangapple
  • 本文由 发表于 2020年3月4日 05:46:12
  • 转载请务必保留本文链接:https://java.coder-hub.com/60515992-2.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定