2016-10-13 9 views

答えて

1

我々は新しいフレームワークといくつかのコードを追加する必要がIOS 10を使用する場合

STEP:1

iOS 10から、私たちはUserNotificationsフレームワークとdelegat.Firstを追加する必要があります。

STEP:2

デリゲートUNUserNotificationCenterDelegate今

を追加appDelegate.h

#import <UserNotifications/UserNotifications.h> 
@interface AppDelegate : UIResponder <UIApplicationDelegate,UNUserNotificationCenterDelegate> 

appDeleg ate.m

STEP:3

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{ 
    application.applicationIconBadgeNumber = 0; 
    if(SYSTEM_VERSION_LESS_THAN(@"10.0")) 
    { 
    [[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge) categories:nil]]; 
    [[UIApplication sharedApplication] registerForRemoteNotifications]; 
    } 
    else 
    { 
    UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter]; 
    center.delegate = self; 
    [center requestAuthorizationWithOptions:(UNAuthorizationOptionSound | UNAuthorizationOptionAlert | UNAuthorizationOptionBadge) completionHandler:^(BOOL granted, NSError * _Nullable error) 
    { 
     if(!error) 
     { 
     [[UIApplication sharedApplication] registerForRemoteNotifications]; // required to get the app to do anything at all about push notifications 
     NSLog(@"Push registration success."); 
     } 
     else 
     { 
     NSLog(@"Push registration FAILED"); 
     NSLog(@"ERROR: %@ - %@", error.localizedFailureReason, error.localizedDescription); 
     NSLog(@"SUGGESTIONS: %@ - %@", error.localizedRecoveryOptions, error.localizedRecoverySuggestion); 
     } 
    }]; 
    } 

    return YES; 
} 

STEP:4

- (void)application:(UIApplication*)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken 
    { 
     // custom stuff we do to register the device with our AWS middleman 
     NSString *strToken = [[deviceToken description] stringByTrimmingCharactersInSet: [NSCharacterSet characterSetWithCharactersInString:@"<>"]]; 
     strToken = [strToken stringByReplacingOccurrencesOfString:@" " withString:@""]; 
     NSLog(@"content---%@", strToken); 

     NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",@"your url here"]]]; 


     [request setHTTPMethod:@"POST"]; 

     //Pass The String to server 
     NSString *userUpdate =[NSString stringWithFormat:@"device_token=%@",strToken,nil]; 

     //Check The Value what we passed 
     NSLog(@"the data Details is =%@", userUpdate); 

     //Convert the String to Data 
     NSData *data1 = [userUpdate dataUsingEncoding:NSUTF8StringEncoding]; 

     //Apply the data to the body 
     [request setHTTPBody:data1]; 

     //Create the response and Error 
     NSURLSession *session = [NSURLSession sharedSession]; 
     NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) { 
     NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response; 
     if(httpResponse.statusCode == 200) 
     { 
     if(httpResponse.statusCode == 200) 
     { 
      NSError *parseError = nil; 
      NSDictionary *responseDictionary = [NSJSONSerialization JSONObjectWithData:data options:0 error:&parseError]; 
      NSLog(@"The response is - %@",responseDictionary); 
     } 
     else 
     { 
      NSLog(@"Error"); 
     } 
     } 
     else 
     { 
      NSLog(@"faield to connect"); 
     } 
     }]; 
     [dataTask resume];  

} 

STEP:5

-(void) application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void 
(^)(UIBackgroundFetchResult))completionHandler 
{ 
    // iOS 10 will handle notifications through other methods 

    if(SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"10.0")) 
    { 
     NSLog(@"iOS version >= 10. Let NotificationCenter handle this one."); 
     // set a member variable to tell the new delegate that this is background 
     return; 
    } 
    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); 
    } 
    } 

//OR 

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo 
{ 
    [self application:application didReceiveRemoteNotification:userInfo fetchCompletionHandler:^(UIBackgroundFetchResult result) { 
     }]; 
     NSLog(@"Received notification: %@", userInfo); 
     if ([userInfo count]!=0) 
     { 
     NSMutableArray *notify=[[NSMutableArray alloc]init]; 
     NSMutableArray *mutableRetrievedDictionary; 
    mutableRetrievedDictionary = [[NSUserDefaults standardUserDefaults] objectForKey:@"noyificationcount"] ; 
     NSMutableArray *deviealert = [[NSUserDefaults standardUserDefaults] objectForKey:@"noyificationcount"] ; 
     deviealert=[[NSMutableArray alloc]init]; 
     [notify addObject: [[userInfo valueForKey:@"aps"] valueForKey:@"alert"]]; 
     NSLog(@"notification is - %@", notify); 
     NSString *strAlertValue = [[userInfo valueForKey:@"aps"] valueForKey:@"badge"]; 
     NSLog(@"my message-- %@",strAlertValue); 
     deviealert=[notify mutableCopy]; 
     NSLog(@"new...%@",deviealert); 
     [[ NSUserDefaults standardUserDefaults]setObject:deviealert forKey:@"noyificationcount" ]; 
     [[NSUserDefaults standardUserDefaults]synchronize]; 
     NSLog(@"dev.....%@",deviealert); 
     [UIApplication sharedApplication].applicationIconBadgeNumber+=1; 
     } 


    } 

STEP:6

フォアグラウンド状態に対する

- (void)userNotificationCenter:(UNUserNotificationCenter *)center 
    willPresentNotification:(UNNotification *)notification 
    withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler 
    { 
    NSLog(@"Handle push from foreground"); 
    // custom code to handle push while app is in the foreground 
    NSLog(@"%@", notification.request.content.userInfo); 
    } 

STEP:7

背景状態について

+0

私はjson apiを持っていますが、私はAPIを呼び出します –

+0

コードを投稿してください。私はこれのために多くの問題に直面しています。 –

+0

jsonコードでdidRecieveRemoteNotificationsを呼び出すと、自動的にpushnotificationsが返されます。配列や文字列を取得すると、何かがそこにあります。 –

2

iOS 10でプッシュ通知を実装する方法については、下のリンクから完全に理解できます。ここでは、リンクの下に参照してくださいXCodeの8に同じことを実装するための手順を見つける:Push notification issue with iOS 10

+0

ここでjson apiが実装されています –

関連する問題