在app delegate之外注册远程通知

use*_*064 14 iphone objective-c push-notification ios

到目前为止,我所看到的一切都表明我会在我的设置中设置推送通知警报AppDelegate.但是,我的应用程序要求用户完成注册过程,并且我不想询问用户是否希望接收推送通知,除非用户已经到达viewController注册过程完成后出现的推送通知.

我可以将一些代码放在viewDidLoad除我的应用程序委托之外的视图控制器的方法中吗?我是否需要在我的应用程序委托中保留这两个底部方法" didRegisterForRemoteNotificationsWithDeviceToken"和" didReceiveRemoteNotification",还是应该将它们移动到我尝试注册远程通知的任何地方?

我正在我的应用程序中使用下面的代码块注册推送通知:

在我的app委托的didFinishLaunchingWithOptions方法中:

[application registerForRemoteNotificationTypes:UIRemoteNotificationTypeBadge|
                                                UIRemoteNotificationTypeAlert|
                                                UIRemoteNotificationTypeSound];
Run Code Online (Sandbox Code Playgroud)

我的app委托中添加的方法:

- (void)application:(UIApplication *)application
        didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
{
    // Store the deviceToken
}

- (void)application:(UIApplication *)application 
        didReceiveRemoteNotification:(NSDictionary *)userInfo {
    //handle push notification
}
Run Code Online (Sandbox Code Playgroud)

我访问过的资源表明这段代码

Ken*_*ner 18

您可以随时进行注册呼叫 - 当您在应用中知道您希望用户接收推送通知时,最好这样做.

但是,当您在应用程序委托上注册通知类型时,两个应用程序委托回调必须在您的应用程序委托中,并且您只有一个.我建议使用一个应用程序委托方法来调用然后进行注册,你可以从你的视图控制器调用它[[UIApplication sharedApplication] delegate](将该调用的结果转换为你的应用程序委托类).


tmr*_*tmr 11

这个答案是肯德尔·赫尔姆斯特特·格尔纳(Kendall Helmstetter Gelner)的"投票结果"回复(肯德尔的答案有0票,但对我来说效果很好,我没有足够的分数来表达答案).按照Kendall的建议,我的视图控制器,CMRootViewController.m - >

#pragma mark - push notificaiton
-(void)registerToReceivePushNotification {
    // Register for push notifications
    UIApplication* application =[UIApplication sharedApplication];
    [application registerForRemoteNotificationTypes:
     UIRemoteNotificationTypeBadge |
     UIRemoteNotificationTypeAlert |
     UIRemoteNotificationTypeSound];
}
Run Code Online (Sandbox Code Playgroud)

并且两个应用程序委托回调在我的app委托中,CMAppDelegate.m - >

// handle user accepted push notification, update parse
- (void)application:(UIApplication *)application
didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)newDeviceToken {
    // Store the deviceToken in the current installation and save it to Parse.
    PFInstallation *currentInstallation = [PFInstallation currentInstallation];
    [currentInstallation setDeviceTokenFromData:newDeviceToken];

    // enable future push to deviceId
    NSUUID *identifierForVendor = [[UIDevice currentDevice] identifierForVendor];
    NSString* deviceId = [identifierForVendor UUIDString];
    [currentInstallation setObject:deviceId forKey:@"deviceId"];

    [currentInstallation saveInBackground];
}


// handle push notification arrives when app is open
- (void)application:(UIApplication *)application
didReceiveRemoteNotification:(NSDictionary *)userInfo {
    [PFPush handlePush:userInfo];
}
Run Code Online (Sandbox Code Playgroud)

谢谢Kendall.


Dip*_*ara 5

最好的方法是,在您的app委托方法中处理删除通知,使用发送通知 NSNotificationCenter

[[NSNotificationCenter defaultCenter] postNotification:@"remoteNotification" withObject:whateverYouWantHere];
Run Code Online (Sandbox Code Playgroud)

然后使用NSNotificationCenter添加任何感兴趣的UIViewControllers作为remoteNotification通知的观察者.