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

Dee*_*san 0 objective-c ios uilocalnotification

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

这是我的一段代码......

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

}
Run Code Online (Sandbox Code Playgroud)

Has*_* MH 7

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