本地通知横幅和声音在前台不工作 Objective-C

Local Notification Banner and sound not working in Foreground Objective-C

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

这是我的一段代码...

- (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;

}

不,它不会播放声音或显示横幅。但是,在您的应用程序委托中,仍然会调用收到通知,如果您希望通知用户,您可以在其中显示警报视图。

但是您可以手动添加音频。

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

SystemSoundID systemSoundID;
NSURL *soundURL = [[NSBundle mainBundle] URLForResource:@"blip"
                                          withExtension:@"mp3"];
AudioServicesCreateSystemSoundID((__bridge CFURLRef)soundURL, &systemSoundID);
AudioServicesPlaySystemSound(systemSoundID);
}

Local notification in Foreground

如果应用程序处于活动状态,那么您将不会收到任何声音、徽章或警报,但是应用程序委托 application:didReceiveLocalNotification:将被调用

来自苹果文档

If the application is foremost and visible when the system delivers the notification, no alert is shown, no icon is badged, and no sound is played. However, the application:didReceiveLocalNotification: is called if the application delegate implements it. The UILocalNotification instance is passed into this method, and the delegate can check its properties or access any custom data from the userInfo dictionary.

如果您的应用目前 运行 并且处于活动状态(即可见),您将不会看到提醒消息。相反 iOS 将通过以下方法直接将通知发送到您的应用程序

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

如果应用处于活动状态,您将收到通知 application:didReceiveLocalNotification:仅在应用委托中。在那里,您可以显示自定义横幅,例如在视图层次结构中显示的顶部 viewcontroller 视图。只需在应用程序打开时查看 whatsapp 通知