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

时间:2015-08-05 13:36:07

标签: ios objective-c uilocalnotification

我正在尝试向我的应用实施本地通知,并且我实施了本地通知,但问题是......我没有获得 通知BANNER和SOUND < / strong>当我的应用位于 前景 时。但当我的应用程序位于 后台 时, 正常运行 。 如何将通知横幅和声音带到前台..这可能吗?

这是我的代码......

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

}

3 个答案:

答案 0 :(得分:7)

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

答案 1 :(得分:2)

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

来自apple docs

  

如果应用程序在系统发送通知时最重要且可见,则不会显示警报,没有图标标记,也不会播放声音。但是,如果应用程序委托实现它,则调用application:didReceiveLocalNotification :. UILocalNotification实例将传递给此方法,委托可以检查其属性或访问userInfo字典中的任何自定义数据。

如果您的应用当前正在运行且处于活动状态(即可见),则您将看不到警报消息。相反,iOS会通过以下方法直接向您的应用发送通知

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

答案 2 :(得分:1)

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

但您可以手动添加音频。

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