React-native-iap: [Android] purchaseUpdatedListener 仅触发一次,而不是每次订阅交易时触发

创建于 2020-09-21  ·  27评论  ·  资料来源: dooboolab/react-native-iap

react-native-iap 的版本

"react-native-iap": "4.5.4",

react-native 的版本

"react-native": "0.61.4",

您遇到错误的平台(IOS 或 Android 或两者?)

Android,未在 iOS 上测试

预期行为

每次收到购买时都会触发 purchaseUpdatedListener

实际行为

purchaseUpdatedListener 只为第一个接收者触发一次,然后什么也没有发生

测试环境(模拟器?真实设备?)

真实设备,使用测试 google 帐户在 dev env 中运行(订阅每 5 分钟重复一次 - 电子邮件已收到,但收据未收到)。

我正在使用的简化代码:

import React from 'react';
import { Text, View, TouchableOpacity, Alert } from 'react-native';
import RNIap, {
  purchaseUpdatedListener,
  purchaseErrorListener,
} from 'react-native-iap';

import { SpinnerOverlay } from '../components/SpinnerOverlay';

import { itemSkus } from '../config/in-app';
import { validateRecipt } from '../utils/validateRecipt';
import { logger } from '../utils/logger';

export class SubscriptionScreenInner extends React.Component {
  purchaseUpdateSubscription = null;
  purchaseErrorSubscription = null;

  constructor(props) {
    super(props);
    this.state = {
      products: [],
      productsLoaded: false,
      isProcessing: false
    };
  }

  componentDidMount = async () => {
    try {
      if (itemSkus) {
        await RNIap.initConnection();
        await RNIap.flushFailedPurchasesCachedAsPendingAndroid();
        const products = await RNIap.getSubscriptions(itemSkus);
        this.setState({ products, productsLoaded: true });

        this.purchaseUpdateSubscription = purchaseUpdatedListener(
          async (purchase) => {
            try {
              const receipt = purchase.transactionReceipt;
              if (receipt) {
                const validate = await validateRecipt(purchase);

                if (validate) {
                  await RNIap.finishTransaction(purchase, false);
                  this.props.fetchUserProfile();
                  this.resetScreenState();
                  Alert.alert('receipt validated', 'cool');
                }
              }
            } catch (error) {
              this.resetScreenState();
              logger('Validate recipt error', error);
            }
          }
        );

        this.purchaseErrorSubscription = purchaseErrorListener((error) => {
          this.resetScreenState();
          logger('Purchase error during loading', error);
        });
      }
    } catch (error) {
      logger('Store prroducts error', error);
    }
  };

  componentWillUnmount() {
    if (this.purchaseUpdateSubscription) {
      this.purchaseUpdateSubscription.remove();
    }

    if (this.purchaseErrorSubscription) {
      this.purchaseErrorSubscription.remove();
    }

    RNIap.endConnection();
  }

  requestSubscription = async (sku) => {
    this.setState({ isProcessing: true }, async () => {
      await RNIap.requestSubscription(sku);
    });
  };

  resetScreenState = () => {
    this.setState({ isProcessing: false });
  };

  render() {
    const { products, productsLoaded, isProcessing } = this.state;

    if (!productsLoaded || isProcessing) {
      return (
        <View>
          <SpinnerOverlay />
        </View>
      );
    }

    return (
      <View>
        <View>
          <View>
            <View>
              <Text>Buy monthly subscription</Text>
            </View>

            <View>
              <TouchableOpacity onPress={() => this.requestSubscription(products[0].productId)}>
                <Text>Buy</Text>
              </TouchableOpacity>
            </View>
          </View>
        </View>
      </View>
    );
  }
}
🙏 help wanted 🤖 android 🧪 test

最有用的评论

原来如此。 您遇到下一个订阅事件的问题。 让我测试一下然后回来。

所有27条评论

我将 React Native 升级到 0.63.2 但它没有帮助。

将 react-native-iap 提升到 4.6.3 后没有变化。

我有完全相同的问题,曾经在 4.4.9 上,撞到 4.6.3 并且仍然出现
您是否能够修复它,或者您是否找到了解决方法?

不幸的是,我没有解决方案。

这很奇怪。 您是说在第一次购买后再次尝试购买, purchaseUpdatedListener没有触发? 我之前已经测试过了,它应该会一直触发,直到你杀死了监听器。

原来如此。 您遇到下一个订阅事件的问题。 让我测试一下然后回来。

我们面临同样的问题,我使用的是 4.4.9 并尝试降级并升级到最新的 5.1.1,但结果仍然相同。 仅第一次获得事件。

有什么解决办法吗?

作为临时解决方案,我保存订阅事件的第一个收据并在每个应用程序初始化时对其进行验证 - 它有效,但我仅针对 Android 对其进行了测试。

嗨,@hyochan
知道什么时候会解决这个问题吗?

谢谢

感谢您的提醒! 我明天测试一下。

@hyochan
我知道你仍在解决这个问题,只是想知道这个问题的下一个补丁日期,因为我们整个团队都在等待这个问题。
谢谢

@Kalesberg我肯定会在本周尝试并分享更新。 然而,这个库被许多其他人使用,并且purchaseUpdatedListner上的实现从那时起就没有改变。 我可能必须首先制作一个可重复的示例。 我这周试试。

与 #1160 中的iOS

@hyochan
欣赏它!!

@Kalesberg您能否确认一下android.test.purchased也多次触发?

@hyochan会这样做!

@hyochan
我们的产品仅用于订阅基础,因此android.test.purchased无法最好地证明。
所以我尝试在这个库中使用你的演示应用程序并添加了test.sub1但我无法获得任何产品
Screenshot 2020-11-04 at 6 53 06 PM

@Kalesberg你能把 android logcat 放在触发purchaseUpdatedListener吗?

请告诉我billingResultpurchases

@hyochan ,希望有帮助
真的很期待这个补丁发布

我现在正试图调查这个。 顺便说一句,你能给我更好的文件格式吗? rtf文件很难在我的环境中打开,即chromeos 。 如果您认为有隐私问题,您也可以给我发送电子邮件 ([email protected])。

@hyochan
这是否意味着这个插件没有任何问题?

iOS 版本运行良好,但只有 android 手机有问题

我们只是在找出问题所在。 但是,按照您提供的 logcat,在我看来, acknowledgePurchase也就是finishTransaction无法正常工作,因为购买已播出。 我已经分享了stackoverflow ,它也谈到了类似的情况。

@hyochan
即使在应该总是成功的测试订阅模式下,问题仍然存在

@hyochan ,插件有什么更新吗?

此页面是否有帮助?
0 / 5 - 0 等级