ios - Disable Push Notification after New Installation of app without login -


i have situation logging app sending device token server , getting push notifications. if consumer logs out app sending request server not getting push notification. if user uninstall app after logging in can't send request server stop push notification install app again didn't login automatically i'm getting push notification.

so problem how stop push notification when user didn't login app

a bit late, might someone.

i faced similar situation in project of mine:

the problem

  1. first run gets device token
  2. user logs in , app registers device token server
  3. user uninstalls app without logging out hence server still thinks it's active
  4. user reinstalls app, , still receives push notifications though they're not logged in yet

in app register , request device token in app delegate:

- (bool)application:(uiapplication *)application didfinishlaunchingwithoptions:(nsdictionary *)launchoptions{        ..        [[uiapplication sharedapplication] registerforremotenotificationtypes:        (uiremotenotificationtypebadge | uiremotenotificationtypesound | uiremotenotificationtypealert)];        .. } 

and once reply received apple:

- (void)application:(uiapplication*)application didregisterforremotenotificationswithdevicetoken:(nsdata*)token{         //get trimmed device token         nsstring *devicetoken = [[token description] stringbytrimmingcharactersinset:[nscharacterset charactersetwithcharactersinstring:@"<>"]];         devicetoken = [devicetoken stringbyreplacingoccurrencesofstring:@" " withstring:@""];          //store device token in user defaults         nsuserdefaults *userdefaults = [nsuserdefaults standarduserdefaults];         [userdefaults setobject:devicetoken forkey:@"dtoken"];         [userdefaults synchronize];  } 

this more or less typical way i've seen done.

now when user logs in, device token sent server , registered main view controller of user content area. device token retrieved accessing user defaults within view controller. problem if user never logs in user content area, notifications still persist because device never unregistered server result of previous uninstall while still logged in.

the solution

there few approaches solving problem mentioned in other answers. didn't want modify server , main structure of registering devices there, goes like:

  • when user first logs in: authenticate user , register passed device token @ same time.
  • when user logs out: unregister device token user.
  • whenever device token exists in db: deactivate users except user logging in.

seeing user content area must have device token log in user, start requesting device token in didfinishlaunchingwithoptions did before.

now when reply received in didregisterforremotenotificationswithdevicetoken following:

- (void)application:(uiapplication*)application didregisterforremotenotificationswithdevicetoken:(nsdata*)token{         //get trimmed device token         nsstring *devicetoken = [[token description] stringbytrimmingcharactersinset:[nscharacterset charactersetwithcharactersinstring:@"<>"]];         devicetoken = [devicetoken stringbyreplacingoccurrencesofstring:@" " withstring:@""];          //store device token in user defaults         nsuserdefaults *userdefaults = [nsuserdefaults standarduserdefaults];         [userdefaults setobject:devicetoken forkey:@"dtoken"];         [userdefaults synchronize];          //if not logged in yet, unregister notifications         if(![[nsuserdefaults standarduserdefaults] objectforkey:@"loggedin"])             [[uiapplication sharedapplication] unregisterforremotenotifications];  } 

i check if user content view controller has not been reached yet, i.e. user never logged in, , disable notifications.

in user content view controller following:

- (void)viewdidload{     ..     //set logged-in notifications continue received     if(![[nsuserdefaults standarduserdefaults] objectforkey:@"loggedin"]){         [[nsuserdefaults standarduserdefaults] setvalue:@"done" forkey:@"loggedin"];         [[nsuserdefaults standarduserdefaults] synchronize];          //register notifications (again)         [[uiapplication sharedapplication] registerforremotenotificationtypes:          (uiremotenotificationtypebadge | uiremotenotificationtypesound | uiremotenotificationtypealert)];     }      //send user authentication , device token (retrieved user defaults) server     .. } 

now whenever user first logs in device token sent server , registered without delays because has been retrieved apple. notifications disabled until user logs in , therefore main problem solved.

this approach insures user content area view controller never loaded without device token saved in user defaults , therefore allows me continue using same method of authenticating user , passing device token @ once.


Comments

Popular posts from this blog

assembly - 8086 TASM: Illegal Indexing Mode -

Java, LWJGL, OpenGL 1.1, decoding BufferedImage to Bytebuffer and binding to OpenGL across classes -

javascript - addthis share facebook and google+ url -