为什么我的本地通知在iOS 7中没有显示横幅

时间:2014-02-22 22:26:31

标签: ios ios7 background notifications

我的iOS 7应用程序正在NSOperationQueue块中调用的方法中生成本地通知。通知显示在通知中心,但它们未在屏幕顶部显示横幅。应用程序在后台时会生成通知。

我已经尝试了我能想到的所有内容,并进行了大量的Google搜索,但我仍然无法显示横幅。

以下是构建和安排通知的代码:

    // In the most recent case, I have verified that 
    // alertText = Why not work? and alertAction = View

    UILocalNotification *localNotification = [[UILocalNotification alloc] init];

    localNotification.alertBody = alertText;
    localNotification.alertAction = alertAction;

    localNotification.alertLaunchImage = launchImage;

    UIApplication *application = [UIApplication sharedApplication];
    application.applicationIconBadgeNumber++;

    localNotification.applicationIconBadgeNumber = application.applicationIconBadgeNumber;

    [self performSelectorOnMainThread:@selector(scheduleNotification:) 
    withObject:localNotification waitUntilDone:NO];
}

- (void)scheduleNotification: (id)notification
{
    UILocalNotification *localNotification = (UILocalNotification *)notification;

    // Schedule it with the app
    [[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
}

我已检查过我的应用的通知设置,它们是:

警报风格:横幅 徽章App图标:开启 声音:关闭 在通知中心显示:开启 包括:5个近期项目 在锁定屏幕上显示:开启

2 个答案:

答案 0 :(得分:1)

如果您的应用程序正在运行,则无法使用此横幅(除非您创建自己的横幅)。

解决方案可能是:

当应用程序运行时,Notification

处理
- (void)application:(UIApplication *)application 
   didReceiveRemoteNotification:(NSDictionary *)userInfo 

然后你可以使用这个项目(我使用的,哪个非常好):TSMessages创建类似于横幅的东西。

希望这会有所帮助...

答案 1 :(得分:1)

该错误实际上是我的代码的不同部分。我在后台线程中生成通知,并且在通知发出之前线程已被取消。