Objective-C
Xcode
iOS
iPhone開発
More than 3 years have passed since last update.


APNSへの登録

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

// プッシュ許可の確認を表示
if(NSFoundationVersionNumber > NSFoundationVersionNumber_iOS_7_1){
// iOS8以降
UIUserNotificationType types = UIUserNotificationTypeBadge |
UIUserNotificationTypeSound |
UIUserNotificationTypeAlert;
UIUserNotificationSettings *mySettings = [UIUserNotificationSettings settingsForTypes:types categories:nil];
[application registerUserNotificationSettings:mySettings];
}else{
// iOS8以前
[application registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)];
}
}


デバイストークンの受け取り

// iOS8以降ここを通る

- (void)application:(UIApplication *)application didRegisterUserNotificationSettings:
(UIUserNotificationSettings *)notificationSettings{
// これ呼ばないとデバイストークン取得できない
[application registerForRemoteNotifications];
}

- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
// デバイストークン取得完了
NSString *token = deviceToken.description;
token = [token stringByReplacingOccurrencesOfString:@"<" withString:@""];
token = [token stringByReplacingOccurrencesOfString:@">" withString:@""];
token = [token stringByReplacingOccurrencesOfString:@" " withString:@""];

// トークンをサーバに保存する処理など
[self sendToken:token];
}

- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
// APNSへの登録失敗
}