在应用程序委托之外注册远程通知

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

我能够把这些代码中的一些视图控制器的viewDidLoad方法,而不是我的应用程序委托? 我是否需要在我的应用程序委托中保留这两个底部方法“ didRegisterForRemoteNotificationsWithDeviceToken ”和“ didRegisterForRemoteNotificationsWithDeviceToken ”,还是应该将它们移动到我尝试注册远程通知的位置?

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

在我的应用程序委托的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 } 

我所访问的资源表明这块代码

您可以随时进行注册电话 – 只有当您知道应用程序中您希望用户接收推送通知时,才会这样做。

两个应用程序委托callback必须在您的应用程序委托中,因为您在应用程序委托上注册通知types,并且只有一个。 我会build议做一个应用程序委托方法来调用,然后做注册,你可以通过[[UIApplication sharedApplication] delegate]从视图控制器调用它(将该调用的结果转换到您的应用程序委托类)。

这个答案是肯德尔·赫尔姆斯泰特·格尔纳(Kendall Helmstetter Gelner)的回答。肯德尔的答案有0票,但是对我来说非常好,而我没有足够的积分来回答问题。 按照肯德尔的build议,我的视图控制器,CMRootViewController.m – >

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

和两个应用程序委托callback是在我的应用程序委托,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]; } 

谢谢肯德尔。

最好的方法是在您的应用程序委托方法中处理删除通知,使用NSNotificationCenter发出通知

 [[NSNotificationCenter defaultCenter] postNotification:@"remoteNotification" withObject:whateverYouWantHere]; 

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

SWIFT 3及以上版本

从AppDelegate之外调用推送通知

 import UserNotifications class HomeViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() let application = UIApplication.shared registerPushNotification(application) } func registerPushNotification(_ application: UIApplication){ UNUserNotificationCenter.current().requestAuthorization(options:[.badge, .alert, .sound]){ (granted, error) in if granted { print("Notification: Granted") } else { print("Notification: not granted") } } application.registerForRemoteNotifications() } } extension HomeViewController{ // Called when APNs has assigned the device a unique token func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) { // Convert token to string let deviceTokenString = deviceToken.reduce("", {$0 + String(format: "%02X", $1)}) // Print it to console print("APNs device token: \(deviceTokenString)") // Persist it in your backend in case it's new } // Called when APNs failed to register the device for push notifications func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) { // Print the error to console (you should alert the user that registration failed) print("APNs registration failed: \(error)") } // Push notification received func application(_ application: UIApplication, didReceiveRemoteNotification data: [AnyHashable : Any]) { // Print notification payload data print("Push notification received: \(data)") } } 

@Abishrek Mitra

您发布的解决scheme是否有效? 我曾试图做类扩展我想调用以下函数来检索设备令牌。 但是不叫。

唯一的方法是在AppDelegate中有这些function,我想避免。

 // Called when APNs has assigned the device a unique token func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) { // Convert token to string let deviceTokenString = deviceToken.reduce("", {$0 + String(format: "%02X", $1)}) // Print it to console print("APNs device token: \(deviceTokenString)") // Persist it in your backend in case it's new } // Called when APNs failed to register the device for push notifications func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) { // Print the error to console (you should alert the user that registration failed) print("APNs registration failed: \(error)") } // Push notification received func application(_ application: UIApplication, didReceiveRemoteNotification data: [AnyHashable : Any]) { // Print notification payload data print("Push notification received: \(data)") }