Skip to content

Commit

Permalink
Added CKSubscription to receive changes in iCloud as push notifications
Browse files Browse the repository at this point in the history
  • Loading branch information
Atul M committed Nov 21, 2016
1 parent 901c952 commit f1d7384
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 6 deletions.
10 changes: 10 additions & 0 deletions Blink/AppDelegate.m
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@
////////////////////////////////////////////////////////////////////////////////

#import "AppDelegate.h"
#import "BKiCloudSyncHandler.h"
@import UserNotifications;
@import CloudKit;

#if HOCKEYSDK
@import HockeySDK;
Expand All @@ -56,6 +59,10 @@ - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(
[[BITHockeyManager sharedHockeyManager].authenticator authenticateInstallation]; // This line is obsolete in the crash only build
#endif

[[UNUserNotificationCenter currentNotificationCenter]requestAuthorizationWithOptions:(UNAuthorizationOptionAlert) completionHandler:^(BOOL granted, NSError * _Nullable error) {

}];
[application registerForRemoteNotifications];
return YES;
}

Expand Down Expand Up @@ -86,4 +93,7 @@ - (void)applicationWillTerminate:(UIApplication *)application
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler{
[[BKiCloudSyncHandler sharedHandler]fetchFromiCloud];
}
@end
5 changes: 5 additions & 0 deletions Blink/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@
<string>40</string>
<key>LSRequiresIPhoneOS</key>
<true/>
<key>UIBackgroundModes</key>
<array>
<string>fetch</string>
<string>remote-notification</string>
</array>
<key>UILaunchStoryboardName</key>
<string>LaunchScreen</string>
<key>UIMainStoryboardFile</key>
Expand Down
31 changes: 25 additions & 6 deletions Settings/Network/BKiCloudSyncHandler.m
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,34 @@ + (id)sharedHandler{
- (instancetype)init{
self = [super init];
if(self){
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(checkForReachability:) name:kReachabilityChangedNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(checkForReachability:) name:UIApplicationDidBecomeActiveNotification object:nil];
_internetReachable = [Reachability reachabilityForInternetConnection];
[_internetReachable startNotifier];
[self loadSyncItems];
[self initSyncHandler];
}
return self;
}

- (void)initSyncHandler{
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(checkForReachability:) name:kReachabilityChangedNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(checkForReachability:) name:UIApplicationDidBecomeActiveNotification object:nil];
_internetReachable = [Reachability reachabilityForInternetConnection];
[_internetReachable startNotifier];
[self loadSyncItems];
CKDatabase *database = [[CKContainer containerWithIdentifier:@"iCloud.com.carloscabanero.blinkshell"]privateCloudDatabase];
CKRecordZone *zone = [[CKRecordZone alloc]initWithZoneName:@"DefaultZone"];
[database saveRecordZone:zone
completionHandler:^(CKRecordZone * _Nullable zone, NSError * _Nullable error) {

}];
NSPredicate *predicate = [NSPredicate predicateWithValue:YES];
CKQuerySubscription *subscripton = [[CKQuerySubscription alloc]initWithRecordType:@"BKHost" predicate:predicate options:(CKQuerySubscriptionOptionsFiresOnRecordCreation|CKQuerySubscriptionOptionsFiresOnRecordUpdate|CKQuerySubscriptionOptionsFiresOnRecordDeletion)];
CKNotificationInfo *info = [[CKNotificationInfo alloc]init];
info.alertBody = @"Host update";
subscripton.notificationInfo = info;

[database saveSubscription:subscripton completionHandler:^(CKSubscription * _Nullable subscription, NSError * _Nullable error) {

}];
}

- (void)loadSyncItems
{
if (DocumentsDirectory == nil) {
Expand All @@ -75,7 +94,6 @@ - (void)checkForReachability:(NSNotification*)notification{
if(remoteHostStatus == NotReachable) {

}else{
[self deleteAllItems];
[self fetchFromiCloud];
}
}
Expand All @@ -95,6 +113,7 @@ - (void)deleteAllItems{
}

- (void)fetchFromiCloud{
[self deleteAllItems];
CKDatabase *database = [[CKContainer containerWithIdentifier:@"iCloud.com.carloscabanero.blinkshell"]privateCloudDatabase];
CKQuery *hostQuery = [[CKQuery alloc]initWithRecordType:@"BKHost" predicate:[NSPredicate predicateWithValue:YES]];
[database performQuery:hostQuery inZoneWithID:nil completionHandler:^(NSArray<CKRecord *> * _Nullable results, NSError * _Nullable error) {
Expand Down

0 comments on commit f1d7384

Please sign in to comment.