gpt4 book ai didi

ios - 不是为 LocalNotification 调用的后台方法

转载 作者:行者123 更新时间:2023-11-28 21:43:04 26 4
gpt4 key购买 nike

如果我从后台强行关闭我的应用程序。然后本地通知来了。如果点击本地通知,当应用程序在前台运行时我的方法不会被调用。我在 iOS 中较新。请帮忙。

-(void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forLocalNotification:(UILocalNotification *)notification completionHandler:(void (^)())completionHandler
{
[[NSNotificationCenter defaultCenter] postNotificationName:@"Reh" object:nil];

[[UIApplication sharedApplication] setApplicationIconBadgeNumber: 0];
[[UIApplication sharedApplication] cancelAllLocalNotifications];
UIApplicationState state = [application applicationState];

if (state == UIApplicationStateActive) {

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Reminder"

message:notification.alertBody

delegate:self cancelButtonTitle:@"OK"

otherButtonTitles:nil,nil];





[alert show];


NSLog(@"%@",notification.soundName);






// AudioServicesPlaySystemSound (1010);




MyNotificationViewController *profile=[[MyNotificationViewController alloc]initWithNibName:@"MyNotificationViewController" bundle:nil];
[[NSNotificationCenter defaultCenter] postNotificationName:@"RefreshFoeByPush" object:nil];

self.viewController = [[SWRevealViewController alloc] initWithRearViewController:self.leftMenuController frontViewController:profile]; self.viewController.rightViewController=nil;
[UIView transitionWithView:self.window duration:0.5 options:UIViewAnimationOptionCurveEaseInOut
animations:^{self.window.rootViewController = self.viewController;} completion:nil];






application.applicationIconBadgeNumber = 0;

}
else

{
NSString *tokend= [[NSUserDefaults standardUserDefaults] stringForKey:@"token"];
if (tokend == (id)[NSNull null] || tokend.length == 0 )
{

}
else
{
MyNotificationViewController *profile=[[MyNotificationViewController alloc]initWithNibName:@"MyNotificationViewController" bundle:nil];
[[NSNotificationCenter defaultCenter] postNotificationName:@"RefreshFoeByPush" object:nil];

self.viewController = [[SWRevealViewController alloc] initWithRearViewController:self.leftMenuController frontViewController:profile]; self.viewController.rightViewController=nil;
[UIView transitionWithView:self.window duration:0.5 options:UIViewAnimationOptionCurveEaseInOut
animations:^{self.window.rootViewController = self.viewController;} completion:nil];


}
}

}

最佳答案

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

UILocalNotification *localNotification = [launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];
if (localNotification != nil) {
[self showLocalNotificationAlert:localNotification];
}

return YES;
}


- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
[self showLocalNotificationAlert:notification];
}


-(void)showLocalNotificationAlert:(UILocalNotification *)notification {
// handle here what you want
}

还有

当本地通知触发 didReceiveLocalNotification 方法时调用不是 handleActionWithIdentifier

是的,把你的东西放在通用方法中 -(void)showLocalNotificationAlert:(UILocalNotification *)notification 所以你只需要调用

  1. 当应用程序在前台时,didReceiveLocalNotification 将被调用。

  2. 当应用程序不在前台并且您点击通知时,可以从 didFinishLaunchingWithOptions

  3. 获取此通知对象

用于远程通知

didFinishLaunchingWithOptions

NSDictionary *remoteNotification = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
if (remoteNotification) {
[self showRemoteNotificationAlert:remoteNotification];
}

Dictionary contains payload for remotenotification

并且还制作了远程通知fire和远程通知taped的通用方法。

哈哈

关于ios - 不是为 LocalNotification 调用的后台方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31317335/

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