gpt4 book ai didi

ios - iOS 中的通知操作后台获取?

转载 作者:行者123 更新时间:2023-11-29 11:58:31 24 4
gpt4 key购买 nike

我使用这段代码在推送通知中将应用程序从接近后台唤醒

UIMutableUserNotificationAction *action2;
action2 = [[UIMutableUserNotificationAction alloc] init];
[action2 setActivationMode:UIUserNotificationActivationModeBackground];////UIUserNotificationActivationModeBackground
[action2 setTitle:@"ACCEPT"];
[action2 setIdentifier:NotificationActionTwoIdent];
[action2 setDestructive:NO];
[action2 setAuthenticationRequired:NO];

现在我通过以下方法访问网络服务//远程通知

- (void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forRemoteNotification:(NSDictionary *)userInfo completionHandler:(void (^)())completionHandler 
{
/Call to web services.
//show local notification in background

UILocalNotification *localNotification = [[UILocalNotification alloc] init];
if (localNotification == nil)
{
return;
}
else
{
localNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:60];
localNotification.alertAction = nil;
localNotification.soundName = UILocalNotificationDefaultSoundName;
localNotification.alertBody = [NSString stringWithFormat:@"Worked=> %@",jsonArray];
localNotification.alertAction = NSLocalizedString(@"Read Msg", nil);
localNotification.applicationIconBadgeNumber=1;
localNotification.repeatInterval=0;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
}
}

我在应用程序中启用了后台获取。我的问题是

  1. 我正在使用 NSURLConnetion 类进行网络调用。它有时有效,但有时似乎无效。
  2. 通知提醒未在后台显示。
  3. 我需要使用 performFetchWithCompletionHandler completionHandler 委托(delegate)吗?任何人都可以帮我解决这个问题吗?提前致谢。

最佳答案

我是这样实现的:

- (void)application:(UIApplication *)application handleEventsForBackgroundURLSession:(NSString *)identifier
completionHandler:(void (^)())completionHandler {
self.backgroundSessionCompletionHandler = completionHandler;

//add notification
[self presentNotification];
}

-(void)presentNotification{
UILocalNotification* localNotification = [[UILocalNotification alloc] init];
localNotification.alertBody = @"Upload Complete!";
localNotification.alertAction = @"Background Transfer Upload!";

//On sound
localNotification.soundName = UILocalNotificationDefaultSoundName;

//increase the badge number of application plus 1
localNotification.applicationIconBadgeNumber = [[UIApplication sharedApplication] applicationIconBadgeNumber] + 1;

[[UIApplication sharedApplication] presentLocalNotificationNow:localNotification];
}

关于ios - iOS 中的通知操作后台获取?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38067864/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com