IOS local notification: I would like to execute a function when re-entering the app via the local notificaion

I am new to iOS programming and also working with XCode 4.6 and trying to create a local notification function that will execute the function when I re-enter the application. Basically, I would like to change the text on some shortcuts and add a sound when re-entering the application. I thought I was on the right track, but only some parts of my code work when I log into my app again via local notification.

I first added this function to my AppDelegate:

-(void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification{
    NSLog(@"test1");   //this traces successfully 
    myAppViewController * controller = [myAppViewController alloc];
    [controller doSomething];  //calling a function in my myAppViewController.m
}

      

I thought I figured it out, but now in my function in myAppViewController.m only NSLog works:

-(void)doSomething{
    NSLog(@"do something"); //traces successfully 
    self.notificationTime.text=@"something else"; //nothing happens here, it works in other functions but not here
    [self doSomethingElse]; //calling another function from this function for further testing 
}

      

The next function is called ....

-(void)doSomethingElse{
    NSLog(@"do something else"); //this works
    //this whole thing doesn't work -- no sound -- 
    NSURL* url = [[NSBundle mainBundle] URLForResource:@"cash" withExtension:@"mp3"];
    NSAssert(url, @"URL is valid.");
    self.player = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil];
    [self.player prepareToPlay];
    [self.player play];
    //this doesn't work again
    self.notificationTime.text=@"something else";
}

      

I was hoping for some general advice here and that would be greatly appreciated. If anyone knows a completely different way of solving the problem, that would be great!

+3


source to share


2 answers


You don't need to allocate a second instance of the app controller. You can just use self

. If you do this, does the code work as expected?



0


source


The method didReceiveLocalNotification

is only called when the application is launched in the foreground. If you see the icon and click "Application" to launch it, you need to handle the local notification using application:willFinishLaunchingWithOptions:

(or application:didFinishLaunchingWithOptions:

). To get local notification in either of these two methods, use the UIApplicationLaunchOptionsLocalNotificationKey

as key to the parameter dictionary.



Note that after extracting the local notification from the launch parameters, it is a viable approach to calling your method didReceiveLocalNotification

.

0


source







All Articles