gpt4 book ai didi

ios - 本地通知横幅和声音在前台 Objective-C 中不起作用

转载 作者:行者123 更新时间:2023-11-28 18:01:01 24 4
gpt4 key购买 nike

我正在尝试对我的应用实现本地通知,我实现了本地通知,但问题是......我没有收到通知横幅和声音 当我的应用位于前台时。但当我的应用程序处于后台时,它运行良好。如何将通知横幅和声音置于前台。这可能吗?

这是我的一段代码...

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Handle launching from a notification
UILocalNotification *locationNotification = [launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];
if (locationNotification) {
// Set icon badge number to zero
application.applicationIconBadgeNumber = 0;
}
}

- (void)application:(UIApplication*)application didReceiveRemoteNotification:(NSDictionary*)userInfo
{

if (application.applicationState == UIApplicationStateActive ) {
NSLog(@"it entered active push");

UILocalNotification *localNotification = [[UILocalNotification alloc] init];
localNotification.userInfo = userInfo;
localNotification.soundName = UILocalNotificationDefaultSoundName;
localNotification.alertBody = userInfo[@"aps"][@"alert"][@"body"];
localNotification.alertLaunchImage= userInfo[@"acme1"];
localNotification.fireDate = [NSDate date];
localNotification.applicationIconBadgeNumber = 1;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
}
}


- (void)applicationWillEnterForeground:(UIApplication *)application
{
// Remove the badge number
application.applicationIconBadgeNumber = 0;
}

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

UIApplicationState state = [application applicationState];
if (state == UIApplicationStateActive) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Reminder"
message:notification.alertBody
delegate:self cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alert show];
}

// Set icon badge number to zero
application.applicationIconBadgeNumber = 0;

}

最佳答案

如果应用处于事件状态,您将收到 application:didReceiveLocalNotification: 仅在应用委托(delegate)中的通知。在那里,您可以显示自定义横幅,如在 View 层次结构中显示的顶层 View Controller 上的 View 。只需在应用程序打开时查看 whatsapp 通知即可

关于ios - 本地通知横幅和声音在前台 Objective-C 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31834146/

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