Apple Remote Push Notification
#import "AppDelegate.h"
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{
#pragma mark------------Edited By Subhr----------------------------------
#ifdef LIVE
hostUrl = kBaseLiveURL;
#elif BETA
hostUrl = kBaseBetaURL;
#elif DEBUG
hostUrl = kBaseDevURL;
#else
hostUrl = kBaseLiveURL;
#endif
if(SYSTEM_VERSION_GRATERTHAN_OR_EQUALTO(@"10.0")){
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
center.delegate = self;
[center requestAuthorizationWithOptions:(UNAuthorizationOptionSound | UNAuthorizationOptionAlert | UNAuthorizationOptionBadge) completionHandler:^(BOOL granted, NSError * _Nullable error){
if(!error){
[[UIApplication sharedApplication] registerForRemoteNotifications];
NSLog(@"Register APNS iOS10");
}
}];
}
else {
// Code for old versions
if ([application respondsToSelector:@selector(registerUserNotificationSettings:)]){
UIUserNotificationSettings* notificationSettings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound categories:nil];
[[UIApplication sharedApplication] registerUserNotificationSettings:notificationSettings];
[[UIApplication sharedApplication] registerForRemoteNotifications];
NSLog(@"Register APNS iOS9");
}
}
return YES;
}
#pragma mark-------for iOS10 and above------
//Called when a notification is delivered to a foreground app.
-(void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler{
NSLog(@"User Info : %@",notification.request.content.userInfo);
NSLog(@"willPresentNotification Info APNS iOS10");
NSDictionary * userInfo = notification.request.content.userInfo;
if ( [UIApplication sharedApplication].applicationState == UIApplicationStateActive ){
// app was already in the foreground
NSLog(@"Foreground");
NSString *text ;
if( [[[userInfo valueForKey:@"aps"] valueForKey:@"notificationtype"] intValue]==0){
text = @"New sell request";
}else{
text = @"New Buy request";
}
__weak typeof(self) _weakSelf = self;
UIAlertController *alert = [UIAlertController createAlertControllerWithType:UIAlertControllerStyleAlert btnTitleArray:@[OK] title:text messageTxt:[[userInfo valueForKey:@"aps"] valueForKey:@"alert"] withCompletionHandler:^(NSUInteger index) {
__strong typeof(_weakSelf) _strongSelf = _weakSelf;
if(index == 0){
receivedNotification=@"YES";
_pushclicked = 1;
dispatch_async(dispatch_get_main_queue(), ^{
[_strongSelf.notificationDelegate moveOnToBidPageOnAlert];
});
}
}];
[self.window.rootViewController presentViewController:alert animated:YES completion:^{
}];
}
completionHandler(UNAuthorizationOptionSound | UNAuthorizationOptionAlert | UNAuthorizationOptionBadge);
}
//Called to let your app know which action was selected by the user for a given notification.
- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void(^)(void))completionHandler{
NSLog(@"User Info : %@",response.notification.request.content.userInfo);
NSLog(@"didReceiveNotificationResponse Info APNS iOS10");
NSDictionary *userInfo = response.notification.request.content.userInfo;
_auctionid = [[userInfo valueForKey:@"aps"] valueForKey:@"auctionid"];
_typeOfNotification = [[userInfo valueForKey:@"aps"] valueForKey:@"notificationtype"];
_first_time = [[userInfo valueForKey:@"aps"] valueForKey:@"firsttime"];
NSLog(@"UserInfo...%@",userInfo);
// app was just brought from background to foreground
NSLog(@"Background to forground");
receivedNotification=@"YES";
_pushclicked = 1;
__weak typeof(self) _weakSelf = self;
dispatch_async(dispatch_get_main_queue(), ^{
__strong typeof(_weakSelf) _strongSelf = _weakSelf;
NSLog(@"PushNotificationReceived method Info APNS iOS10");
[_strongSelf.notificationDelegate PushNotificationReceived];
});
completionHandler();
}
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler{
// iOS 10 will handle notifications through other methods
if(SYSTEM_VERSION_GRATERTHAN_OR_EQUALTO(@"10.0")){
NSLog( @"iOS version >= 10. Let NotificationCenter handle this one." );
completionHandler( UIBackgroundFetchResultNewData);
}else{
NSLog( @"HANDLE PUSH, didReceiveRemoteNotification: %@", userInfo );
// custom code to handle notification content
if( [UIApplication sharedApplication].applicationState == UIApplicationStateInactive ){
NSLog( @"INACTIVE" );
completionHandler( UIBackgroundFetchResultNewData );
}else if( [UIApplication sharedApplication].applicationState == UIApplicationStateBackground ){
NSLog( @"BACKGROUND" );
completionHandler( UIBackgroundFetchResultNewData );
}else{
NSLog( @"FOREGROUND" );
completionHandler( UIBackgroundFetchResultNewData );
}
}
}
#pragma mark------End-------
- (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings{
if(notificationSettings.types != UIUserNotificationTypeNone){
}
}
- (void)application:(UIApplication*)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData*)deviceToken{
NSString *token = [[deviceToken description] stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"<>"]];
_pushToken = [token stringByReplacingOccurrencesOfString:@" " withString:@""];
//--------------------Edited By Subhr----------------------------------
[SSKeychain createKeychainValue:_pushToken forIdentifier:kDevice_Token];
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{
_auctionid = [[userInfo valueForKey:@"aps"] valueForKey:@"auctionid"];
_typeOfNotification = [[userInfo valueForKey:@"aps"] valueForKey:@"notificationtype"];
_first_time = [[userInfo valueForKey:@"aps"] valueForKey:@"firsttime"];
NSLog(@"UserInfo...%@",userInfo);
if ( application.applicationState == UIApplicationStateActive ){
// app was already in the foreground
NSLog(@"Foreground");
NSString *text ;
if( [[[userInfo valueForKey:@"aps"] valueForKey:@"notificationtype"] intValue]==0){
text = @"New sell request";
}else{
text = @"New Buy request";
}
AudioServicesPlaySystemSound (systemSoundID);
__weak typeof(self) _weakSelf = self;
UIAlertController *alert = [UIAlertController createAlertControllerWithType:UIAlertControllerStyleAlert btnTitleArray:@[OK] title:text messageTxt:[[userInfo valueForKey:@"aps"] valueForKey:@"alert"] withCompletionHandler:^(NSUInteger index) {
__strong typeof(_weakSelf) _strongSelf = _weakSelf;
if(index == 0){
receivedNotification=@"YES";
_pushclicked = 1;
dispatch_async(dispatch_get_main_queue(), ^{
NSLog(@"PushNotificationReceived method Info APNS iOS10 **");
[_strongSelf.notificationDelegate moveOnToBidPageOnAlert];
});
}
}];
[self.window.rootViewController presentViewController:alert animated:YES completion:^{
}];
}
else{
// app was just brought from background to foreground
NSLog(@"Background to forground");
receivedNotification=@"YES";
_pushclicked = 1;
__weak typeof(self) _weakSelf = self;
dispatch_async(dispatch_get_main_queue(), ^{
__strong typeof(_weakSelf) _strongSelf = _weakSelf;
NSLog(@"PushNotificationReceived method Info APNS iOS10 **");
[_strongSelf.notificationDelegate PushNotificationReceived];
});
}
}
- (void)applicationWillResignActive:(UIApplication *)application{
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
// Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}
- (void)applicationDidEnterBackground:(UIApplication *)application{
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}
- (void)applicationWillEnterForeground:(UIApplication *)application{
// Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}
- (void)applicationDidBecomeActive:(UIApplication *)application{
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}
- (void)applicationWillTerminate:(UIApplication *)application{
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}
@end