Smack陷阱和过时的代码几乎无法连接到FCM CCS

时间:2016-07-07 00:30:19

标签: authentication oauth xmpp smack firebase-cloud-messaging

尝试通过Smack Library连接到Firebase CCS几天了。什么都没发生。今天,我尝试设置一个持续时间为10秒的valueanimator,以便在连接发生某些事情时尝试重新连接。我尽可能地尝试了Logd的每一步,但日志甚至都没有显示。我在日志上有几个例外,例如:X-OAUTH2“不正确/编码,PARSER已到达文档末尾”。根据我的阅读,FCM CCS需要验证我的连接。还是不知道如何用Smack做到这一点。在我向他询问如何解决这个问题之后,即便是StackOverflow中所谓的Smack权威之一也变得沉默。

我的问题是连接只是基于Smack库上addConnectionListener的connectionclosed()overriden方法自行关闭:

这就是我的设置:希望有人能指出我正确的方向。这实在令人沮丧。 Smack似乎是一个很好用于XMPP的库,但我看不到它的支持。甚至Packet侦听器和扩展的术语也是抽象的。在线教程已经过时了,所谓的权威甚至都没有回复。

这是我的代码一步一步:

//to set up connection i do this: this thread is called inside onStart()
new Thread(new Runnable(){

                    XMPPTCPConnectionConfiguration.Builder configBuilder = XMPPTCPConnectionConfiguration.builder();
                    private Handler umm;
                    @Override
                    public void run() {
                        configBuilder.setSecurityMode(XMPPTCPConnectionConfiguration.SecurityMode.ifpossible);
                        configBuilder.setServiceName("fcm-xmpp.googleapis.com");
                        configBuilder.setHost("fcm-xmpp.googleapis.com");//configBuilder.setHost("192.168.1.74");//fcm-xmpp.googleapis.com
                        configBuilder.setPort(5236);
                        configBuilder.setCompressionEnabled(false);
                        configBuilder.setSendPresence(false);
                        configBuilder.setUsernameAndPassword(senderID@gcm.googleapis.com,serverKey);
                        //configBuilder.setDebuggerEnabled(true);  <-------wish I knew how to use this
                        configBuilder.setSocketFactory(SSLSocketFactory.getDefault());
                        SASLPlainMechanism p = new SASLPlainMechanism();
                        SASLAuthentication.registerSASLMechanism(p); //ACCORDING TO GOOGLE FCM WE NEED TO HAVE A PLAIN
                        //MECHANISM 
                        XMPPTCPConnection.setUseStreamManagementDefault(true);
                        XMPPTCPConnection.setUseStreamManagementResumptionDefault(true);
                        FCMconnection = new XMPPTCPConnection(configBuilder.build());

                        //FCMconnection.setDebuggerEnabled(true); <-----------WISH I KNEW HOW TO USE THIS                           
                       //THIS MANAGER NEVER WORKED 
                        /* 
                        manager = ReconnectionManager.getInstanceFor(FCMconnection);
                        manager.setFixedDelay(15);
                        ReconnectionManager.setDefaultReconnectionPolicy(ReconnectionManager.ReconnectionPolicy.FIXED_DELAY);
                        manager.enableAutomaticReconnection();
                        try {
                            FCMconnection.connect();
                            FCMconnection.login();
                            Log.d("Thisi","fadf"+myRegId);
                         Log.d("here","attempting");
                        } catch (Exception e) {
                            Log.d("myerror","JJ: "+e);
                            e.printStackTrace();
                        }
                        */

                        umm = yes;

                        try {
                            FCMconnection.connect();                                
                            Log.v("pony", "white horse"+FCMconnection.getStreamId()+" last: "+FCMconnection.getLastStanzaReceived());
                            Log.d("user","WW: "+FCMconnection.getUser()+" EE "+FCMconnection.isConnected()+" SS "+
                                    FCMconnection.isSecureConnection());
                            DeliveryReceiptManager dm = DeliveryReceiptManager // WHO KNOWS WHAT THIS DOES. 
                            //NEVER LOGGED ANYTHING FOR ME
                                    .getInstanceFor(FCMconnection);
                            dm.setAutoReceiptMode(DeliveryReceiptManager.AutoReceiptMode.always);
                            dm.addReceiptReceivedListener(new ReceiptReceivedListener() {

                                @Override
                                public void onReceiptReceived(final String fromid,
                                                              final String toid, final String msgid,
                                                              final Stanza packet) {
                                    Log.d("credentials","from: "+fromid + " to: "+toid+" msg: "+msgid+" stanza: " +
                                            packet.toString());
                                }
                            });                               

                            android.os.Message y4 = android.os.Message.obtain();
                            y4.what = LOGINTOFCM;
                            y4.obj = d.toXML();
                            umm.sendMessage(y4);

                        } catch (SmackException e) {
                            e.printStackTrace();
                            Log.d("pony", "::Dragony:  " + e);
                        } catch (IOException e) {
                            e.printStackTrace();
                            Log.d("pony", "Eagle:: " + e);
                        } catch (XMPPException e) {
                            e.printStackTrace();
                            Log.d("pony", "Earthy:  " + e);
                        }

                    }
                }).start();

注意上面我发送消息给我的onCreate()Handler,它说“LOGINTOFCM”一旦到达处理程序,就会启动两个方法,其中一个方法将connectionListener添加到我的FCMconnection对象,另一个方法将addAsyncStanzaListener添加到我的FCMconnection对象。在这个方法中,我启动了一个尝试登录FCM CCS的valueanimator

此后发生的事情是我的连接被关闭,所以首次尝试我的连接根据日志连接,FCMconnection.isConnected()返回true。第一次尝试是在onStart()中。当我启动尝试登录FCM CCS的valueanimator时,我的连接已关闭。所以,我实际上是在没有连接的情况下尝试登录。今天,我发明了一个黑客试图在每次关闭时重新连接:这是我今天尝试过的,但是原木再一次没有开火:

我创建了一个将在动画结束时重新启动的valueanimator。因此,在某种程度上它将作为一个while循环:它的持续时间是10秒:这是代码:

private class reconnect implements Runnable{

    private reconnect(){

    }

    @Override
    public void run() {
        reconnect_animator.addListener(new AnimatorListenerAdapter() {

            @Override
            public void onAnimationStart(Animator animation) {
                super.onAnimationStart(animation);
                if(!FCMconnection.isConnected()) {
                    try {
                        FCMconnection.connect();
                        FCMconnection.login();
                    } catch (XMPPException e) {
                        e.printStackTrace();
                    } catch (SmackException e) {
                        e.printStackTrace();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    Log.d("are", "LL " + FCMconnection.isConnected() + " WW " + FCMconnection.isAuthenticated() + " ID: " + myRegId);

                }
            }

            @Override
            public void onAnimationEnd(Animator animation) {
                super.onAnimationEnd(animation);

                if(!FCMconnection.isConnected())reconnect_animator.start();
                else{
                    Log.d("conni","WEE: "+FCMconnection.isConnected());
                    reconnect_animator.start();
                }
            }
        });
    }
}

现在在addConnectionListener接口内的connectionClosed方法里面,我为Smack库设置FCMconnection对象 我向我的onCreate()处理程序发送一条消息,试图启动这个动画,如果连接断开,它会尝试重新连接。调用是成功的onCreate()处理程序,所以我的动画师开始,但它不像我想的那样。 此外,我尝试记录他们在FCM文档中讨论的FirebaseRegistrationId服务类给出的注册令牌的值。 甚至看起来这个服务类甚至没有开始,即使我清楚地记录了文档说要做的事情。在这个类中,我尝试使用FirebaseInstanceId.getInstance()更改驻留在onCreate()上的静态字符串的值.getToken();在onCreate()中,我给这个静态字符串一个chewbaka的初始值,它在应用程序的整个操作过程中保持不变。这让我相信Firebase API中这么重要的类甚至没有被调用,即使它在我的清单文件中清楚地记录下来。

如果有任何想法或建议,请帮助我。我尝试了不同的策略,但是这个过时的Smack示例并没有削减它,而Packets,STanzas,PacketExtensions,ExtensionProvider,Elements和DefaultElementExtensions的所有抽象术语只会让工作变得更糟。

再次感谢您的尝试。

0 个答案:

没有答案