pubnub 设置不适用于 android OS9 android java

pubnub setup not working on android OS9 android java

我使用 java

在 android 上设置了 pubnub

在所有其他操作系统上,设置工作正常。在 Android P 上,但是附加侦听器总是给出此错误 -

  PNStatus(category=PNBadRequestCategory, errorData=PNErrorData(information=null, throwable=PubNubException(errormsg=CLEARTEXT communication to ps.pndsn.com not permitted by network security policy, pubnubError=PubNubError(errorCode=103, errorCodeExtended=0, errorObject=null, message=HTTP Error. Please check network connectivity. Please contact support with error details if issue persists., errorString=null), jso=null, response=null, statusCode=0)), error=true, statusCode=0, operation=PNSubscribeOperation, tlsEnabled=false, uuid=null, authKey=null, origin=null, clientRequest=null, affectedChannels=[5d67fb36f4f95718bf8ec310], affectedChannelGroups=[])

设置如下 -

    PNConfiguration pnConfiguration = new PNConfiguration();
    pnConfiguration.setLogVerbosity(PNLogVerbosity.BODY);
    pnConfiguration.setPublishKey(getString(R.string.pubnub_publish_key));
    pnConfiguration.setSubscribeKey(getString(R.string.pubnub_subscribe_key));
    pnConfiguration.setSecure(false);
    pubnub = new PubNub(pnConfiguration);

然后我像这样附加监听器

pubnub.addListener(new SubscribeCallback() {
@Override
public void status(PubNub pubnub, PNStatus status) {
    Log.e("Pubnub:- ", status.toString());
    switch (status.getOperation()) {
        // let's combine unsubscribe and subscribe handling for ease of use
        case PNSubscribeOperation:
        case PNUnsubscribeOperation:
            // note: subscribe statuses never have traditional
            // errors, they just have categories to represent the
            // different issues or successes that occur as part of subscribe
            switch (status.getCategory()) {
                case PNConnectedCategory:
                    // this is expected for a subscribe, this means there is no error or issue whatsoever
                case PNReconnectedCategory:
                    // this usually occurs if subscribe temporarily fails but reconnects. This means
                    // there was an error but there is no longer any issue
                case PNDisconnectedCategory:
                    // this is the expected category for an unsubscribe. This means there
                    // was no error in unsubscribing from everything
                case PNUnexpectedDisconnectCategory:
                    // this is usually an issue with the internet connection, this is an error, handle appropriately
                case PNAccessDeniedCategory:
                    // this means that PAM does allow this client to subscribe to this
                    // channel and channel group configuration. This is another explicit error
                default:
                    // More errors can be directly specified by creating explicit cases for other
                    // error categories of `PNStatusCategory` such as `PNTimeoutCategory` or `PNMalformedFilterExpressionCategory` or `PNDecryptionErrorCategory`
            }

        case PNHeartbeatOperation:
            // heartbeat operations can in fact have errors, so it is important to check first for an error.
            // For more information on how to configure heartbeat notifications through the status
            // PNObjectEventListener callback, consult <link to the PNCONFIGURATION heartbeart config>
            if (status.isError()) {
                // There was an error with the heartbeat operation, handle here
            } else {
                // heartbeat operation was successful
            }
        default: {
            // Encountered unknown status type
        }
    }
}

@Override
public void message(PubNub pubnub, PNMessageResult message) {
    String messagePublisher = message.getPublisher();
    System.out.println("Message publisher: " + messagePublisher);
    System.out.println("Message Payload: " + message.getMessage());
    System.out.println("Message Subscription: " + message.getSubscription());
    System.out.println("Message Channel: " + message.getChannel());
    System.out.println("Message timetoken: " + message.getTimetoken());
}

@Override
public void presence(PubNub pubnub, PNPresenceEventResult presence) {

}

@Override
public void signal(PubNub pubnub, PNSignalResult pnSignalResult) {
    System.out.println("Signal publisher: " + signal.getPublisher());
    System.out.println("Signal payload: " + signal.getMessage());
    System.out.println("Signal subscription: " + signal.getSubscription());
    System.out.println("Signal channel: " + signal.getChannel());
    System.out.println("Signal timetoken: " + signal.getTimetoken());
}

@Override
public void user(PubNub pubnub, PNUserResult pnUserResult) {
    // for Objects, this will trigger when:
    // . user updated
    // . user deleted
    PNUser pnUser = pnUserResult.getUser(); // the user for which the event applies to
    pnUserResult.getEvent(); // the event name
}

@Override
public void space(PubNub pubnub, PNSpaceResult pnSpaceResult) {
    // for Objects, this will trigger when:
    // . space updated
    // . space deleted
    PNSpace pnSpace = pnSpaceResult.getSpace(); // the space for which the event applies to
    pnSpaceResult.getEvent(); // the event name
}

@Override
public void membership(PubNub pubnub, PNMembershipResult pnMembershipResult) {
    // for Objects, this will trigger when:
    // . user added to a space
    // . user removed from a space
    // . membership updated on a space
    JsonElement data = pnMembershipResult.getData(); // membership data for which the event applies to
    pnMembershipResult.getEvent(); // the event name
}

@Override
public void messageAction(PubNub pubnub, PNMessageActionResult pnActionResult) {
    PNMessageAction pnMessageAction = pnActionResult.getAction();
    System.out.println("Message action type: " + pnMessageAction.getType());
    System.out.println("Message action value: " + pnMessageAction.getValue());
    System.out.println("Message action uuid: " + pnMessageAction.getUuid());
    System.out.println("Message action actionTimetoken: " + pnMessageAction.getActionTimetoken());
    System.out.println("Message action messageTimetoken: " + pnMessageAction.getMessageTimetoken());]

    System.out.println("Message action subscription: " + pnActionResult.getSubscription());
    System.out.println("Message action channel: " + pnActionResult.getChannel());
    System.out.println("Message action timetoken: " + pnActionResult.getTimetoken());
}

});

就像我说的,它在除 9 以外的其他 OS 中工作正常,我使用的是最新版本的 pubnub,事实上我升级到最新版本并且上面的错误是相同的。

而且我注意到了其他事情,我在调试器中看到的一条消息仅在 android 版本 9 中的 运行 时出现。

  isWhitelistProcess - Process is Whitelisted

我搜索了该消息,发现它是一条无害的警告消息。

启用 TLS (SSL) 可解决此错误: pnConfiguration.setSecure(true);

原因已在 another Stack Overflow thread 中得到解答。它不是 PubNub 特有的,但因为您在 PubNub 中禁用了 TLS (SSL),所以它在错误中明确调用了 PubNub 域。