【问题标题】:Incorrect encoding exception when connecting to FCM using Smack Library使用 Smack 库连接到 FCM 时出现不正确的编码异常
【发布时间】:2016-07-01 08:09:53
【问题描述】:

我正在尝试使用 smack 库连接到 FCM:

这是我尝试过的。它可以工作,但是当连接尝试登录时出现异常。

new Thread(new Runnable(){
XMPPTCPConnectionConfiguration.Builder configBuilder =     XMPPTCPConnectionConfiguration.builder();
private Handler umm;
@Override
public void run() {
                        configBuilder.setSecurityMode(XMPPTCPConnectionConfiguration.SecurityMode.disabled );
configBuilder.setServiceName("fcm-xmpp.googleapis.com");
configBuilder.setHost("fcm-xmpp.googleapis.com");//MAYBE PROBLEM HERE??
configBuilder.setPort(5236);
configBuilder.setCompressionEnabled(false);
configBuilder.setSendPresence(true);
                         configBuilder.setSocketFactory(SSLSocketFactory.getDefault());
InterfaceClass.FCMconnection = new XMPPTCPConnection(configBuilder.build());
umm = yes;

try {
InterfaceClass.FCMconnection.connect();
Log.v("pony", "white horse");
//InterfaceClass.FCMloggin.start();
android.os.Message y4 = android.os.Message.obtain();
y4.what = LOGINTOFCM;
umm.sendMessage(y4);
//the rest of the thread is just exception handling in catch clauses

一旦我的处理程序收到消息,我就会尝试使用连接登录 像这样:

try {                        FCMconnection.login("senderId@gcm.googleapis.com","SERVER_KEY");
Log.d("black","r2d2");
} catch (XMPPException e) {//exception thrown here
e.printStackTrace();
Log.d("black","maity "+e);

我得到以下异常: “smack.sasl.SASLErrorException:使用 X-OAUTH2 的 SASLError:编码不正确”

现在从文档中清楚地表明要实现 SASL 普通机制, 但我不知道怎么做?文档是这样说的:

“连接有两个重要的要求:

您必须启动传输层安全 (TLS) 连接。请注意,CCS 目前不支持 STARTTLS 扩展。 CCS 需要 SASL PLAIN 身份验证机制,使用 @gcm.googleapis.com(FCM 发件人 ID)和服务器密钥作为密码,其中发件人 ID 和服务器密钥是您在配置客户端应用程序时收集的值。有关获取这些凭据的信息,请参阅您平台的客户端文档。”

有人知道是什么原因导致了这个异常吗?我应该如何使用 smack 库连接到 FCM?

感谢您的建议。

【问题讨论】:

    标签: android smack firebase-cloud-messaging sasl


    【解决方案1】:

    根据通过 XMPP 协议连接到 FCM 的文档需要:

    1) 传输层中的 TLS 连接,为此使用 TLS 协议扩展创建 SSLContext

    2) 普通 SASL 协议,确保“smack-sasl-javax-4.1.8.jar”已集成到您的构建设置中。这花了我很多时间来弄清楚

    3) 主机、服务名和端口号正确(参考下面的代码sn-p)

    下面的代码 sn-p 非常适合我:

    SSLContext sslContext = null;
    try {
        sslContext = SSLContext.getInstance("TLS");
        sslContext.init(null, null, null);
    } catch (Exception e) {
        //Failed to get default ssl context with TLS enabled... something can't proceed further
    }
    XMPPTCPConnectionConfiguration.Builder config = XMPPTCPConnectionConfiguration.builder();
    config.setConnectTimeout(CONNECTION_TIMEOUT);
    config.setSendPresence(true);
    config.setCustomSSLContext(sslContext);
    config.setSecurityMode(ConnectionConfiguration.SecurityMode.disabled);
    config.setServiceName("gcm.googleapis.com");
    config.setHost("fcm-xmpp.googleapis.com");
    config.setPort(5236);//not production server
    config.setDebuggerEnabled(true);
    config.setCompressionEnabled(true);
    config.setSocketFactory(sslContext.getSocketFactory());
    (mConnection = new XMPPTCPConnection(config.build())).addConnectionListener(ConnectionSession.this);
    mConnection.setPacketReplyTimeout(REPLY_TIMEOUT);
    mConnection.connect();
    mConnection.login(userID, password); //use your app server credential here
    

    Smack 版本 4.8.1 通过 openfire 设置实施和测试。

    希望这对某人有所帮助!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-02-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-03
      • 1970-01-01
      • 2019-07-14
      相关资源
      最近更新 更多