0

i'm using react-native with android

I want to use push notifications using React native and firebase fcm

In the official firebase document, admin is used to send message to the device, but instead of admin, I want to send it to the device using rest api.

But I am confused how to use the rest api to request a message The official documentation says to use rest api as follows

        https://fcm.googleapis.com/fcm/send

this is my code

(backend)

            router.post('/:postId/comment', isLoggedIn, async (req, res, next) => { // POST /post/1/comment
                try {
                
                await admin.messaging().sendToDevice(
                ownertoken.fcmtoken, // ['token_1', 'token_2', ...]
                {
                    data: {
                    owner: JSON.stringify(owner),
                    user: JSON.stringify(user),
                    },
                },
                {
                    priority: 'high',
                },
                );
            

(front)

    import {AppRegistry} from 'react-native';
    import Root from './App';
    import {name as appName} from './app.json';
    import messaging from '@react-native-firebase/messaging';

    messaging().setBackgroundMessageHandler(async remoteMessage => {
      const owner = JSON.parse(remoteMessage.data.owner);
          const user = JSON.parse(remoteMessage.data.user);

          console.log(`The user "${user.name}" comment your post`);
      });

    AppRegistry.registerComponent(appName, () => Root);

in backend router

How can I send a message to the front device using the rest api instead of admin?

user15322469
  • 841
  • 1
  • 8
  • 31
  • you check firebase console any notification data which you send from mobile ? and for notification your app should in background . – Ankit Patel Mar 26 '21 at 10:28
  • @AnkitPatel no when somone write comment my post, i'm tying to send message to owner sorry i edit my question – user15322469 Mar 26 '21 at 10:31
  • so you are store that in you data base and also make api for firbase notification which send that data on notification if you have a issue to make api so tell me i share my code to you – Ankit Patel Mar 26 '21 at 13:33
  • @AnkitPatel yes if you share your code i'll appreciate it makes easer to understand.!!!! – user15322469 Mar 26 '21 at 13:50

0 Answers0