iOS:我怎么称呼;应用程序DIDregisterForRemotonificationswithDeviceToken“;在AppDelegate之外?

iOS:我怎么称呼;应用程序DIDregisterForRemotonificationswithDeviceToken“;在AppDelegate之外?,ios,parse-platform,apple-push-notifications,appdelegate,Ios,Parse Platform,Apple Push Notifications,Appdelegate,使用 解析 场景=我有一个发送消息的应用程序。当消息发送给用户时,它还将发送推送通知 困难=应用程序启动时调用 -(void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken { if ([PFUser currentUser]) { PFInstallation *currentInstalla

使用 解析

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

困难=应用程序启动时调用

-(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。因此,如果当前用户存在,应用程序将在
didRegisterForRemotionTificationsWithDeviceToken
方法中进行检查。由于第一次启动应用程序时currentUser不存在(他们必须先创建帐户),我希望在创建currentUser后能够再次调用
DidRegisterForRemotionTificationsSwithDeviceToken

问题=如何在
AppDelegate
类之外调用
didRegisterForRemotionTificationswithDeviceToken

我在注册用户时尝试的内容=此代码

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

[app registerForRemoteNotificationTypes:UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound];
这会抛出一个错误

*** Assertion failure in -[UIApplication init]
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'There can only be one UIApplication instance.'

UIApplication是一个单例应用程序,您应该使用
sharedApplication
调用它

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

我犯了一个错误registerForRemote是一个UIApplication方法

UIApplication是一个单例,您应该使用
sharedApplication
调用它

UIApplication * app = [UIApplication sharedApplication];
[app registerForRemoteNotificationTypes:UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound];
[编辑]
我犯了一个错误registerForRemote是一个UIApplication方法

为什么要这样做

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

[app registerForRemoteNotificationTypes:UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound];
相反,您应该知道UIApplication是一个单例类,因此您可以这样做

  [[UIApplication sharedApplication] registerForRemoteNotificationTypes: (UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound)];
在需要的地方使用上述代码

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

你为什么要这样做

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

[app registerForRemoteNotificationTypes:UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound];
相反,您应该知道UIApplication是一个单例类,因此您可以这样做

  [[UIApplication sharedApplication] registerForRemoteNotificationTypes: (UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound)];
在需要的地方使用上述代码

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

使用这个

 [[UIApplication sharedApplication] registerForRemoteNotificationTypes:UIRemoteNotificationTypeBadge|
 UIRemoteNotificationTypeAlert|
 UIRemoteNotificationTypeSound];
用这个

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

您不应该直接使用DeviceToken调用DidRegisterForRemotenotifications,因为它是委托协议的一部分。您应该保存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");
    }

}
#import "AppDelegate.h"

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

@property(strong) NSData* token;
-(void)saveInstallation;
注册屏幕.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");
    }

}
#import "AppDelegate.h"

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

您不应该直接使用DeviceToken调用DidRegisterForRemotenotifications,因为它是委托协议的一部分。您应该保存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");
    }

}
#import "AppDelegate.h"

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

@property(strong) NSData* token;
-(void)saveInstallation;
注册屏幕.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");
    }

}
#import "AppDelegate.h"

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

嘿,布鲁诺,谢谢你的回复。我喜欢那样做。不过有几个问题。1) 将设备令牌存储为AppDelegate上的属性是否存在安全风险?2) 在代码中,“(YourAppDelegate*)”为什么是“your”?我不明白。在我使用Xcode开发的每个应用程序中,它总是被简单地称为“AppDelegate”。你把“你的”放在那里,就像我应该把我的应用程序的名称放在单词的“…AppDelegate”部分之前一样。这让我感到困惑。1)我不认为这会有安全风险,因为你会将其存储在RAM内存中。但是如果你担心它,你可以做
self.token=nil保存安装后。2) 对不起,我仍然习惯于用老方法称呼应用程序代表。在Xcode 5之前(如果我没有记错的话),应用程序委托有另一个名称,比如“FacebookAppDelegate”。我正在编辑我的回复,以便对像你这样的新项目更方便。嘿,布鲁诺,谢谢你的回复。我喜欢那样做。不过有几个问题。1) 将设备令牌存储为AppDelegate上的属性是否存在安全风险?2) 在代码中,“(YourAppDelegate*)”为什么是“your”?我不明白。在我使用Xcode开发的每个应用程序中,它总是被简单地称为“AppDelegate”。你把“你的”放在那里,就像我应该把我的应用程序的名称放在单词的“…AppDelegate”部分之前一样。这让我感到困惑。1)我不认为这会有安全风险,因为你会将其存储在RAM内存中。但是如果你担心它,你可以做
self.token=nil保存安装后。2) 对不起,我仍然习惯于用老方法称呼应用程序代表。在Xcode 5之前(如果我没有记错的话),应用程序委托有另一个名称,比如“FacebookAppDelegate”。我正在编辑我的回复,以便于像你这样的新项目。嗨,安德里亚,谢谢你的回复。只是想澄清一下,我可能在我的代码中也犯了错误-但是“RegisterForRemotionTificationTypes”是否与“DidRegisterForRemotionTificationsSwithDeviceToken”相同?您不应该自己调用didRegister。。当您成功注册推送通知时,系统将调用此命令。这个过程就是你在需要的地方调用registerforemotentification,应用程序向苹果服务器请求推送令牌,一旦成功接收到它,就调用didRegisterforemotentification。要获得有效的代币,你必须要它。嗨,安德里亚,谢谢你的回复。只是想澄清一下,我可能在我的代码中也犯了错误-但是“RegisterForRemotionTificationTypes”是否与“DidRegisterForRemotionTificationsSwithDeviceToken”相同?您不应该自己调用didRegister。。当您成功注册推送通知时,系统将调用此命令。这个过程就是你在需要的地方调用registerforemotentification,应用程序向苹果服务器请求推送令牌,一旦成功接收到它,就调用didRegisterforemotentification。要获得有效令牌,您必须请求它。