Last active
August 29, 2015 14:06
-
-
Save imack/5ca2465c06801cd27925 to your computer and use it in GitHub Desktop.
Push Notification Snippets
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
if ([[UIApplication sharedApplication] respondsToSelector:@selector(isRegisteredForRemoteNotifications)]){ | |
// iOS 8 Notifications | |
[[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge) categories:nil]]; | |
[[UIApplication sharedApplication] registerForRemoteNotifications]; | |
} else { | |
// iOS < 8 Notifications | |
[[UIApplication sharedApplication] registerForRemoteNotificationTypes: | |
(UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert)]; | |
} |
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
{ | |
"aps": | |
{ | |
"alert": "Hello, world!", | |
"sound": "default" | |
} | |
} |
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
- (void)application:(UIApplication*)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData*)deviceToken | |
{ | |
NSLog(@"My token is: %@", deviceToken); | |
} | |
- (void)application:(UIApplication*)application didFailToRegisterForRemoteNotificationsWithError:(NSError*)error | |
{ | |
NSLog(@"Failed to get token, error: %@", error); | |
} | |
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo { | |
UIApplicationState state = [application applicationState]; | |
if (state == UIApplicationStateActive) { | |
if ( [userInfo objectForKey:@"url"] ){ | |
NSURL *url = [NSURL URLWithString:[userInfo objectForKey:@"url"]]; | |
[self application:application handleOpenURL:url]; | |
} else { | |
NSString *message = [[userInfo valueForKey:@"aps"] valueForKey:@"alert"]; | |
[TSMessage showNotificationInViewController:[UIApplication sharedApplication].keyWindow.rootViewController title:@"Message" subtitle:message type:TSMessageNotificationTypeWarning duration:TSMessageNotificationDurationEndless canBeDismissedByUser:YES]; | |
} | |
} else { | |
//Do stuff that you would do if the application was not active | |
} | |
} | |
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification | |
{ | |
if ( notification.userInfo){ | |
[TSMessage showNotificationWithTitle:[NSString stringWithFormat:@"Notice: %@", notification.alertBody] | |
type:TSMessageNotificationTypeMessage]; | |
} | |
} |
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
My token is: | |
<740f4707 bebcf74f 9b7c25d4 8e335894 5f6aa01d a5ddb387 462c7eaf 61bb78ad> |
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
UILocalNotification *notification = [[UILocalNotification alloc] init]; | |
notification.alertBody = [NSString stringWithFormat:@"Welcome to %@, it has been added to your whistler passport!", event.title]; | |
NSDictionary *userInfo = @{@"event":event.title}; | |
notification.userInfo = userInfo; | |
[[UIApplication sharedApplication] presentLocalNotificationNow:notification]; | |
//[[UIApplication sharedApplication] scheduleLocalNotification:notification]; if fire date set |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment