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