iOS:我如何调用"应用程序didRegisterForRemoteNotificationsWithDeviceToken"在AppDelegate之外?

时间:2014-07-23 05:51:11

标签: ios parse-platform apple-push-notifications appdelegate

解析

场景 =我有一个发送消息的应用。当消息发送给用户时,它还将发送推送通知。

困难 =当应用启动时,它会调用

-(void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {

    if ([PFUser currentUser]) {

        PFInstallation *currentInstallation = [PFInstallation currentInstallation];
        [currentInstallation setDeviceTokenFromData:deviceToken];
        [currentInstallation setObject:[PFUser currentUser][@"userID"] forKey:@"userID"];
        [currentInstallation saveInBackground];

        NSLog(@"INSTALLATION REGISTERED");
    }

}

当用户首次下载应用时,将不会创建当前用户。因此,如果currentUser存在,应用程序将在didRegisterForRemoteNotificationsWithDeviceToken方法内部进行检查。由于currentUser在第一次启动应用时不存在(他们必须先创建一个帐户),我希望能够在当前用户之后再次呼叫{em>}已创建。

问题 =如何在didRegisterForRemoteNotificationsWithDeviceToken课程之外拨打didRegisterForRemoteNotificationsWithDeviceToken

我尝试了什么 =我在注册用户时的代码

AppDelegate

这会抛出错误

UIApplication *app = [[UIApplication alloc]init];

[app registerForRemoteNotificationTypes:UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound];

4 个答案:

答案 0 :(得分:2)

使用此

 [[UIApplication sharedApplication] registerForRemoteNotificationTypes:UIRemoteNotificationTypeBadge|
 UIRemoteNotificationTypeAlert|
 UIRemoteNotificationTypeSound];

答案 1 :(得分:1)

UIApplication是一个单例,你应该使用sharedApplication来调用它。

UIApplication * app = [UIApplication sharedApplication];
[app registerForRemoteNotificationTypes:UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound];

[编辑]
我犯了一个错误,registerForRemote是一个UIApplication方法

答案 2 :(得分:1)

你为什么要这样做

UIApplication *app = [[UIApplication alloc]init];

[app registerForRemoteNotificationTypes:UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound];

相反,您应该知道UIApplication是一个单例类,因此您可以这样做

  [[UIApplication sharedApplication] registerForRemoteNotificationTypes: (UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound)];

使用上面的代码

希望这会对你有所帮助。快乐的编码:)

答案 3 :(得分:1)

您不应直接调用 didRegisterForRemoteNotificationsWithDeviceToken ,因为它是委托协议的一部分。您应该保存deviceToken并稍后将其传递给PFInstallation,例如:

<强> AppDelegate.m

-(void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
    self.token = deviceToken;
    [self saveInstallation];
}

-(void)saveInstallation{

    if ([PFUser currentUser]) {
        PFInstallation *currentInstallation = [PFInstallation currentInstallation];
        [currentInstallation setDeviceTokenFromData:self.token];
        [currentInstallation setObject:[PFUser currentUser][@"userID"] forKey:@"userID"];
        [currentInstallation saveInBackground];

        NSLog(@"INSTALLATION REGISTERED");
    }

}

<强> AppDelegate.h

@property(strong) NSData* token;
-(void)saveInstallation;

<强> RegistrationScreen.m

#import "AppDelegate.h"

-(void)yourSaveAction{
    // Call this after having a valid PFUser.
    [(AppDelegate*)[[UIApplication sharedApplication] delegate] saveInstallation];
}