gpt4 book ai didi

ios - iOS 应用程序上的多个通知

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

在我的应用程序中,我需要发送第一个通知(应用程序启动时)和用户按下按钮时的第二个通知。为了发送第一个通知,我实现了以下代码:

- (void)viewDidAppear:(BOOL)animated {

UILocalNotification *localNotification = [[UILocalNotification alloc]init];
localNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:5];
localNotification.alertBody = @"My notification text";
localNotification.alertAction = @"text";
localNotification.soundName = UILocalNotificationDefaultSoundName;
localNotification.timeZone = [NSTimeZone localTimeZone];
localNotification.userInfo = @{@"ID": @"Geo"};
localNotification.applicationIconBadgeNumber = [[UIApplication sharedApplication] applicationIconBadgeNumber] + 1;

[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
}

对于第二个通知,我实现了以下代码:

- (IBAction)buttonDone:(UIButton *)sender {
[self.buttonDone setEnabled:NO];
UILocalNotification *localNotification = [[UILocalNotification alloc]init];
localNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:5];
localNotification.alertBody = @"My notification text";
localNotification.alertAction = @"text";
localNotification.soundName = UILocalNotificationDefaultSoundName;
localNotification.timeZone = [NSTimeZone localTimeZone];
localNotification.userInfo = @{@"ID": @"Pay"};
localNotification.applicationIconBadgeNumber = [[UIApplication sharedApplication] applicationIconBadgeNumber] + 1;

[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
}

现在,当我在模拟器上运行该应用程序时,我遇到了以下问题:该应用程序已启动并向我发送了(5 秒后)第一个通知,但我没有在状态栏上看到横幅,当我去通知中心我看不到我收到的通知,但是应用程序打开了在按下通知后应该打开的 View Controller 。当我按下第二个通知的按钮时,一切正常。在我的 AppDelegate.m 中,我插入了以下代码:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
[[UINavigationBar appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName: [UIColor whiteColor]}];
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
[application registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound categories:nil]];

UILocalNotification *localNotification = [launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];
if (localNotification) {
application.applicationIconBadgeNumber = 0;
}
application.applicationIconBadgeNumber = 0;

return YES;
}

我插入了以下方法来响应通知:

- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification {
UIApplicationState state = [application applicationState];

NSString *ident = [notification.userInfo objectForKey:@"ID"];

if (state == UIApplicationStateActive) {
[[UIApplication sharedApplication] cancelAllLocalNotifications];
if ([ident isEqualToString:@"Geo"]) {
ViewController *notificationVC = [self.window.rootViewController.storyboard instantiateViewControllerWithIdentifier:@"Local"];
[self.window setRootViewController:notificationVC];
} else if ([ident isEqualToString:@"Pay"]) {
ViewController *notificationVC = [self.window.rootViewController.storyboard instantiateViewControllerWithIdentifier:@"Notification"];
[self.window setRootViewController:notificationVC];
}
}

[[UIApplication sharedApplication] cancelAllLocalNotifications];

if ([ident isEqualToString:@"Geo"]) {
ViewController *notificationVC = [self.window.rootViewController.storyboard instantiateViewControllerWithIdentifier:@"Local"];
[self.window setRootViewController:notificationVC];
} else if ([ident isEqualToString:@"Pay"]) {
ViewController *notificationVC = [self.window.rootViewController.storyboard instantiateViewControllerWithIdentifier:@"Notification"];
[self.window setRootViewController:notificationVC];
}

application.applicationIconBadgeNumber = 0;

}

我将 Storyboard ID 设置为我在代码中输入的名称。我的代码有什么问题?为什么它不显示通知横幅并且不在通知中心显示通知?我希望你能帮助我解决这个问题。

最佳答案

详情

如果你在前台,你必须用你自己的代码处理消息。如果您在后台,iOS SDK 会通过显示通知横幅为您管理它。

解决方案

您可以自行处理 appDelegate 下 didReceiveLocalNotification 上的通知。

如果您有兴趣在此处实现类似于 iOS SDK 通知横幅的东西,您可以在 GitHub 上找到它 AGPushNote :

enter image description here

关于ios - iOS 应用程序上的多个通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26383421/

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