Skip to content

Instantly share code, notes, and snippets.

@imack
Last active August 29, 2015 14:06
Show Gist options
  • Save imack/5ca2465c06801cd27925 to your computer and use it in GitHub Desktop.
Save imack/5ca2465c06801cd27925 to your computer and use it in GitHub Desktop.
Push Notification Snippets
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)];
}
{
"aps":
{
"alert": "Hello, world!",
"sound": "default"
}
}
- (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];
}
}
My token is:
<740f4707 bebcf74f 9b7c25d4 8e335894 5f6aa01d a5ddb387 462c7eaf 61bb78ad>
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