15

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

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

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

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

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

4

4 回答 4

19

您可以随时进行注册呼叫 - 只有当您在应用程序中知道您希望用户接收推送通知时才这样做是个好主意。

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

于 2013-06-19T04:39:43.253 回答
11

这个答案是对 Kendall Helmstetter Gelner 的“投票”回复(Kendall 的回答有 0 票赞成,但对我来说效果很好,而且我没有足够的分数来支持答案)。按照 Kendall 的建议,我的视图控制器 CMRootViewController.m ->

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

并且两个应用程序委托回调在我的应用程序委托 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];
}

谢谢你肯德尔。

于 2014-07-25T16:47:54.457 回答
5

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

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

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

于 2013-06-19T06:16:56.967 回答
1

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)")
    }
}
于 2017-10-10T14:57:57.913 回答