Remote notifications

Discussion in 'iOS Programming' started by DennisBlah, Feb 1, 2015.

  1. DennisBlah macrumors 6502


    Dec 5, 2013
    The Netherlands
    Hi all,

    Im finding some little issues when a users opens my app the first time and still has to accept the remote notifications.
    My app will store the token of the user in a public string of the AppDelegate

    However, when the user takes a while for accepting it, the app is already in its main viewcontroller. By then the app still didnt fetched the token. Once the user closed the app fully and opens it again, it did fetched the token and it can be stored on my host.

    Anyone understand my problem and how to fix this?
    Is there some kind of 'listener' I can set up that will be triggered once the app received an token?

  2. Boris-VTR macrumors regular

    Apr 18, 2013
    I would use something like Local Notifications. When user receives token, app would create and send notification witch custom name. Then you could create notifications listener that would react to that notification.

    When user receives token you create local notification:
    NSNotification *notification = [NSNotification      notificationWithName:recievedToken 
         userInfo: @"whatever data you save"];
    [[NSNotificationCenter defaultCenter] postNotification:notification];
    Then set up listener to this notification:
    NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
    [center addObserver: self
               selector: @selector(handleNotification)
               name: notification
               object:[[UIApplication sharedApplication] delegate)];
    -(void) handleNotification
        // Handle notification code here...

Share This Page