Tommz
Tommz

Reputation: 3453

How to get notification when push notification is received in iOS without AppDelegate?

I'm building static library for iOS for push notifications. How can I find out when push notification is received without having access to AppDelegate? In other words, I don't want user to call some libraries delegate in order to tell to lib when push notiication is received. AppDelegate has this method didRegisterForRemoteNotificationsWithDeviceToken and it would be perfect if I could receive notification when that method is being called. I thought NSNotificationCenter must be solution, but there is no such notification that can be received through adding observer to NSNotificaitonCenter which will be fired when push notification is received. Or am I wrong?

However, there is one solution, not the cleanest one I must admit, where one can receive AppDelegate being set by user, save it to property and make new AppDelegate that will route everything to previously set AppDelegate. (on dealloc, property should be updated) But, that would mean that only one library can do that, so I don't think that's the way to do it. Here's code for it:

{
    ....
    self.applicationDelegate = [UIApplication sharedApplication].delegate;
    [UIApplication sharedApplication].delegate = self;
}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    return [self.applicationDelegate application:application didFinishLaunchingWithOptions:launchOptions];
}

Upvotes: 2

Views: 289

Answers (1)

Cy-4AH
Cy-4AH

Reputation: 4615

I think you need subclass in your's library UIResponder <UIApplicationDelegate>, implement in it didRegisterForRemoteNotificationsWithDeviceToken. And make user of yours library to subclass yours appdelegate class.

Upvotes: 1

Related Questions