首页 > 解决方案 > graphql 订阅在本机反应中作为推送通知处理

问题描述

我用 graphql-subscription 构建了一个应用程序,我的视图是关于图书列表的,所以每次我的视图将收到一本新书时,它都会实时更新我的​​图书列表,这部分已经可以正常工作,但我想添加同时推送通知,假设应用程序正在后台运行,如果有人在我的书单中添加了新书也应该收到推送通知,我有一个订阅,接收作为参数的 userId 以便更新只有我的书单。

是否可以将我的 graphql 订阅作为推送通知处理?

componentDidUpdate = () => {
  client
    .subscribe({
      query: SUBSCRIPTION_BOOK_ADDED_BY_ID,
      variables: {
        _id: this.state.user._id,
      },
    })
    .subscribe({
      next: (data) => {
        this.updateBookList(data);
      },
      error: (err) => {
        console.log(err);
      },
    });
};

updateBookList = ({ data }) => {
  this.setState((prevState) => ({
    bookList: _.uniqBy(
      [...prevState.bookList, data.bookAddedById],
      '_id',
    ),
  }));
};

*********

//SUBSCRIPTION_BOOK_ADDED_BY_ID
subscription bookAddedById($_id: ID!) {
  bookAddedById(_id: $_id) {
    _id
    createdBy
    title
    area
  }
}


标签: reactjsreact-nativegraphqlgraphql-subscriptionsreact-native-push-notification

解决方案


推荐阅读