Untitled

mail@pastecode.io avatar
unknown
plain_text
2 months ago
1.6 kB
3
Indexable
Never
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;
  };
Leave a Comment