Iphone 当应用程序关闭时,如何从后端发送电子邮件?
我正在制作一个电子邮件应用程序,用户可以在其中设置发送电子邮件的时间? 我能够在用户选择的时间在后台进程中发送电子邮件,但只有在应用程序打开时,在UILocalNotification的帮助下。 但我想在用户关闭应用程序后发送电子邮件。 例如:用户选择了10分钟后发送邮件的时间,并在此之前关闭了应用程序 请帮帮我 以下是我的代码:Iphone 当应用程序关闭时,如何从后端发送电子邮件?,iphone,ios,ios5,iphone-sdk-3.0,xcode4.2,Iphone,Ios,Ios5,Iphone Sdk 3.0,Xcode4.2,我正在制作一个电子邮件应用程序,用户可以在其中设置发送电子邮件的时间? 我能够在用户选择的时间在后台进程中发送电子邮件,但只有在应用程序打开时,在UILocalNotification的帮助下。 但我想在用户关闭应用程序后发送电子邮件。 例如:用户选择了10分钟后发送邮件的时间,并在此之前关闭了应用程序 请帮帮我 以下是我的代码: - (void)application:(UIApplication *)application didReceiveLocalNotification:(UILoc
- (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]);
}
如果应用程序已经关闭,则无法“安排”从应用程序发送电子邮件。您最好的选择是在后端服务器上处理所有这些内容。该应用程序存储消息应该发送到谁、什么、何时、何地。设置一个cronjob,通过需要发送的消息队列进行轮询,如果计划的时间匹配,则发送电子邮件。据我所知,当应用程序关闭时,不可能执行某些操作,并且没有可用的应用程序实例
我更愿意建议使用后端服务器在同一系统中实现推送通知。您可以在数据库中节省用户发送电子邮件的时间,并相应地编写用于在该时间发送电子邮件的cron。据我所知,一旦关闭应用程序,您将无法执行任何操作。您可以在后端服务器端执行此操作以自动执行操作