2

iOS アプリでプッシュ通知を送信するためにParse.comを使用しています。しかし、次のコードを実行してPFInstallationオブジェクトを作成すると、デバイス トークン フィールドが空白になります。

- (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決して実行されないことに気付きました。私は自分の証明書とプロビジョニング プロファイルをクロス チェックし、ここで指定された方法を使用してそれらをテストしました(「PEM ファイルの作成」を参照)。証明書と接続は正常に機能します。Wi-Fi がプッシュ通知をブロックしているかどうかも確認しましたが、問題はありません。

誰でもいいので、ここで何が間違っているのか教えてください。

4

2 に答える 2

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)];
    }
于 2013-07-17T12:16:28.427 に答える