如何注册设备令牌来打击4.2?

时间:2018-02-24 11:15:06

标签: java android smack

以下是我的代码,我想在我的设备离线或应用被杀时获得推送通知。 当我的应用程序是后台时,我想通过XMPP ejabberd服务器获取通知

IQ Class

class MyCustomIQ extends IQ {

    String token = SharedPreferenceManager.getValue(getApplicationContext(), "DEVICE_TOKEN");
    protected MyCustomIQ() {
        super("query", "urn:xmpp:registernoti");
    }
    @Override
    protected IQChildElementXmlStringBuilder getIQChildElementBuilder(IQChildElementXmlStringBuilder xml) {
        xml.rightAngleBracket();
        xml.element("token", token);
        xml.element("devicetpye", "android");
        return xml;
    }
}

已连接

@Override
public void connected(XMPPConnection connection) {
    Log.e(TAG, "connected: ");
    MyCustomIQ iq = new MyCustomIQ();
    iq.setType(IQ.Type.set);
    try {
        abstractXMPPConnection.sendIqWithResponseCallback(iq, new StanzaListener() {
            @Override
            public void processStanza(Stanza packet) throws SmackException.NotConnectedException, InterruptedException {
                Log.e(TAG, "processStanza: " + packet.toString());
            }
        });
    } catch (SmackException.NotConnectedException e) {
        e.printStackTrace();
    } catch (InterruptedException e) {
        e.printStackTrace();
    }

}

1 个答案:

答案 0 :(得分:0)

在xmpp服务器

上尝试使用注册设备令牌代码

注册令牌节

<iq to="YourServer" type="set">
<register xmlns="https://android.googleapis.com/gcm" >
<key>API_KEY</key>
</register>
</iq>

1)注册令牌的方式

  public void registerTokenTomod_gcm(final String deviceToken){
    IQ iq=new IQ("register","https://android.googleapis.com/gcm") {
        @Override
        protected IQChildElementXmlStringBuilder getIQChildElementBuilder(IQChildElementXmlStringBuilder xml) {
            xml.attribute("key",deviceToken);
            xml.setEmptyElement();
            return xml;
        }
    };
    iq.setType(IQ.Type.set);
    iq.setTo(AppConstants.CHAT_HOSTNAME);

    debugLog("getpushDicsoInfo send stanza:"+iq.toXML());
    try {
        if(connection.isSmEnabled()) {
            debugLog("sm enabled");
            try {
                connection.addStanzaIdAcknowledgedListener(iq.getStanzaId(), new StanzaListener() {
                    @Override
                    public void processPacket(Stanza stanza) throws SmackException.NotConnectedException {
                        debugLog("SendMessage addStanzaIdAcknowledgedListener ::" + stanza.toXML());
                        if(registerXmppListener!=null){
                            registerXmppListener.onStanzaIdAcknowledgedReceived(stanza);
                        }
                    }
                });
            } catch (StreamManagementException.StreamManagementNotEnabledException e) {
                e.printStackTrace();
            }
        }
        connection.sendStanza(iq);
    } catch (SmackException.NotConnectedException e) {
        e.printStackTrace();
    }

}

2)注册令牌的方式

public void registerTokenToXmpp(String deviceId,String deviceName,String deviceToken){

    DataForm xep0004 = new DataForm(DataForm.Type.submit);
    FormField token = new FormField("token");
    token.addValue(deviceToken);
    FormField device_id = new FormField("device-id");
    device_id.addValue(deviceId);
    FormField device_name = new FormField("device-name");
    device_name.addValue(deviceName);
    xep0004.addField(token);
    xep0004.addField(device_id);
    xep0004.addField(device_name);

    AdHocCommandData stanza = new AdHocCommandData();
    stanza.setTo("android");
    stanza.setType(IQ.Type.set);
    stanza.setStanzaId("0043423");
    stanza.setNode("register-push-gcm");
    stanza.setAction(AdHocCommand.Action.execute);
    stanza.setForm(xep0004);
    stanza.setFrom(connection.getUser());
    debugLog("getpushDicsoInfo send stanza:"+stanza.toXML());
    try {
        if(connection.isSmEnabled()) {
            debugLog("sm enabled");
            try {
                connection.addStanzaIdAcknowledgedListener(stanza.getStanzaId(), new StanzaListener() {
                    @Override
                    public void processPacket(Stanza stanza) throws SmackException.NotConnectedException {
                        debugLog("SendMessage addStanzaIdAcknowledgedListener ::" + stanza.toXML());
                        if(registerXmppListener!=null){
                            registerXmppListener.onStanzaIdAcknowledgedReceived(stanza);
                        }
                    }
                });
            } catch (StreamManagementException.StreamManagementNotEnabledException e) {
                e.printStackTrace();
            }
        }
        connection.sendStanza(stanza);
    } catch (SmackException.NotConnectedException e) {
        e.printStackTrace();
    }
 }

从服务器取消注册设备令牌

private void unregisterToken(){

  String deviceid="3524940..."; //sony
    String tokenstring="GHkd7Ro2qtMg:XPA91bflkgklDeg..."; // sony
    DataForm xep0004 = new DataForm(DataForm.Type.submit);
    FormField token = new FormField("token");
    token.addValue(tokenstring);
    FormField device_id = new FormField("device-id");
    device_id.addValue(deviceid);
    FormField device_name = new FormField("device-name");
   // device_name.addValue(devicename);
    xep0004.addField(token);
    xep0004.addField(device_id);
    xep0004.addField(device_name);

    AdHocCommandData stanza = new AdHocCommandData();
    stanza.setTo("android");
    stanza.setType(IQ.Type.set);
    stanza.setStanzaId("0043423");
    stanza.setNode("register-push-gcm");
    stanza.setAction(AdHocCommand.Action.execute);
    stanza.setForm(xep0004);
    stanza.setFrom(connection.getUser());
    debugLog("getpushDicsoInfo send stanza:"+stanza.toXML());
    try {
        if(connection.isSmEnabled()) {
            debugLog("sm enabled");
            try {
                connection.addStanzaIdAcknowledgedListener(stanza.getStanzaId(), new StanzaListener() {
                    @Override
                    public void processPacket(Stanza stanza) throws SmackException.NotConnectedException {
                        debugLog("SendMessage addStanzaIdAcknowledgedListener ::" + stanza.toXML());
                        if(registerXmppListener!=null){
                            registerXmppListener.onStanzaIdAcknowledgedReceived(stanza);
                        }
                    }
                });
            } catch (StreamManagementException.StreamManagementNotEnabledException e) {
                e.printStackTrace();
            }
        }
        connection.sendStanza(stanza);
    } catch (SmackException.NotConnectedException e) {
        e.printStackTrace();
    }
}
}