Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

implementing didReceiveRemoteNotification for receiving messages whil…

…e in the foreground
  • Loading branch information...
commit b0259c301a9b920d9df3183f48eaaa2e5fc29efe 1 parent 4e047c8
geremy cohen authored
View
34 objective-c/3.3/Pubnub/AppDelegate.m
@@ -43,6 +43,40 @@ - (void)application:(UIApplication*)application didRegisterForRemoteNotification
}
+
+// This will fire if the application is running and in the foreground, and you receive an APNS notification
+
+- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
+{
+ NSString *message = nil;
+
+ id alert = [userInfo objectForKey:@"aps"];
+
+ NSLog(@"%@", userInfo);
+ NSLog(@"%@", alert);
+
+
+ if ([alert isKindOfClass:[NSString class]]) {
+ message = alert;
+ } else
+
+ if ([alert isKindOfClass:[NSDictionary class]]) {
+ message = [alert objectForKey:@"alert"];
+ }
+ if (alert) {
+ UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Received while running in the foreground!"
+ message:message delegate:self
+ cancelButtonTitle:@"Thanks PubNub!"
+ otherButtonTitles:nil];
+ [alertView show];
+ [self performSelector:@selector(byeAlert:) withObject:alertView afterDelay:2];
+ }
+}
+
+-(void)byeAlert:(UIAlertView*)x{
+ [x dismissWithClickedButtonIndex:-1 animated:YES];
+}
+
- (void)application:(UIApplication*)application didFailToRegisterForRemoteNotificationsWithError:(NSError*)error
{
NSLog(@"Failed to get token, error: %@", error);
View
2  objective-c/3.3/Pubnub/iPhoneTest.m
@@ -349,7 +349,7 @@ - (void) receivePubNubForegroundNotification:(NSNotification *) notification
// as well.
NSLog (@"Successfully received receivePubNubForegroundNotification!");
- NSString *message = [ [pubnub sessionUUID] stringByAppendingString:@" has joined the channel.";
+ NSString *message = [ [pubnub sessionUUID] stringByAppendingString:@" has joined the channel."];
[pubnub publish:[NSDictionary dictionaryWithObjectsAndKeys:channelName,@"channel", message, @"message", nil]];
}
Please sign in to comment.
Something went wrong with that request. Please try again.