2

我使用Parse.com發送推送通知我的iOS應用。 但是當我執行下面的代碼來創建PFInstallation對象時,設備令牌字段爲空。didRegisterForRemoteNotificationsWithDeviceToken不執行 - 推送通知

- (void)application:(UIApplication *)application 
didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)newDeviceToken { 
    // Store the deviceToken in the current installation and save it to Parse. 
    NSLog(@"didRegisterForRemoteNotificationsWithDeviceToken"); 
    PFInstallation *currentInstallation = [PFInstallation currentInstallation]; 
    [currentInstallation setDeviceTokenFromData:newDeviceToken]; 
    [currentInstallation saveInBackground]; 
} 

didFinishLaunchingWithOptions方法

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{ 
    // Override point for customization after application launch. 
    [Parse setApplicationId:@"xhjhkk869698lhlljk554hl55khlkhl4ff99065" clientKey:@"spg1t6jad1ShK2lh5456khh6j7j4nmn1YD6J6rl8vt3"]; 
    [PFAnalytics trackAppOpenedWithLaunchOptions:launchOptions]; 
    [FBProfilePictureView class]; 

// Register for push notifications 
[application registerForRemoteNotificationTypes: 
UIRemoteNotificationTypeBadge | 
UIRemoteNotificationTypeAlert | 
UIRemoteNotificationTypeSound]; 
[[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:UIStatusBarAnimationNone]; 
return YES; 

}

我注意到,從不執行didRegisterForRemoteNotificationsWithDeviceToken。我過檢查了我的證書和配置配置文件,我使用的方法指定here(見下製作一個PEM文件)對它們進行測試。證書和連接正常工作。我還檢查了我的wifi是否阻止推送通知,沒有問題。

所以任何人都可以請建議我在做什麼錯在這裏?

+0

是警報空間PoPing當你註冊遠程通知? – soryngod

+0

沒有任何反應 –

+0

您是在設備上還是在模擬器上測試應用程序? – soryngod

回答

22

你實現? - (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error 也許你有一些錯誤,在這種方法中,你可以得到它的描述。

+0

有關試圖追蹤問題的任何人的重要信息。 – RegularExpression

+0

對我來說非常奇怪的問題。 * iOS 10 swift 3 *我的代碼很完美,但我沒有打電話。我在我的設備上打開WIFI/Internet連接。關閉應用程序並重新啓動。 RegisterRemoteNotification確實呼籲 –

1

這是我在我的應用程序推送通知登記辦:

在AppDelegate.m

#pragma mark PUSH NOTIFICATION 

- (void)application:(UIApplication *)app didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)token 
{ 
    NSUInteger rntypes; 
if ([[UIApplication sharedApplication] respondsToSelector:@selector(registerUserNotificationSettings:)]) { 
    rntypes = [[[UIApplication sharedApplication] currentUserNotificationSettings] types]; 
} else { 
    rntypes = [[UIApplication sharedApplication] enabledRemoteNotificationTypes]; 
} 
    // Set the defaults to disabled unless we find otherwise... 
    NSString *pushBadge = @"disabled"; 
    NSString *pushAlert = @"disabled"; 
    NSString *pushSound = @"disabled"; 

    if(rntypes == UIRemoteNotificationTypeBadge) 
    { 
     pushBadge = @"enabled"; 
    } 
    else if(rntypes == UIRemoteNotificationTypeAlert) 
    { 
     pushAlert = @"enabled"; 
    } 
    else if(rntypes == UIRemoteNotificationTypeSound) 
    { 
     pushSound = @"enabled"; 
    } 
    else if(rntypes == (UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert)) 
    { 
     pushBadge = @"enabled"; 
     pushAlert = @"enabled"; 
    } 
    else if(rntypes == (UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound)) 
    { 
     pushBadge = @"enabled"; 
     pushSound = @"enabled"; 
    } 
    else if(rntypes == (UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound)) 
    { 
     pushAlert = @"enabled"; 
     pushSound = @"enabled"; 
    } 
    else if(rntypes == (UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound)) 
    { 
     pushBadge = @"enabled"; 
     pushAlert = @"enabled"; 
     pushSound = @"enabled"; 
    } 

    NSLog(@"PUSH SOUND %@",pushBadge); 
    NSLog(@"PUSH ALERT %@",pushAlert); 
    NSLog(@"PUSH SOUND %@",pushSound); 

    NSString *deviceToken = [[[[token description] 
           stringByReplacingOccurrencesOfString:@"<"withString:@""] 
           stringByReplacingOccurrencesOfString:@">" withString:@""] 
          stringByReplacingOccurrencesOfString: @" " withString: @""]; 

    NSLog(@"%d bytes", [token length]); 
    NSLog(@"device token = %@", deviceToken); 
} 

- (void)application:(UIApplication *)app didFailToRegisterForRemoteNotificationsWithError:(NSError *)err { 
    NSString *str1 = [NSString stringWithFormat: @"Error: %@", err]; 
    NSLog(@"%@",str1); 
} 

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo 
{ 
    for (id key in userInfo) 
    { 
     NSLog(@"key: %@, value: %@", key, [userInfo objectForKey:key]); 
    } 

    NSLog(@"remote notification: %@",[userInfo description]); 
    NSDictionary *apsInfo = [userInfo objectForKey:@"aps"]; 

    NSString *alert = [apsInfo objectForKey:@"alert"]; 
    NSLog(@"Received Push Alert: %@", alert); 

    NSString *sound = [apsInfo objectForKey:@"sound"]; 
    NSLog(@"Received Push Sound: %@", sound); 

    NSString *badge = [apsInfo objectForKey:@"badge"]; 
    NSLog(@"Received Push Badge: %@", badge); 
    application.applicationIconBadgeNumber = [[apsInfo objectForKey:@"badge"] integerValue]; 

    UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"Notification" message:alert delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil]; 
    [alertView show]; 
    [alertView release]; 
} 

,並把這個成didFinishLaunchingWithOptions:

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)]; 
    } 
+0

thanx作爲迴應。爲什麼你有條件地啓用推送通知類型?這是你的應用程序的具體要求?我在didFinishLaunchingWithOptions中做了什麼是不夠的? –

+0

我正在有條件地無法或禁用徽章,警報或聲音!而你的didFinishLaunchingWithOptions就夠了! – Ajay