1

我寫了下面的代碼在Xcode 6:註冊遠程通知不能在Xcode工作6

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{ 
    // Override point for customization after application launch. 

    [UIApplication sharedApplication].applicationIconBadgeNumber = 0; 
    if ([[UIApplication sharedApplication] respondsToSelector:@selector(registerUserNotificationSettings:)]) { 
     UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:(UIRemoteNotificationTypeBadge|UIRemoteNotificationTypeSound|UIRemoteNotificationTypeAlert|UIUserNotificationTypeAlert|UIUserNotificationTypeBadge|UIUserNotificationTypeSound) categories:nil]; 
     [[UIApplication sharedApplication] registerUserNotificationSettings:settings]; 
    } else { 
     UIRemoteNotificationType myTypes = UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound; 
     [[UIApplication sharedApplication] registerForRemoteNotificationTypes:myTypes]; 
    } 

    return YES; 
} 

的我設置在以下兩種方法突破點:

- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken { 
    //Registration successful 
} 

- (void)application:(UIApplication*)application didFailToRegisterForRemoteNotificationsWithError:(NSError*)error 
{ 
    //Registration failed 
} 

但是這兩個當我在運行iOS 8的設備上測試時,從未觸發過方法。我也嘗試過使用iOS 7,但仍然沒有成功。所以我認爲這可能是我的配置文件的問題。因此,爲了確認,我打開了開發者帳戶,在App ID中,我確認APNS已啓用,撤銷了現有證書,併爲推送通知和開發創建了一個新的開發證書。從鑰匙鏈中刪除舊證書。清除Xcode中的所有配置文件,將新創建的證書添加到鑰匙串中,使用新創建的證書創建開發配置文件,將其添加到Xcode並使用新創建的配置文件再次在兩個設備上運行我的應用程序。仍然沒有結果。然後,我在Xcode 5開盤項目,改變了didFinishLaunching方法如下所示:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{ 
    // Override point for customization after application launch. 

    [UIApplication sharedApplication].applicationIconBadgeNumber = 0; 
    UIRemoteNotificationType myTypes = UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound; 
     [[UIApplication sharedApplication] registerForRemoteNotificationTypes:myTypes]; 

    return YES; 
} 

奔着使用我剛剛創建相同的配置在兩個設備上的應用程序。它運行良好,在- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken的突破點擊中。當我使用Xcode 5進行編譯時,僅使用UIRemoteNotificationType,iOS 7和iOS 8上的遠程通知註冊都成功了!

因此我將應用程序歸檔並提交給iTune connect的測試航班。我第一次得到了來自iTunes Connect的這封郵件:

缺少推送通知授權 - 您的應用似乎包括使用蘋果推送通知服務註冊 API,但 應用程序簽名的權利不包括「 aps-environment「 權利。如果您的應用使用Apple推送通知服務, 請確保您的App ID在 供應門戶中啓用了推送通知,並在使用包含「aps-environment」 權利的 分發供應配置文件對您的應用簽名後重新提交。有關更多信息,請參閱本地和「推送 通知編程指南」中的「供應和開發」。如果您的應用沒有使用Apple推送通知服務 ,則不需要執行任何操作。 您可以從將來的提交中刪除API以停止此警告。 如果您使用第三方框架,則可能需要聯繫 開發人員以獲取有關刪除API的信息。

所以我重新生產APNS證書,生產許可證,加入他們刪除舊後鑰匙扣,使用新創建的生產證書,創造了App Store中的分佈形狀,將其添加到Xcode中,歸檔並提交再次試飛。這次郵件沒有來,但是遠程通知委託方法都沒有觸發(我已經放置了一個AlertView來知道何時觸發了這兩個方法)。

任何人都可以告訴我這種奇怪行爲的原因嗎?我在這裏做錯了什麼?我該如何糾正它?

回答

2

好的。我終於自己找到了解決方案。我不得不添加下面的行IOS 8時:

[[UIApplication sharedApplication] registerForRemoteNotifications]; 

所以對於didFinishLaunchingWithOptions代碼如下所示:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{ 
    // Override point for customization after application launch. 

    [UIApplication sharedApplication].applicationIconBadgeNumber = 0; 
    if ([[UIApplication sharedApplication] respondsToSelector:@selector(registerUserNotificationSettings:)]) { 
     UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:(UIRemoteNotificationTypeBadge|UIRemoteNotificationTypeSound|UIRemoteNotificationTypeAlert|UIUserNotificationTypeAlert|UIUserNotificationTypeBadge|UIUserNotificationTypeSound) categories:nil]; 
     [[UIApplication sharedApplication] registerUserNotificationSettings:settings]; 
     [[UIApplication sharedApplication] registerForRemoteNotifications]; 
    } else { 
     UIRemoteNotificationType myTypes = UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound; 
     [[UIApplication sharedApplication] registerForRemoteNotificationTypes:myTypes]; 
    } 

    return YES; 
} 

奇怪的是,我並沒有改變在其他條件什麼,但是現在它可以在IOS 7和IOS 8上運行。希望它可以幫助某人。我已經花了兩天時間了,當我發佈一個問題後我自己找到解決方案時,我討厭它。

+0

我認爲它是一個好主意,將該行添加到didRegisterUserNotificationSettings回調:http://stackoverflow.com/questions/24216632/remote-notification-ios-8 – 2015-08-25 11:11:45