簡體   English   中英

registerForRemoteNotificationTypes不能正常工作iOS7 xcode 6

[英]registerForRemoteNotificationTypes not working iOS7 xcode 6

升級我們的應用程序以使用iOS 8后出現問題,其中registerForRemoteNotificationTypes似乎無法在運行iOS 7的手機上運行,​​因為didRegisterForRemoteNotificationsWithDeviceToken沒有被調用,並且“允許推送通知”對話框沒有出現在應用程序上。 這是我正在使用的代碼

// Add registration for remote notifications
if ([application respondsToSelector:@selector(registerUserNotificationSettings:)]) {
    NSLog(@"iOS 8 Registering for remote notification");
    UIUserNotificationSettings* notificationSettings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound categories:nil];
    [[UIApplication sharedApplication] registerUserNotificationSettings:notificationSettings];
    [[UIApplication sharedApplication] registerForRemoteNotifications];
} else {
    NSLog(@"Registering for remote notification");
    [[UIApplication sharedApplication] registerForRemoteNotificationTypes: (UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)];
}

此代碼適用於運行iOS 8的手機,而不適用於iOS 7.我們設法通過附帶手機的xcode運行應用程序后,我們設法獲得推送通知才能在iOS 7手機上運行。 然后,我們將與廣告Hoc部署相同的代碼部署到運行iOS 7的另一部iPhone和iPad,但兩者均無效。 有什么明顯的東西我在這里失蹤了。

試試這個代碼,可能適合你。

-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary   *)launchOptions
{
if ([application respondsToSelector:@selector(isRegisteredForRemoteNotifications)])
{
// iOS 8 Notifications
[application registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge) categories:nil]];

[application registerForRemoteNotifications];
}
else
{
// iOS < 8 Notifications
[application registerForRemoteNotificationTypes:
 (UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound)];
}
return YES;
}

我注意到XCode 6似乎生成了自己的標有XC的配置文件,這些配置文件也出現在開發者帳戶的配置文件中。 導出ad Hoc版本的應用程序時,它使用其中一個配置文件,似乎無法選擇您在構建設置中創建和設置的配置文件。 它似乎使用了關閉推送通知的通配符配置文件。 我設法再次使用一個配置文件導出,該配置文件似乎是基於我創建的一個按下開啟,它開始工作。 這可以解釋為什么我設法讓它工作,如果我從Xcode直接將應用程序加載到手機。

我遇到了同樣的問題。 在我的情況下,它變成了一個簡單的問題。 您應該首先檢查您的通知設置。 轉到設置/通知中心,檢查您在代碼中設置的通知類型是否已打開。 希望能幫到你!

是的,代碼是非常正確的,但要在您的設備上獲取默認通知警報,首先重置模擬器的內容並重新安裝應用程序。我試過這個並在模擬器上也運行良好。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM