2016-11-24 8 views
1

私はAPNSを練習したいので、デバイストークンを取得する必要があります。
しかし、私は私のiPhone 5を使用してデバイストークンを取得できません。
更新された別のiphoneを使用すると、デバイストークンが正常に取得されます。
どうすればよいですか?ios 9でiphoneデバイストークンを取得する方法

これは私のコードです:

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

    //APNS setting 

if ([application respondsToSelector:@selector(registerUserNotificationSettings:)]){ 

    UIUserNotificationType types = UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound; 
    UIUserNotificationSettings *mySettings = [UIUserNotificationSettings settingsForTypes:types categories:nil]; 
    [application registerUserNotificationSettings:mySettings]; 
    [application registerForRemoteNotifications]; 
} 
} 


-(void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken{ 

NSString *key = @"once"; 

BOOL onceShowed = [[NSUserDefaults standardUserDefaults] boolForKey:key]; 
if (!onceShowed){ 
     // upload device token to my sever  
} 
} 


-(void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings{ 

[application registerForRemoteNotifications]; 
} 


ありがとう!

+0

携帯電話からアプリを削除してもう一度やり直してください。 – Rajat

+0

私はそれを試してみて、私のサーバにトークンをアップロードすることもできません。 ios10を搭載したiphoneは正常にアップロードできます。 –

+0

同じ問題が発生していますが、これを解決できましたか? –

答えて

1

ステップ1:https://developer.apple.com/notifications/

ステップ2でプッシュ通知のために登録します。このコードを試してみてください。

(BOOL)application:(UIApplication *)application 
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{  
    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0) 
    { 
    [[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge) categories:nil]]; 
    [[UIApplication sharedApplication] registerForRemoteNotifications]; 
    } 
    else 
    { 
    [[UIApplication sharedApplication] registerForRemoteNotificationTypes: 
    (UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert)]; 
    } 

- (void)application:(UIApplication *)application 
didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings 
{ 
    [application registerForRemoteNotifications]; 
} 

- (void)application:(UIApplication*)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData*)deviceToken 
{ 
    NSLog(@"deviceToken: %@", deviceToken); 
    NSString * token = [NSString stringWithFormat:@"%@", deviceToken]; 
    //Format token as you need: 
    token = [token stringByReplacingOccurrencesOfString:@" " withString:@""]; 
    token = [token stringByReplacingOccurrencesOfString:@">" withString:@""]; 
    token = [token stringByReplacingOccurrencesOfString:@"<" withString:@""]; 
} 
+0

素敵なアプローチですが、OPの問題をコードコメントやあなたの答えのいくつかの詳細でどのように修正しているのかを説明するとよいでしょう。 –

+0

@Sreenathなぜ廃止されたメソッドを削除し、最新のもので回答を更新しないのですか? –

関連する問題