-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathqparse_ios.mm
75 lines (68 loc) · 2.99 KB
/
qparse_ios.mm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include "qparse.h"
#include <QNetworkAccessManager>
#include <QtConcurrent>
#import "UIKit/UIKit.h"
/*! Override the QIOSApplicationDelegate adding
* a category for handling the callbacks for device token for push notifications
* The only way to do that even if it's a bit like hacking the Qt stuff
* See: https://bugreports.qt-project.org/browse/QTBUG-38184
*/
@interface QIOSApplicationDelegate
@end
//! Add a category to QIOSApplicationDelegate
@interface QIOSApplicationDelegate (QParseApplicationDelegate)
@end
@implementation QIOSApplicationDelegate (QParseApplicationDelegate)
- (void)application:(UIApplication *)app didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
NSString *token = [[deviceToken description] stringByTrimmingCharactersInSet: [NSCharacterSet characterSetWithCharactersInString:@"<>"]];
token = [token stringByReplacingOccurrencesOfString:@" " withString:@""];
NSLog(@"content---%@", token);
QString qtoken = QString::fromNSString(token);
QParse::instance()->sendInstallationPostRequest( qtoken );
}
- (void)application:(UIApplication *)app didFailToRegisterForRemoteNotificationsWithError:(NSError *)err {
NSString *str = [NSString stringWithFormat: @"Error: %@", err];
NSLog(@"%@",str);
}
@end
void QParse::createInstallation( QStringList channels, QString timeZone ) {
if ( installation.contains("objectId") ) {
// Nothing to do !!!
// IN FUTURE IT SHOULD CHECK IF THE TOKEN IS STILL VALID
return;
}
// prepare the data about channels and timeZone
if ( !channels.isEmpty() ) {
installation["channels"] = QJsonArray::fromStringList(channels);
}
if ( !timeZone.isEmpty() ) {
installation["timeZone"] = timeZone;
}
if ([[UIApplication sharedApplication] respondsToSelector:@selector(registerUserNotificationSettings:)]) {
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:
UIUserNotificationTypeBadge |
UIUserNotificationTypeAlert |
UIUserNotificationTypeSound
categories: nil];
[[UIApplication sharedApplication] registerUserNotificationSettings:settings];
[[UIApplication sharedApplication] registerForRemoteNotifications];
} else {
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:
UIRemoteNotificationTypeBadge |
UIRemoteNotificationTypeAlert |
UIRemoteNotificationTypeSound];
}
// the request on PARSE will be done on the applicatio delegate (see above) that call the below method
}
void QParse::sendInstallationPostRequest( QString token ) {
// Prepare the request
// direct request, the reply will be handled on onRequestFinished
QNetworkRequest request(QUrl("https://api.parse.com/1/installations"));
request.setRawHeader("X-Parse-Application-Id", appId.toLatin1());
request.setRawHeader("X-Parse-REST-API-Key", restKey.toLatin1());
request.setRawHeader("Content-Type", "application/json");
installation["deviceType"] = "ios";
installation["deviceToken"] = token;
QJsonDocument jsonDoc(installation);
net->post( request, jsonDoc.toJson(QJsonDocument::Compact) );
}