2013-05-03 69 views
-1

我想從我的iphone獲取設備令牌。在閱讀帖子後,我已經完成了這個工作。獲取示例設備令牌

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{ 
    [[UIApplication sharedApplication] registerForRemoteNotificationTypes:UIRemoteNotificationTypeBadge|UIRemoteNotificationTypeAlert ]; 
} 
    (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken 
{ 
    NSLog(@"didRegisterForRemoteNotificationsWithDeviceToken: %@", deviceToken); 
} 

我已經把didRegisterForRemoteNotificationsWithDeviceToken斷點,但沒有就此止步。我如何確保它被調用?

對不起,noob問題。需要在此方面的指導...

+1

找到完整的教程教程 - http://www.raywenderlich.com/3443/apple-push-notification-services-tutorial-part -12 – 2013-05-03 03:39:50

+0

適用於您應用的Eanble推送通知(意味着將通知服務添加到您的供應配置文件)。然後重試。 – NSUserDefault 2013-05-03 03:47:39

回答

1

可以有任何理由。

要麼在生成.cer文件時做了錯誤的事情,要麼在XCode中沒有安裝新的配置文件,因爲在爲PUSH NOTIFICATION SERVICES配置應用程序後刪除了舊配置文件。

因此,在爲應用程序配置蘋果推送通知服務時,再次在供應門戶中再次檢查。並再次下載.cer文件。 然後從.cer文件生成.pem文件(它包含私鑰)。 然後再次下載新的設置配置文件,然後從XCode中刪除舊的設置並安裝新的設置。

然後用這個:

#pragma mark - 
#pragma mark Push Notifications 
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)_deviceToken { 
    // Get a hex string from the device token with no spaces or < > 

    NSString *deviceToken = [[NSString alloc] init]; 

    deviceToken = [[[[_deviceToken description] 
        stringByReplacingOccurrencesOfString: @"<" withString: @""] 
        stringByReplacingOccurrencesOfString: @">" withString: @""] 
        stringByReplacingOccurrencesOfString: @" " withString: @""]; 

    NSLog(@"deviceToken = %@",deviceToken); 

    } 

希望它可以幫助你。

+0

不應該依賴描述 - 不保證保持不變。改爲:[NSString stringWithFormat:@「%08x%08x%08x%08x%08x%08x%08x%08x」, ntohl(tokenBytes [0]),ntohl(tokenBytes [1]),ntohl(tokenBytes [ 2]), ntohl(tokenBytes [3]),ntohl(tokenBytes [4]),ntohl(tokenBytes [5]), ntohl(tokenBytes [6]),ntohl(tokenBytes [7])]'' – Mar0ux 2013-05-03 04:43:18

0

您必須爲您的應用程序創建一個新的供應配置文件,並使用相同的方式爲您的應用程序簽名。您必須在設備中運行應用程序才能獲取設備令牌。如果您在模擬器中運行應用程序,它將返回錯誤。

經歷給

http://www.raywenderlich.com/3443/apple-push-notification-services-tutorial-part-12

http://developer.apple.com/library/ios/#documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/Introduction.html