Untitled

 avatar
unknown
plain_text
a year ago
1.6 kB
5
Indexable
import PushNotification from 'react-native-push-notification';
import PushNotificationIOS from '@react-native-community/push-notification-ios';

sendNotification = async () => {
    PushNotification.configure({
      onRegister: (token: string) => {
        // Set the token in the component state
        console.log('token', token);
        this.createPushnotificationApi(token);
      },
      onNotification: (notification: any) => {
        console.log('NOTIFICATION:', notification);
        notification.finish(PushNotificationIOS.FetchResult.NoData);
      },
    });
  };

  createPushnotificationApi = async (token: any) => {
    const firebasetoken = await token.token;

    const body = {
      data: {
        attributes: {
          firebase_token: firebasetoken,
        },
      },
    };
    const header = {
      'Content-Type': 'application/json',
      token: this.authToken,
    };

    const requestMessage = new Message(
      getName(MessageEnum.RestAPIRequestMessage)
    );

    this.apiCallPostNotification = requestMessage.messageId;
    requestMessage.addData(
      getName(MessageEnum.RestAPIResponceEndPointMessage),
      'bx_block_push_notifications/firebase_tokens'
    );

    requestMessage.addData(
      getName(MessageEnum.RestAPIRequestHeaderMessage),
      JSON.stringify(header)
    );

    requestMessage.addData(
      getName(MessageEnum.RestAPIRequestBodyMessage),
      JSON.stringify(body)
    );

    requestMessage.addData(
      getName(MessageEnum.RestAPIRequestMethodMessage),
      'POST'
    );

    runEngine.sendMessage(requestMessage.id, requestMessage);

    return true;
  };
Editor is loading...
Leave a Comment