1
0
Fork 0
mirror of https://github.com/vanitasvitae/Smack.git synced 2024-11-22 03:52:06 +01:00

Push Notifications (XEP-0357) implementation

Fixes SMACK-738
This commit is contained in:
Fernando Ramirez 2016-10-31 13:48:22 -03:00 committed by Florian Schmaus
parent 1d3c48e6ce
commit e266b1acd8
14 changed files with 855 additions and 1 deletions

View file

@ -86,6 +86,7 @@ Experimental Smack Extensions and currently supported XEPs of smack-experimental
| JSON Containers | [XEP-0335](http://xmpp.org/extensions/xep-0335.html) | Encapsulation of JSON data within XMPP Stanzas. |
| [Internet of Things - Discovery](iot.md) | [XEP-0347](http://xmpp.org/extensions/xep-0347.html) | Describes how Things can be installed and discovered by their owners. |
| Client State Indication | [XEP-0352](http://xmpp.org/extensions/xep-0352.html) | A way for the client to indicate its active/inactive state. |
| [Push Notifications](pushnotifications.md) | [XEP-0357](http://xmpp.org/extensions/xep-0357.html) | Defines a way to manage push notifications from an XMPP Server. |
| Google GCM JSON payload | n/a | Semantically the same as XEP-0335: JSON Containers |

View file

@ -0,0 +1,82 @@
Push Notifications
==================
Allows to manage how XMPP servers deliver information for use in push notifications to mobile and other devices.
* Check push notifications support
* Enable push notifications
* Disable push notifications
* Remote disabling of push notifications
**XEP related:** [XEP-0357](http://xmpp.org/extensions/xep-0357.html)
Get push notifications manager
------------------------------
```
PushNotificationsManager pushNotificationsManager = PushNotificationsManager.getInstanceFor(connection);
```
Check push notifications support
--------------------------------
```
boolean isSupported = pushNotificationsManager.isSupportedByServer();
```
Enable push notifications
-----------------------
```
pushNotificationsManager.enable(pushJid, node);
```
or
```
pushNotificationsManager.enable(pushJid, node, publishOptions);
```
*pushJid* is a `Jid`
*node* is a `String`
*publishOptions* is a `HashMap<String, String>` (which means [option name, value])
Disable push notifications
--------------------------
```
pushNotificationsManager.disable(pushJid, node);
```
*pushJid* is a `Jid`
*node* is a `String`
**Disable all**
```
pushNotificationsManager.disableAll(pushJid);
```
*pushJid* is a `Jid`
Remote disabling of push notifications
--------------------------------------
```
// check if the message is because remote disabling of push notifications
if (message.hasExtension(PushNotificationsElements.RemoteDisablingExtension.ELEMENT, PushNotificationsElements.RemoteDisablingExtension.NAMESPACE)) {
// Get the remote disabling extension
PushNotificationsElements.RemoteDisablingExtension remoteDisablingExtension = PushNotificationsElements.RemoteDisablingExtension.from(message);
// Get the user Jid
Jid userJid = remoteDisablingExtension.getUserJid();
// Get the node
String node = remoteDisablingExtension.getNode();
}
```

View file

@ -0,0 +1,169 @@
/**
*
* Copyright © 2016 Fernando Ramirez
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jivesoftware.smackx.push_notifications;
import java.util.HashMap;
import java.util.Map;
import java.util.WeakHashMap;
import org.jivesoftware.smack.ConnectionCreationListener;
import org.jivesoftware.smack.Manager;
import org.jivesoftware.smack.SmackException.NoResponseException;
import org.jivesoftware.smack.SmackException.NotConnectedException;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPConnectionRegistry;
import org.jivesoftware.smack.XMPPException.XMPPErrorException;
import org.jivesoftware.smack.packet.IQ;
import org.jivesoftware.smack.packet.IQ.Type;
import org.jivesoftware.smackx.disco.ServiceDiscoveryManager;
import org.jivesoftware.smackx.push_notifications.element.DisablePushNotificationsIQ;
import org.jivesoftware.smackx.push_notifications.element.EnablePushNotificationsIQ;
import org.jivesoftware.smackx.push_notifications.element.PushNotificationsElements;
import org.jxmpp.jid.Jid;
/**
* Push Notifications manager class.
*
* @see <a href="http://xmpp.org/extensions/xep-0357.html">XEP-0357: Push
* Notifications</a>
* @author Fernando Ramirez
*
*/
public final class PushNotificationsManager extends Manager {
static {
XMPPConnectionRegistry.addConnectionCreationListener(new ConnectionCreationListener() {
@Override
public void connectionCreated(XMPPConnection connection) {
getInstanceFor(connection);
}
});
}
private static final Map<XMPPConnection, PushNotificationsManager> INSTANCES = new WeakHashMap<>();
/**
* Get the singleton instance of PushNotificationsManager.
*
* @param connection
* @return the instance of PushNotificationsManager
*/
public static synchronized PushNotificationsManager getInstanceFor(XMPPConnection connection) {
PushNotificationsManager pushNotificationsManager = INSTANCES.get(connection);
if (pushNotificationsManager == null) {
pushNotificationsManager = new PushNotificationsManager(connection);
INSTANCES.put(connection, pushNotificationsManager);
}
return pushNotificationsManager;
}
private PushNotificationsManager(XMPPConnection connection) {
super(connection);
}
/**
* Returns true if Push Notifications is supported by the server.
*
* @return true if Push Notifications is supported by the server.
* @throws NoResponseException
* @throws XMPPErrorException
* @throws NotConnectedException
* @throws InterruptedException
*/
public boolean isSupportedByServer()
throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException {
return ServiceDiscoveryManager.getInstanceFor(connection())
.serverSupportsFeature(PushNotificationsElements.NAMESPACE);
}
/**
* Enable push notifications.
*
* @param pushJid
* @param node
* @return true if it was successfully enabled, false if not
* @throws NoResponseException
* @throws XMPPErrorException
* @throws NotConnectedException
* @throws InterruptedException
*/
public boolean enable(Jid pushJid, String node)
throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException {
return enable(pushJid, node, null);
}
/**
* Enable push notifications.
*
* @param pushJid
* @param node
* @param publishOptions
* @return true if it was successfully enabled, false if not
* @throws NoResponseException
* @throws XMPPErrorException
* @throws NotConnectedException
* @throws InterruptedException
*/
public boolean enable(Jid pushJid, String node, HashMap<String, String> publishOptions)
throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException {
EnablePushNotificationsIQ enablePushNotificationsIQ = new EnablePushNotificationsIQ(pushJid, node,
publishOptions);
return changePushNotificationsStatus(enablePushNotificationsIQ);
}
/**
* Disable all push notifications.
*
* @param pushJid
* @return true if it was successfully disabled, false if not
* @throws NoResponseException
* @throws XMPPErrorException
* @throws NotConnectedException
* @throws InterruptedException
*/
public boolean disableAll(Jid pushJid)
throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException {
return disable(pushJid, null);
}
/**
* Disable push notifications of an specific node.
*
* @param pushJid
* @param node
* @return true if it was successfully disabled, false if not
* @throws NoResponseException
* @throws XMPPErrorException
* @throws NotConnectedException
* @throws InterruptedException
*/
public boolean disable(Jid pushJid, String node)
throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException {
DisablePushNotificationsIQ disablePushNotificationsIQ = new DisablePushNotificationsIQ(pushJid, node);
return changePushNotificationsStatus(disablePushNotificationsIQ);
}
private boolean changePushNotificationsStatus(IQ iq)
throws NotConnectedException, InterruptedException, NoResponseException, XMPPErrorException {
final XMPPConnection connection = connection();
IQ responseIQ = connection.createPacketCollectorAndSend(iq).nextResultOrThrow();
return responseIQ.getType() != Type.error;
}
}

View file

@ -0,0 +1,82 @@
/**
*
* Copyright © 2016 Fernando Ramirez
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jivesoftware.smackx.push_notifications.element;
import org.jivesoftware.smack.packet.IQ;
import org.jxmpp.jid.Jid;
/**
* Disable Push Notifications IQ.
*
* @see <a href="http://xmpp.org/extensions/xep-0357.html">XEP-0357: Push
* Notifications</a>
* @author Fernando Ramirez
*
*/
public class DisablePushNotificationsIQ extends IQ {
/**
* disable element.
*/
public static final String ELEMENT = "disable";
/**
* the IQ NAMESPACE.
*/
public static final String NAMESPACE = PushNotificationsElements.NAMESPACE;
private final Jid jid;
private final String node;
public DisablePushNotificationsIQ(Jid jid, String node) {
super(ELEMENT, NAMESPACE);
this.jid = jid;
this.node = node;
this.setType(Type.set);
}
public DisablePushNotificationsIQ(Jid jid) {
this(jid, null);
}
/**
* Get the JID.
*
* @return the JID
*/
public Jid getJid() {
return jid;
}
/**
* Get the node.
*
* @return the node
*/
public String getNode() {
return node;
}
@Override
protected IQChildElementXmlStringBuilder getIQChildElementBuilder(IQChildElementXmlStringBuilder xml) {
xml.attribute("jid", jid);
xml.optAttribute("node", node);
xml.rightAngleBracket();
return xml;
}
}

View file

@ -0,0 +1,119 @@
/**
*
* Copyright © 2016 Fernando Ramirez
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jivesoftware.smackx.push_notifications.element;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import org.jivesoftware.smack.packet.IQ;
import org.jivesoftware.smackx.pubsub.packet.PubSub;
import org.jivesoftware.smackx.xdata.FormField;
import org.jivesoftware.smackx.xdata.packet.DataForm;
import org.jxmpp.jid.Jid;
/**
* Enable Push Notifications IQ.
*
* @see <a href="http://xmpp.org/extensions/xep-0357.html">XEP-0357: Push
* Notifications</a>
* @author Fernando Ramirez
*
*/
public class EnablePushNotificationsIQ extends IQ {
/**
* enable element.
*/
public static final String ELEMENT = "enable";
/**
* the IQ NAMESPACE.
*/
public static final String NAMESPACE = PushNotificationsElements.NAMESPACE;
private final Jid jid;
private final String node;
private final HashMap<String, String> publishOptions;
public EnablePushNotificationsIQ(Jid jid, String node, HashMap<String, String> publishOptions) {
super(ELEMENT, NAMESPACE);
this.jid = jid;
this.node = node;
this.publishOptions = publishOptions;
this.setType(Type.set);
}
public EnablePushNotificationsIQ(Jid jid, String node) {
this(jid, node, null);
}
/**
* Get the JID.
*
* @return the JID
*/
public Jid getJid() {
return jid;
}
/**
* Get the node.
*
* @return the node
*/
public String getNode() {
return node;
}
/**
* Get the publish options.
*
* @return the publish options
*/
public HashMap<String, String> getPublishOptions() {
return publishOptions;
}
@Override
protected IQChildElementXmlStringBuilder getIQChildElementBuilder(IQChildElementXmlStringBuilder xml) {
xml.attribute("jid", jid);
xml.attribute("node", node);
xml.rightAngleBracket();
if (publishOptions != null) {
DataForm dataForm = new DataForm(DataForm.Type.form);
FormField formTypeField = new FormField("FORM_TYPE");
formTypeField.addValue(PubSub.NAMESPACE + "#publish-options");
dataForm.addField(formTypeField);
Iterator<Map.Entry<String, String>> publishOptionsIterator = publishOptions.entrySet().iterator();
while (publishOptionsIterator.hasNext()) {
Map.Entry<String, String> pairVariableValue = publishOptionsIterator.next();
FormField field = new FormField(pairVariableValue.getKey());
field.addValue(pairVariableValue.getValue());
dataForm.addField(field);
}
xml.element(dataForm);
}
return xml;
}
}

View file

@ -0,0 +1,100 @@
/**
*
* Copyright © 2016 Fernando Ramirez
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jivesoftware.smackx.push_notifications.element;
import org.jivesoftware.smack.packet.ExtensionElement;
import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.util.XmlStringBuilder;
import org.jivesoftware.smackx.pubsub.packet.PubSub;
import org.jxmpp.jid.Jid;
/**
* Push Notifications elements.
*
* @see <a href="http://xmpp.org/extensions/xep-0357.html">XEP-0357: Push
* Notifications</a>
* @author Fernando Ramirez
*
*/
public class PushNotificationsElements {
public static final String NAMESPACE = "urn:xmpp:push:0";
public static class RemoteDisablingExtension implements ExtensionElement {
public static final String NAMESPACE = PubSub.NAMESPACE;
public static final String ELEMENT = PubSub.ELEMENT;
private final String node;
private final Jid userJid;
public RemoteDisablingExtension(String node, Jid userJid) {
this.node = node;
this.userJid = userJid;
}
@Override
public String getElementName() {
return PubSub.ELEMENT;
}
@Override
public String getNamespace() {
return PubSub.NAMESPACE;
}
/**
* Get the node.
*
* @return the node
*/
public String getNode() {
return node;
}
/**
* Get the user JID.
*
* @return the user JID
*/
public Jid getUserJid() {
return userJid;
}
@Override
public CharSequence toXML() {
XmlStringBuilder xml = new XmlStringBuilder(this);
xml.attribute("node", node);
xml.rightAngleBracket();
xml.halfOpenElement("affiliation");
xml.attribute("jid", userJid);
xml.attribute("affiliation", "none");
xml.closeEmptyElement();
xml.closeElement(this);
return xml;
}
public static RemoteDisablingExtension from(Message message) {
return message.getExtension(ELEMENT, NAMESPACE);
}
}
}

View file

@ -0,0 +1,20 @@
/**
*
* Copyright © 2016 Fernando Ramirez
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* Push Notifications elements (XEP-0357).
*/
package org.jivesoftware.smackx.push_notifications.element;

View file

@ -0,0 +1,20 @@
/**
*
* Copyright © 2016 Fernando Ramirez
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* Classes and interfaces to manage Push Notifications (XEP-0357).
*/
package org.jivesoftware.smackx.push_notifications;

View file

@ -0,0 +1,60 @@
/**
*
* Copyright © 2016 Fernando Ramirez
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jivesoftware.smackx.push_notifications.provider;
import org.jivesoftware.smack.provider.ExtensionElementProvider;
import org.jivesoftware.smackx.push_notifications.element.PushNotificationsElements.RemoteDisablingExtension;
import org.jxmpp.jid.Jid;
import org.jxmpp.jid.impl.JidCreate;
import org.xmlpull.v1.XmlPullParser;
/**
* Push Notifications Remote Disabling Provider class.
*
* @see <a href="http://xmpp.org/extensions/xep-0357.html">XEP-0357: Push
* Notifications</a>
* @author Fernando Ramirez
*/
public class RemoteDisablingProvider extends ExtensionElementProvider<RemoteDisablingExtension> {
@Override
public RemoteDisablingExtension parse(XmlPullParser parser, int initialDepth) throws Exception {
Jid userJid = null;
String node = parser.getAttributeValue("", "node");
outerloop: while (true) {
int eventType = parser.next();
if (eventType == XmlPullParser.START_TAG) {
if (parser.getName().equals("affiliation")) {
userJid = JidCreate.from(parser.getAttributeValue("", "jid"));
String affiliation = parser.getAttributeValue("", "affiliation");
if (affiliation == null || !affiliation.equals("none")) {
return null;
}
}
} else if (eventType == XmlPullParser.END_TAG) {
if (parser.getDepth() == initialDepth) {
break outerloop;
}
}
}
return new RemoteDisablingExtension(node, userJid);
}
}

View file

@ -0,0 +1,20 @@
/**
*
* Copyright © 2016 Fernando Ramirez
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* Push Notifications providers (XEP-0357).
*/
package org.jivesoftware.smackx.push_notifications.provider;

View file

@ -171,5 +171,12 @@
<namespace>urn:xmpp:iot:control</namespace>
<className>org.jivesoftware.smackx.iot.control.provider.IoTSetResponseProvider</className>
</iqProvider>
<!-- XEP-0357 Push Notifications -->
<extensionProvider>
<elementName>pubsub</elementName>
<namespace>http://jabber.org/protocol/pubsub</namespace>
<className>org.jivesoftware.smackx.push_notifications.provider.RemoteDisablingProvider</className>
</extensionProvider>
</smackProviders>

View file

@ -0,0 +1,49 @@
/**
*
* Copyright © 2016 Fernando Ramirez
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jivesoftware.smackx.push_notifications;
import org.jivesoftware.smackx.push_notifications.element.DisablePushNotificationsIQ;
import org.junit.Assert;
import org.junit.Test;
import org.jxmpp.jid.impl.JidCreate;
public class DisablePushNotificationsIQTest {
String disableAllNotificationsIQExample = "<iq id='x97' type='set'>"
+ "<disable xmlns='urn:xmpp:push:0' jid='push-5.client.example'>" + "</disable>" + "</iq>";
String disableNodeNotificationsIQExample = "<iq id='x97' type='set'>"
+ "<disable xmlns='urn:xmpp:push:0' jid='push-5.client.example' node='yxs32uqsflafdk3iuqo'>" + "</disable>"
+ "</iq>";
@Test
public void checkDisableAllPushNotificationsIQ() throws Exception {
DisablePushNotificationsIQ disablePushNotificationsIQ = new DisablePushNotificationsIQ(
JidCreate.from("push-5.client.example"));
disablePushNotificationsIQ.setStanzaId("x97");
Assert.assertEquals(disableAllNotificationsIQExample, disablePushNotificationsIQ.toXML().toString());
}
@Test
public void checkDisableNodePushNotificationsIQ() throws Exception {
DisablePushNotificationsIQ disablePushNotificationsIQ = new DisablePushNotificationsIQ(
JidCreate.from("push-5.client.example"), "yxs32uqsflafdk3iuqo");
disablePushNotificationsIQ.setStanzaId("x97");
Assert.assertEquals(disableNodeNotificationsIQExample, disablePushNotificationsIQ.toXML().toString());
}
}

View file

@ -0,0 +1,58 @@
/**
*
* Copyright © 2016 Fernando Ramirez
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jivesoftware.smackx.push_notifications;
import java.util.HashMap;
import org.jivesoftware.smackx.push_notifications.element.EnablePushNotificationsIQ;
import org.junit.Assert;
import org.junit.Test;
import org.jxmpp.jid.impl.JidCreate;
public class EnablePushNotificationsIQTest {
String exampleEnableIQ = "<iq id='x42' type='set'>"
+ "<enable xmlns='urn:xmpp:push:0' jid='push-5.client.example' node='yxs32uqsflafdk3iuqo'>" + "</enable>"
+ "</iq>";
String exampleEnableIQWithPublishOptions = "<iq id='x42' type='set'>"
+ "<enable xmlns='urn:xmpp:push:0' jid='push-5.client.example' node='yxs32uqsflafdk3iuqo'>"
+ "<x xmlns='jabber:x:data' type='form'>"
+ "<field var='FORM_TYPE'><value>http://jabber.org/protocol/pubsub#publish-options</value></field>"
+ "<field var='secret'><value>eruio234vzxc2kla-91</value></field>" + "</x>" + "</enable>" + "</iq>";
@Test
public void checkEnablePushNotificationsIQ() throws Exception {
EnablePushNotificationsIQ enablePushNotificationsIQ = new EnablePushNotificationsIQ(
JidCreate.from("push-5.client.example"), "yxs32uqsflafdk3iuqo");
enablePushNotificationsIQ.setStanzaId("x42");
Assert.assertEquals(exampleEnableIQ, enablePushNotificationsIQ.toXML().toString());
}
@Test
public void checkEnablePushNotificationsIQWithPublishOptions() throws Exception {
HashMap<String, String> publishOptions = new HashMap<>();
publishOptions.put("secret", "eruio234vzxc2kla-91");
EnablePushNotificationsIQ enablePushNotificationsIQ = new EnablePushNotificationsIQ(
JidCreate.from("push-5.client.example"), "yxs32uqsflafdk3iuqo", publishOptions);
enablePushNotificationsIQ.setStanzaId("x42");
Assert.assertEquals(exampleEnableIQWithPublishOptions, enablePushNotificationsIQ.toXML().toString());
}
}

View file

@ -0,0 +1,67 @@
/**
*
* Copyright © 2016 Fernando Ramirez
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jivesoftware.smackx.push_notifications;
import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.util.PacketParserUtils;
import org.jivesoftware.smackx.push_notifications.element.PushNotificationsElements.RemoteDisablingExtension;
import org.junit.Assert;
import org.junit.Test;
import org.jxmpp.jid.impl.JidCreate;
public class RemoteDisablingPushNotificationsTest {
String remoteDisablingExample = "<message from='push-5.client.example' to='user@example.com'>"
+ "<pubsub xmlns='http://jabber.org/protocol/pubsub' node='yxs32uqsflafdk3iuqo'>"
+ "<affiliation jid='user@example.com' affiliation='none' />" + "</pubsub>" + "</message>";
String wrongRemoteDisabling1 = "<message from='push-5.client.example' to='user@example.com'>"
+ "<pubsub xmlns='http://jabber.org/protocol/pubsub' node='yxs32uqsflafdk3iuqo'>"
+ "<affiliation jid='user@example.com'/>" + "</pubsub>" + "</message>";
String wrongRemoteDisabling2 = "<message from='push-5.client.example' to='user@example.com'>"
+ "<pubsub xmlns='http://jabber.org/protocol/pubsub' node='yxs32uqsflafdk3iuqo'>"
+ "<affiliation jid='user@example.com' affiliation='member' />" + "</pubsub>" + "</message>";
String wrongRemoteDisabling3 = "<message from='push-5.client.example' to='user@example.com'>"
+ "<pubsub xmlns='http://jabber.org/protocol/pubsub'>" + "</pubsub>" + "</message>";
@Test
public void checkRemoteDisablingPushNotificationsParse() throws Exception {
Message message = (Message) PacketParserUtils.parseStanza(remoteDisablingExample);
RemoteDisablingExtension remoteDisablingExtension = RemoteDisablingExtension.from(message);
Assert.assertEquals("yxs32uqsflafdk3iuqo", remoteDisablingExtension.getNode());
Assert.assertEquals(JidCreate.from("user@example.com"), remoteDisablingExtension.getUserJid());
}
@Test
public void checkWrongRemoteDisablighPushNotifications() throws Exception {
Message message1 = (Message) PacketParserUtils.parseStanza(wrongRemoteDisabling1);
RemoteDisablingExtension remoteDisablingExtension1 = RemoteDisablingExtension.from(message1);
Assert.assertNull(remoteDisablingExtension1);
Message message2 = (Message) PacketParserUtils.parseStanza(wrongRemoteDisabling1);
RemoteDisablingExtension remoteDisablingExtension2 = RemoteDisablingExtension.from(message2);
Assert.assertNull(remoteDisablingExtension2);
Message message3 = (Message) PacketParserUtils.parseStanza(wrongRemoteDisabling1);
RemoteDisablingExtension remoteDisablingExtension3 = RemoteDisablingExtension.from(message3);
Assert.assertNull(remoteDisablingExtension3);
}
}