-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #676 from davidchiles/XEP0357
Added XEP-0357
- Loading branch information
Showing
7 changed files
with
222 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
// | ||
// NSXMLElement+NSXMLElement_XEP_0357.h | ||
// | ||
// Created by David Chiles on 2/9/16. | ||
// | ||
// | ||
|
||
#import "XMPPIQ.h" | ||
@class XMPPJID; | ||
|
||
/** | ||
XMPPIQ (XEP0357) is a class extension on XMPPIQ for creating the elements for XEP-0357 http://xmpp.org/extensions/xep-0357.html | ||
*/ | ||
extern NSString * __nonnull const XMPPPushXMLNS; | ||
|
||
@interface XMPPIQ (XEP0357) | ||
|
||
/** | ||
Creates an IQ stanza for enabling push notificiations. http://xmpp.org/extensions/xep-0357.html#enabling | ||
@param jid The jid of the XMPP Push Service | ||
@param node Optional node of the XMPP Push Service | ||
@param options Optional values to passed to your XMPP service (this is likely some sort of secret or token to validate this user/device with teh app server) | ||
@return An IQ stanza | ||
*/ | ||
+ (nonnull instancetype)enableNotificationsElementWithJID:(nonnull XMPPJID *)jid node:(nullable NSString *)node options:(nullable NSDictionary <NSString *,NSString *>*)options; | ||
|
||
/** | ||
Creates an IQ stanza for disable push notifications. http://xmpp.org/extensions/xep-0357.html#disabling | ||
@param jid the jid of the XMPP Push Service | ||
@param node the node of the XMPP push Service | ||
@return an IQ Stanza | ||
*/ | ||
+ (nonnull instancetype)disableNotificationsElementWithJID:(nonnull XMPPJID *)jid node:(nullable NSString *)node; | ||
|
||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
// | ||
// NSXMLElement+NSXMLElement_XEP_0357.m | ||
// Pods | ||
// | ||
// Created by David Chiles on 2/9/16. | ||
// | ||
// | ||
|
||
#import "XMPPIQ+XEP_0357.h" | ||
#import "XMPPJID.h" | ||
|
||
NSString *const XMPPPushXMLNS = @"urn:xmpp:push:0"; | ||
|
||
@implementation XMPPIQ (XEP0357) | ||
|
||
+ (instancetype)enableNotificationsElementWithJID:(XMPPJID *)jid node:(NSString *)node options:(nullable NSDictionary<NSString *,NSString *> *)options | ||
{ | ||
NSXMLElement *enableElement = [self elementWithName:@"enable" xmlns:XMPPPushXMLNS]; | ||
[enableElement addAttributeWithName:@"jid" stringValue:[jid full]]; | ||
if ([node length]) { | ||
[enableElement addAttributeWithName:@"node" stringValue:node]; | ||
} | ||
|
||
if ([options count]) { | ||
NSXMLElement *dataForm = [self elementWithName:@"x" xmlns:@"jabber:x:data"]; | ||
NSXMLElement *formTypeField = [NSXMLElement elementWithName:@"field"]; | ||
[formTypeField addAttributeWithName:@"var" stringValue:@"FORM_TYPE"]; | ||
[formTypeField addChild:[NSXMLElement elementWithName:@"value" stringValue:@"http://jabber.org/protocol/pubsub#publish-options"]]; | ||
[dataForm addChild:formTypeField]; | ||
|
||
[options enumerateKeysAndObjectsUsingBlock:^(NSString * _Nonnull key, NSString * _Nonnull obj, BOOL * _Nonnull stop) { | ||
NSXMLElement *formField = [NSXMLElement elementWithName:@"field"]; | ||
[formField addAttributeWithName:@"var" stringValue:key]; | ||
[formField addChild:[NSXMLElement elementWithName:@"value" stringValue:obj]]; | ||
[dataForm addChild:formField]; | ||
}]; | ||
[enableElement addChild:dataForm]; | ||
} | ||
|
||
return [self iqWithType:@"set" child:enableElement]; | ||
|
||
} | ||
|
||
+ (instancetype)disableNotificationsElementWithJID:(XMPPJID *)jid node:(NSString *)node | ||
{ | ||
NSXMLElement *disableElement = [self elementWithName:@"disable" xmlns:XMPPPushXMLNS]; | ||
[disableElement addAttributeWithName:@"jid" stringValue:[jid full]]; | ||
if ([node length]) { | ||
[disableElement addAttributeWithName:@"node" stringValue:node]; | ||
} | ||
return [self iqWithType:@"set" child:disableElement]; | ||
} | ||
|
||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
// | ||
// XMPPPushTests.swift | ||
// XMPPFrameworkTests | ||
// | ||
// Created by David Chiles on 2/9/16. | ||
// | ||
// | ||
import XMPPFramework | ||
|
||
import XCTest | ||
|
||
class XMPPPushTests: XCTestCase { | ||
|
||
override func setUp() { | ||
super.setUp() | ||
// Put setup code here. This method is called before the invocation of each test method in the class. | ||
} | ||
|
||
override func tearDown() { | ||
// Put teardown code here. This method is called after the invocation of each test method in the class. | ||
super.tearDown() | ||
} | ||
|
||
func testEnableStanzaWithoutOptions() { | ||
|
||
let jid = XMPPJID.jidWithString("push-5.client.example") | ||
let node = "yxs32uqsflafdk3iuqo" | ||
let enableStanza = XMPPIQ.enableNotificationsElementWithJID(jid, node: node, options: nil) | ||
XCTAssertNotNil(enableStanza,"No Stanza") | ||
|
||
/** | ||
<iq type="set"> | ||
<enable xmlns="urn:xmpp:push:0" jid="push-5.client.example" node="yxs32uqsflafdk3iuqo"></enable> | ||
</iq> | ||
*/ | ||
XCTAssertTrue(enableStanza.XMLString() == "<iq type=\"set\"><enable xmlns=\"urn:xmpp:push:0\" jid=\"push-5.client.example\" node=\"yxs32uqsflafdk3iuqo\"></enable></iq>","XML does not match \(enableStanza.XMLString())") | ||
} | ||
|
||
func testEnableStanzaWithOptions() { | ||
let jid = XMPPJID.jidWithString("push-5.client.example") | ||
let node = "yxs32uqsflafdk3iuqo" | ||
let options = ["secret":"eruio234vzxc2kla-91"] | ||
let enableStanza = XMPPIQ.enableNotificationsElementWithJID(jid, node: node, options: options) | ||
XCTAssertNotNil(enableStanza,"No Stanza") | ||
|
||
/** | ||
<iq type="set"> | ||
<enable xmlns="urn:xmpp:push:0" jid="push-5.client.example" node="yxs32uqsflafdk3iuqo"> | ||
<x xmlns="jabber:x:data"> | ||
<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> | ||
*/ | ||
XCTAssertTrue(enableStanza.XMLString() == "<iq type=\"set\"><enable xmlns=\"urn:xmpp:push:0\" jid=\"push-5.client.example\" node=\"yxs32uqsflafdk3iuqo\"><x xmlns=\"jabber:x:data\"><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>","XML does not match \(enableStanza.XMLString())") | ||
} | ||
|
||
func testDisableStanza() { | ||
let jid = XMPPJID.jidWithString("push-5.client.example") | ||
let node = "yxs32uqsflafdk3iuqo" | ||
let disableStanza = XMPPIQ.disableNotificationsElementWithJID(jid, node: node) | ||
XCTAssertNotNil(disableStanza) | ||
print("\(disableStanza.XMLString())") | ||
/** | ||
<iq type="set"> | ||
<disable xmlns="urn:xmpp:push:0" jid="push-5.client.example" node="yxs32uqsflafdk3iuqo"></disable> | ||
</iq> | ||
*/ | ||
XCTAssertTrue(disableStanza.XMLString() == "<iq type=\"set\"><disable xmlns=\"urn:xmpp:push:0\" jid=\"push-5.client.example\" node=\"yxs32uqsflafdk3iuqo\"></disable></iq>","XML does not match \(disableStanza.XMLString())") | ||
} | ||
} |