xcode - iOS Local Notification Action in Background -



xcode - iOS Local Notification Action in Background -

i'm building application when user receives local notification charges credit card via stripe , parse, there more thats start. when notification received within app works fine when notification received outside app action not complete.

https://github.com/jackintosh7/wake

i action finish outside app , view brought when user clicks on notification.

- (bool)application:(uiapplication *)application didfinishlaunchingwithoptions:(nsdictionary *)launchoptions { if ([uiapplication instancesrespondtoselector:@selector(registerusernotificationsettings:)]){ [application registerusernotificationsettings:[uiusernotificationsettings settingsfortypes:uiusernotificationtypealert|uiusernotificationtypebadge|uiusernotificationtypesound categories:nil]]; } self.window = [[uiwindow alloc] initwithframe:uiscreen.mainscreen.bounds]; uistoryboard *storyboard = [uistoryboard storyboardwithname:@"main" bundle:nil]; if (stripepublishablekey) { [stripe setdefaultpublishablekey:stripepublishablekey]; } if (parseapplicationid && parseclientkey) { [parse setapplicationid:parseapplicationid clientkey:parseclientkey]; } if ([[nsuserdefaults standarduserdefaults] boolforkey:@"customer created"]) { uiviewcontroller *viewcontroller = [storyboard instantiateviewcontrollerwithidentifier:@"home"]; self.window.rootviewcontroller = viewcontroller; } else { [[nsuserdefaults standarduserdefaults] setbool:yes forkey:@"customer created"]; [[nsuserdefaults standarduserdefaults] synchronize]; uiviewcontroller *viewcontroller = [storyboard instantiateviewcontrollerwithidentifier:@"tutorial"]; self.window.rootviewcontroller = viewcontroller; } [self.window makekeyandvisible]; uilocalnotification *notification = [launchoptions valueforkey:uiapplicationlaunchoptionslocalnotificationkey]; if (notification) { [self application:application didreceivelocalnotification:notification]; } homecoming yes; } - (void)applicationwillresignactive:(uiapplication *)application { nslog(@"1"); // sent when application move active inactive state. can occur types of temporary interruptions (such incoming phone phone call or sms message) or when user quits application , begins transition background state. // utilize method pause ongoing tasks, disable timers, , throttle downwards opengl es frame rates. games should utilize method pause game. } - (void)applicationdidenterbackground:(uiapplication *)application { // utilize method release shared resources, save user data, invalidate timers, , store plenty application state info restore application current state in case terminated later. // if application supports background execution, method called instead of applicationwillterminate: when user quits. } - (void)applicationwillenterforeground:(uiapplication *)application { // called part of transition background inactive state; here can undo many of changes made on entering background. } - (void)applicationdidbecomeactive:(uiapplication *)application { // restart tasks paused (or not yet started) while application inactive. if application in background, optionally refresh user interface. } - (void)applicationwillterminate:(uiapplication *)application { // saves changes in application's managed object context before application terminates. [self savecontext]; } - (void)savecontext { nserror *error = nil; nsmanagedobjectcontext *managedobjectcontext = self.managedobjectcontext; if (managedobjectcontext != nil) { if ([managedobjectcontext haschanges] && ![managedobjectcontext save:&error]) { // replace implementation code handle error appropriately. // abort() causes application generate crash log , terminate. should not utilize function in shipping application, although may useful during development. nslog(@"unresolved error %@, %@", error, [error userinfo]); abort(); } } } #pragma mark - core info stack // returns managed object context application. // if context doesn't exist, created , bound persistent store coordinator application. - (nsmanagedobjectcontext *)managedobjectcontext { if (_managedobjectcontext != nil) { homecoming _managedobjectcontext; } nspersistentstorecoordinator *coordinator = [self persistentstorecoordinator]; if (coordinator != nil) { _managedobjectcontext = [[nsmanagedobjectcontext alloc] init]; [_managedobjectcontext setpersistentstorecoordinator:coordinator]; } homecoming _managedobjectcontext; } // returns managed object model application. // if model doesn't exist, created application's model. - (nsmanagedobjectmodel *)managedobjectmodel { if (_managedobjectmodel != nil) { homecoming _managedobjectmodel; } nsurl *modelurl = [[nsbundle mainbundle] urlforresource:@"alarmmodel" withextension:@"mom"]; _managedobjectmodel = [[nsmanagedobjectmodel alloc] initwithcontentsofurl:modelurl]; homecoming _managedobjectmodel; } // returns persistent store coordinator application. // if coordinator doesn't exist, created , application's store added it. - (nspersistentstorecoordinator *)persistentstorecoordinator { if (_persistentstorecoordinator != nil) { homecoming _persistentstorecoordinator; } nsurl *storeurl = [[self applicationdocumentsdirectory] urlbyappendingpathcomponent:@"alarmmodel.sqlite"]; nserror *error = nil; _persistentstorecoordinator = [[nspersistentstorecoordinator alloc] initwithmanagedobjectmodel:[self managedobjectmodel]]; if (![_persistentstorecoordinator addpersistentstorewithtype:nssqlitestoretype configuration:nil url:storeurl options:nil error:&error]) { /* replace implementation code handle error appropriately. abort() causes application generate crash log , terminate. should not utilize function in shipping application, although may useful during development. typical reasons error here include: * persistent store not accessible; * schema persistent store incompatible current managed object model. check error message determine actual problem was. if persistent store not accessible, there typically wrong file path. often, file url pointing application's resources directory instead of writeable directory. if encounter schema incompatibility errors during development, can cut down frequency by: * deleting existing store: [[nsfilemanager defaultmanager] removeitematurl:storeurl error:nil] * performing automatic lightweight migration passing next dictionary options parameter: @{nsmigratepersistentstoresautomaticallyoption:@yes, nsinfermappingmodelautomaticallyoption:@yes} lightweight migration work limited set of schema changes; consult "core info model versioning , info migration programming guide" details. */ nslog(@"unresolved error %@, %@", error, [error userinfo]); abort(); } homecoming _persistentstorecoordinator; } - (void)application:(uiapplication *)application didreceivelocalnotification:(uilocalnotification *)notification { dispatch_async(dispatch_get_main_queue(), ^{ uialertview *alertview = [[uialertview alloc] initwithtitle:@"wake" message:notification.alertbody delegate:nil cancelbuttontitle:@"ok" otherbuttontitles: nil]; [alertview show]; }); nsstring *customerid = @"cus_4ot6ggkuop6bhg"; nsnumber *amountincents = [nsnumber numberwithinteger: 1000]; [self chargecustomer:customerid amount:(nsnumber *)amountincents completion:^(id object, nserror *error) { }]; nslog(@"11"); } -(void)chargecustomer:(nsstring *)customerid amount:(nsnumber *)amountincents completion:(pfidresultblock)handler { nslog(@"22"); [pfcloud callfunctioninbackground:@"chargecustomer" withparameters:@{ @"amount":amountincents, @"customerid":customerid } block:^(id object, nserror *error) { //object nsdictionary contains stripe charge information, can utilize or create, instance of own charge class. handler(object,error); }]; } #pragma mark - application's documents directory // returns url application's documents directory. - (nsurl *)applicationdocumentsdirectory { homecoming [[[nsfilemanager defaultmanager] urlsfordirectory:nsdocumentdirectory indomains:nsuserdomainmask] lastobject]; } @end

action:

- (void)application:(uiapplication *)application didreceivelocalnotification:(uilocalnotification *)notification { uialertview *alertview = [[uialertview alloc] initwithtitle:@"wake" message:notification.alertbody delegate:self cancelbuttontitle:@"ok" otherbuttontitles: nil]; [alertview show]; nsstring *customerid = @"cus_4ot6ggkuop6bhg"; nsnumber *amountincents = [nsnumber numberwithinteger: 1000]; [appdelegate chargecustomer:customerid amount:(nsnumber *)amountincents completion:^(id object, nserror *error) { }]; nslog(@"11"); } +(void)chargecustomer:(nsstring *)customerid amount:(nsnumber *)amountincents completion:(pfidresultblock)handler { nslog(@"22"); [pfcloud callfunctioninbackground:@"chargecustomer" withparameters:@{ @"amount":amountincents, @"customerid":customerid } block:^(id object, nserror *error) { //object nsdictionary contains stripe charge information, can utilize or create, instance of own charge class. handler(object,error); }]; }

result of nslogs added: see charge information:(null) error:invalid_request_error: no such customer: cus_4ot6ggkuop6bhg (code: 141, version: 1.4.1) lastly 1 understand issue is.

when app running application:didreceivelocalnotification: calling, if app not running, info local notifications added launchoptions dict;

in appdelegate, in application:didfinishlaunchingwithoptions add together code:

-(bool)application:(uiapplication *)application didfinishlaunchingwithoptions:(nsdictionary *)launchoptions { // maintain al exist code in app...and @ end of methods uilocalnotification *localnotification = [launchoptions valueforkey:uiapplicationlaunchoptionslocalnotificationkey]; if (localnotification) { [self application:application didreceivelocalnotification:localnotification]; } homecoming yes; }

it´s thing forcefulness uialert in main thread,:

dispatch_async(dispatch_get_main_queue(), ^{ uialertview *alertview = [[uialertview alloc] initwithtitle:@"wake" message:notification.alertbody delegate:self cancelbuttontitle:@"ok" otherbuttontitles: nil]; [alertview show]; });

new proposal:

- (void)application:(uiapplication *)application didreceivelocalnotification:(uilocalnotification *)notification { dispatch_async(dispatch_get_main_queue(), ^{ uialertview *alertview = [[uialertview alloc] initwithtitle:@"wake" message:notification.alertbody delegate:nil cancelbuttontitle:@"ok" otherbuttontitles: nil]; [alertview show]; nsstring *customerid = @"cus_4ot6ggkuop6bhg"; nsnumber *amountincents = [nsnumber numberwithinteger: 1000]; [self chargecustomer:customerid amount:(nsnumber *)amountincents completion:^(id object, nserror *error) { }]; });

}

and :

-(void)chargecustomer:(nsstring *)customerid amount:(nsnumber *)amountincents completion:(pfidresultblock)handler { [pfcloud callfunctioninbackground:@"chargecustomer" withparameters:@{ @"amount":amountincents, @"customerid":customerid } block:^(id object, nserror *error) { //object nsdictionary contains stripe charge information, can utilize or create, instance of own charge class. handler(object,error); nslog(@"see error:%@",[error localizeddescription]); nslog(@"see charge information:%@",[object description]); }]; }

ios xcode parse.com uilocalnotification stripe-payments

Comments

Popular posts from this blog

assembly - What is the addressing mode for ld, add, and rjmp instructions? -

vowpalwabbit - Interpreting Vowpal Wabbit results: Why are some lines appended by "h"? -

Is there a way to convert an HTML page styled with Bootstrap CSS into email-compatible html? -