Skip to content

Commit

Permalink
crazy push notifications
Browse files Browse the repository at this point in the history
  • Loading branch information
seanhess committed Jul 3, 2013
1 parent d151fe3 commit 1004895
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 12 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ Multi-resolution support

Push Notifications
* https://www.parse.com/docs/push_guide#top/iOS
* https://www.parse.com/apps/wizard-war/push_notifications



Expand Down
26 changes: 22 additions & 4 deletions WizardWar/AppDelegate.m
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,23 @@
#import "AppStyle.h"
#import "UserService.h"
#import <Parse/Parse.h>
#import "MatchmakingViewController.h"

// The director should belong to the app delegate or a singleton
// and you should manually unload or reload it

@interface AppDelegate ()
@property (nonatomic, strong) MatchmakingViewController * matches;
@property (nonatomic, strong) CCDirectorIOS * director;
@property (nonatomic, strong) UINavigationController * rootNavigationController;
@end

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{

NSLog(@"applicationDidFinishLaunchingWithOptions");
[application setStatusBarHidden:YES withAnimation:UIStatusBarAnimationNone];

[AppStyle customizeUIKitStyles];
Expand All @@ -43,6 +47,7 @@ - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(
MainNavViewController * navigationController = [[MainNavViewController alloc] initWithRootViewController:landing];
[self.window setRootViewController:navigationController];
[self.window makeKeyAndVisible];
self.rootNavigationController = navigationController;

// INITIALIZE DIRECTOR
NSLog(@"INITILIZE WITH BOUNDS %@", NSStringFromCGRect(self.window.bounds));
Expand All @@ -58,6 +63,12 @@ - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(
// NSLog(@"FONT: %@",[UIFont fontNamesForFamilyName:@"Comic Zine OT"]);
// NSLog(@"FAMLIES %@", [UIFont familyNames]);


// No idea why it doesn't call didReceiveRemoteNotification on its own
// This is only needed if they open a notification from a cold boot
NSDictionary * remoteNotification = [launchOptions valueForKey:@"UIApplicationLaunchOptionsRemoteNotificationKey"];
if (remoteNotification) [self application:application didReceiveRemoteNotification:remoteNotification];

return YES;
}

Expand All @@ -67,27 +78,34 @@ - (void)application:(UIApplication *)application didRegisterForRemoteNotificatio
NSString *deviceToken = [[newDeviceToken description] stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"<>"]];
deviceToken = [deviceToken stringByReplacingOccurrencesOfString:@" " withString:@""];

NSLog(@"MY VERY SPECIAL deviceToken=%@", deviceToken);
NSLog(@"didRegisterForRemoteNotificationsWithDeviceToken: %@", deviceToken);

// Store the deviceToken in the current installation and save it to Parse.
PFInstallation *currentInstallation = [PFInstallation currentInstallation];
[currentInstallation setDeviceToken:deviceToken];
[[PFInstallation currentInstallation] saveInBackground];
[currentInstallation setDeviceTokenFromData:newDeviceToken];
[currentInstallation saveInBackground];

// If they allow it here, and current user exists
[[UserService shared] saveDeviceToken:deviceToken];
}

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
NSLog(@"!!!! PUSH PUSH PUSH!");
NSLog(@"***************** PUSHY TIME: %@", userInfo);
[PFPush handlePush:userInfo];
[self launchMatchmaking:userInfo[@"matchId"]];
}

- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
NSLog(@"ERROR REGISTER %@", error);
}

- (void)launchMatchmaking:(NSString*)matchId {
if ([[self.rootNavigationController.viewControllers lastObject] class] == [MatchmakingViewController class]) return;
MatchmakingViewController * matchmaking = [MatchmakingViewController new];
matchmaking.autoconnectToMatchId = matchId;
[self.rootNavigationController pushViewController:matchmaking animated:YES];
}

// getting a call, pause the game
-(void) applicationWillResignActive:(UIApplication *)application
{
Expand Down
16 changes: 10 additions & 6 deletions WizardWar/Menus/ChallengeService.m
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,15 @@ -(void)onAdded:(FDataSnapshot *)snapshot {
challenge.opponent = [UserService.shared userWithId:challenge.opponent.userId];

self.myChallenges[challenge.matchId] = challenge;
[self.updated sendNext:self.myChallenges];
[self.updated sendNext:challenge];
}
}

-(void)onRemoved:(FDataSnapshot*)snapshot {
if (self.myChallenges[snapshot.name]) {
Challenge * challenge = self.myChallenges[snapshot.name];
if (challenge) {
[self.myChallenges removeObjectForKey:snapshot.name];
[self.updated sendNext:self.myChallenges];
[self.updated sendNext:challenge];
}
}

Expand All @@ -84,13 +85,13 @@ - (Challenge*)user:(User*)user challengeOpponent:(User*)opponent isRemote:(BOOL)
[child setValue:challenge.toObject];

if (isRemote) {
[self notifyChallenge:challenge];
[self notifyOpponent:challenge];
}

return challenge;
}

- (void)notifyChallenge:(Challenge*)challenge {
- (void)notifyOpponent:(Challenge*)challenge {

if (!challenge.opponent.deviceToken) {
NSLog(@"CANNOT PUSH! no device token");
Expand All @@ -103,7 +104,10 @@ - (void)notifyChallenge:(Challenge*)challenge {
[pushQuery whereKey:@"deviceToken" equalTo:challenge.opponent.deviceToken];

// Send push notification to query
[PFPush sendPushMessageToQueryInBackground:pushQuery withMessage:@"Hello World!"];
[PFPush sendPushDataToQueryInBackground:pushQuery withData:@{
@"alert":[NSString stringWithFormat:@"%@ challenges you to WAR", challenge.main.name],
@"matchId":challenge.matchId
}];
}


Expand Down
2 changes: 2 additions & 0 deletions WizardWar/Menus/MatchmakingViewController.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
@property (nonatomic, strong) NSString* nickname;
@property (nonatomic, strong) NSString* matchID;

@property (nonatomic, strong) NSString* autoconnectToMatchId;

-(void)disconnect;
-(void)reconnect;

Expand Down
12 changes: 10 additions & 2 deletions WizardWar/Menus/MatchmakingViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,9 @@ - (void)connect {
[self didUpdateLocation];

// CHALLENGES
[ChallengeService.shared.updated subscribeNext:^(id x) {
NSLog(@"UPDATED CHALLENGES");
[ChallengeService.shared.updated subscribeNext:^(Challenge *challenge) {
[wself.tableView reloadData];
[wself checkAutoconnectChallenge:challenge];
}];
}

Expand Down Expand Up @@ -187,6 +187,14 @@ - (NSArray*)strangers {
}


#pragma mark - Challenges
-(void)checkAutoconnectChallenge:(Challenge*)challenge {
if ([challenge.matchId isEqualToString:self.autoconnectToMatchId]) {
[self joinMatch:challenge];
}
}


#pragma mark - Firebase stuff

- (void)joinLobby
Expand Down

0 comments on commit 1004895

Please sign in to comment.