About APNS Device Token.

Discussion in 'iOS Programming' started by cthesky, Nov 27, 2011.

  1. cthesky macrumors member

    Joined:
    Aug 21, 2011
    #1
    Hi all,

    To implement Apple Push Notification, I need register my Iphone Application to APNS in didFinishLaunchingWithOptions method and then get the Device Token in didRegisterForRemoteNotificationsWithDeviceToken.

    When I run my application for first time, the didFinishLaunchingWithOptions will get called. But when I run my application for second time, the didFinishLaunchingWithOptions will not get called. So, it seems my application just register one time to APNS that is the first time I run the application. So, what I worry about is if the device token changed next time, it seems I cannot get the latest device token although I had run my application.

    Anyone have any idea on this? or I am thinking something wrong?

    Any idea and comments are welcome. Thanks a lot :)
     
  2. jnoxx macrumors 65816

    jnoxx

    Joined:
    Dec 29, 2010
    Location:
    Aartselaar // Antwerp // Belgium
    #2
    Be carefull what you say, you are saying your didFinishLaunching won't run the second time.
    that statement is FALSE.
    Since you didn't define if your app is hard killed on your "task-bar", or if it's being run again fully.
    If it's still in your task bar, then there are other delegate methods. to listen for your app to return to it's active state in your applicationDelegate.
     
  3. cthesky thread starter macrumors member

    Joined:
    Aug 21, 2011
    #3
    Hi,

    Thanks for your reply. :)

    In my case is like this, when I first time run my application, the didFinishLaunching will get called. Let's say in this first run, I stop at "Login page". Then I press Home button in my Iphone. Next time, when I run my application again, I will saw a page I last visited (in this case is Login Page). This mean my apps does not start over again from first loading page. So, the didFinishLaunching will not get called for the second time I run my application. But the applicationDidBecomeActive method will get called everytime.

    So, what I think is is it suitable I register my application to APNS in applicationDidBecomeActive method? This is because a lot of code sample is register application APNS in didFinishLaunching method.

    Hope anyone can give me some advices. Thanks a lot. :)
     
  4. jnoxx macrumors 65816

    jnoxx

    Joined:
    Dec 29, 2010
    Location:
    Aartselaar // Antwerp // Belgium
    #4
    It's best pratice to only send it to the server in the didFinishLaunching, why would they need to send a new token if the app is still active?
    Just curious since it's best practice to just do it in the DidFinishLaunching
     
  5. cthesky thread starter macrumors member

    Joined:
    Aug 21, 2011
    #5
    Thanks for your reply. :)

    Let's say if my application still in "task bar" for a few weeks and I no delete it from "task bar". So, is it possible in this period the device token will change?

    (In this period, when I tapped my application icon, my application will run without called didFinishLaunching method).

    Am I thinking something wrong?
     
  6. jnoxx macrumors 65816

    jnoxx

    Joined:
    Dec 29, 2010
    Location:
    Aartselaar // Antwerp // Belgium
    #6
    your device token doesn't change.. This is a special token needed for APNS, so that won't change at all.
    If you register, you will generate it, and add it to your backend.
    BUT, i don't know much bout the backend part..
     
  7. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #7
    It can change, but very rarely. As stated in the Local and Push Notification Programming Guide:
     
  8. jnoxx macrumors 65816

    jnoxx

    Joined:
    Dec 29, 2010
    Location:
    Aartselaar // Antwerp // Belgium
    #8
    I'm sorry, I meant in terms of the APNS.
    Every time you start your app, you need to send a token, which is not your device Token.
    Might be confusing, but there is a difference, and I'm not sure if I stated it right, but in my head it all figures out >.< x)
     
  9. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #9
    Everytime you start your app, you should register for remote notifications, via the registerForRemoteNotificationTypes: call. No tokens needed.
     
  10. jnoxx macrumors 65816

    jnoxx

    Joined:
    Dec 29, 2010
    Location:
    Aartselaar // Antwerp // Belgium
    #10
    :( Then i'm majorly confused myself, will need to need to check my code later. Sorry for confusion in meanwhile.
    Think i'm messing up 'registerForRemoteNotificationTypes' and 'didRegister..'
    since, registering isn't the problem, when you get a callback of didRegister, then you have the devicetoken as NSData, and that's what I meant, or I'm just messing up OP's original question..
     
  11. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #11
    didRegisterForRemoteNotificationsWithDeviceToken: does pass in a device token, which you are expected to pass on to your server. Perhaps you are confusing the deviceToken with the UDID.
     
  12. jnoxx macrumors 65816

    jnoxx

    Joined:
    Dec 29, 2010
    Location:
    Aartselaar // Antwerp // Belgium
    #12
    I know what you are saying, you are preaching to the wrong choir (this is an expression you guys use right?)

    But it's to the OP's quote
    He was thinking the device token, that's what i was trying to explain.
     
  13. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #13
    To which the answer is: no.
     

Share This Page