Question

i am making an email application,where user can set his time to send email? I am able to send an email in a background process at the user selected time,but only when the application is on,with the help of UILocalNotification. but i want to send the email when user have closed the appliaction. For example:user have selected time for the mail to be send after 10 min and have closed his app before that.

please help me out

following is my code:

- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{

    NSLog(@"didReceiveLocalNotification");

    NSLog(@"\nNotification dic = %@ %@",notification.userInfo,notification.alertBody);

    dictUser=notification.userInfo;
    [dictUser retain];

    NSLog(@"dictuser124:%@",dictUser);
    [dictUser retain];

    if ([str_info4 isEqualToString:@"0"]) 
    {
        [self sendEMAIL];
    }
    else if ([str_info4 isEqualToString:@"1"]) 
    {
        [self sendSMS];
    }

}

-(void)sendEMAIL
{
    [dictUser retain];

    SKPSMTPMessage *testMsg = [[SKPSMTPMessage alloc] init];


    //testMsg.fromEmail = @"Lexi mobile";//nimit51parekh@gmail.com

    testMsg.fromEmail = str_uname;
    NSLog(@"str_Uname=%@",testMsg.fromEmail);

    str_info = [str_info stringByReplacingOccurrencesOfString:@"," withString:@""];
    testMsg.toEmail = str_info;
    NSLog(@"autoemail=%@",testMsg.toEmail);


    testMsg.relayHost = @"smtp.gmail.com";


    testMsg.requiresAuth = YES;


    testMsg.login = str_uname;
    NSLog(@"autoelogin=%@",testMsg.login);

    testMsg.pass = str_password;
    NSLog(@"autopass=%@",testMsg.pass);

    testMsg.subject = @"Schedule Sms And Email";


    testMsg.wantsSecure = YES; 

    NSString *sendmsg=[[NSString alloc]initWithFormat:@"%@",str_info2];
    NSLog(@"automsg=%@",sendmsg);

    testMsg.delegate = self;


    NSDictionary *plainPart = [NSDictionary dictionaryWithObjectsAndKeys:@"text/plain",kSKPSMTPPartContentTypeKey,

                             sendmsg,kSKPSMTPPartMessageKey,@"8bit",kSKPSMTPPartContentTransferEncodingKey,nil];

    testMsg.parts = [NSArray arrayWithObjects:plainPart,nil];


    [testMsg send];

   // [self DeleteRowAfterSending];
    [self performSelector:@selector(DeleteRowAfterSending) withObject:nil afterDelay:5.0];
}



-(void)messageSent:(SKPSMTPMessage *)message

{
    [message release];

    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Success" message:@"Your email is sent successfully" delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil];

    [alert show];

    [alert release];

    NSLog(@"delegate - message sent");

}

-(void)messageFailed:(SKPSMTPMessage *)message error:(NSError *)error{

    [message release];

    // open an alert with just an OK button

    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Fail" message:@"Message sending failure" delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil];

    [alert show];

    [alert release];


    NSLog(@"delegate - error(%d): %@", [error code], [error localizedDescription]);

}
Was it helpful?

Solution

As per my knowledge I know this that its kind of not possible to execute something when application is closed and there will be no instance of the application available.

I would rather suggest the implementation of push notifications in the same using the SERVER at backend. You can save the time in database during which the user has to send the email and accordingly write the CRONS for sending the email at that moment of time.

OTHER TIPS

As of my knowledge you cannot do any operation once the app is closed. You can work this on the back end SERVER side to perform operation automatically.

Theres no way to 'Schedule' an email to send FROM the app, if it's closed already. Your best option is to handle all this stuff on a backend server. The app stores who,what,when,where the message should be sent to. Setup a cronjob that polls through the queue of messages that need to be sent, if the scheduled time matches, send the email.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top