注册app delegate之外的远程通知 [英] Register for remote notifications outside of app delegate

查看:119
本文介绍了注册app delegate之外的远程通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

到目前为止我看到的所有内容都表明我会在 AppDelegate 中设置推送通知提醒。但是,我的应用程序要求用户完成注册过程,我不想询问用户是否希望接收推送通知,除非用户已到达 viewController 在注册过程完成后出现。

Everything I have seen thus far indicates that I would setup a push notification alert in my AppDelegate. However, my app requires that the user goes through a registration process, and I do not want to ask the user if they would like to receive push notifications unless the user has arrived on the viewController that appears after the registration process is complete.

我能否将部分代码放入 viewDidLoad 我的应用程序委托以外的视图控制器的方法?我是否需要在我的应用代表中保留这两个底部方法 didRegisterForRemoteNotificationsWithDeviceToken didReceiveRemoteNotification ,或者我应该移动他们到我试图注册远程通知的哪个地方?

Am I able to put some of this code in the viewDidLoad method of a view controller other than my app delegate? Do I need to leave those two bottom methods "didRegisterForRemoteNotificationsWithDeviceToken" and "didReceiveRemoteNotification" in my app delegate or should I move them to wherever I try to register for remote notifications?

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

I am registering for push notifications in my app with the blocks of code below:

在我的app delegate的didFinishLaunchingWithOptions方法中:

[application registerForRemoteNotificationTypes:UIRemoteNotificationTypeBadge|
                                                UIRemoteNotificationTypeAlert|
                                                UIRemoteNotificationTypeSound];

我的应用代表中添加的方法:

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

- (void)application:(UIApplication *)application 
        didReceiveRemoteNotification:(NSDictionary *)userInfo {
    //handle push notification
}

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

The resources I have visited indicate that this block of code

推荐答案

您可以随时进行注册通话 - 最好只在您希望用户知道应用时才这样做接收推送通知。

You can do the registration call at any time - and it is a good idea to only do so when you know in the app you would like the user to receive push notifications.

两个应用程序委托回调必须在您的应用程序委托中,因为您在应用程序委托上注册通知类型而您只有一个。我建议创建一个应用程序委托方法来调用然后进行注册,你可以通过 [[UIApplication sharedApplication]委托] 从你的视图控制器调用它(投射结果调用你的应用程序委托类。)

The two application delegate callbacks have to be in your app delegate though, as you register for notification types on the application delegate and you only have one. I would suggest making an application delegate method to call that then does the registration, you could call it from your view controller through [[UIApplication sharedApplication] delegate] (cast the result of that call to your application delegate class).

这篇关于注册app delegate之外的远程通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆