【问题标题】:iOS Push Notification Banner did not appeariOS 推送通知横幅未出现
【发布时间】:2014-05-29 07:37:42
【问题描述】:

我尝试在我的主类 (ViewDidLoad) 中执行以下代码,但显然横幅没有出现在我的手机上。我注意到通知确实在通知中心弹出。只是当应用程序仍处于活动状态时,横幅并未出现在屏幕上。

        UILocalNotification *localNotif = [[UILocalNotification alloc] init];
            localNotif.alertBody = @"Test Push Notification";
            localNotif.applicationIconBadgeNumber = 1;
            localNotif.soundName=@"default";
            [application presentLocalNotificationNow:localNotif];

我也实施了 scheduleLocalNotification 来进行测试,但结果是一样的。 我不太确定出了什么问题。任何帮助或评论将不胜感激。

【问题讨论】:

  • 当您安排通知时,您的应用程序似乎正在前台运行,是这样吗?如果是这样,这就是您没有看到通知的原因。
  • 我的应用程序已启动并正在运行。当我触发本地通知时,它处于活动状态。我不知道发生了什么。

标签: ios objective-c notifications push-notification apple-push-notifications


【解决方案1】:

如果您的应用在前台,您将不会看到本地通知。来自documentation

如果您的应用位于最前面,则 application:didReceiveRemoteNotification:application:didReceiveLocalNotification:method 在其应用程序上被调用 委托

这意味着您应该在您的应用委托中实现这些方法。以下是如何执行此操作的示例:

- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
   UIAlertView *alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Message", nil)
                                                   message:notification.alertBody
                                                  delegate:self
                                         cancelButtonTitle:NSLocalizedString(@"OK", nil)
                                         otherButtonTitles:nil];
    [alert show];
}

(代码改编自this SO answer

【讨论】:

  • 上述功能的工作原理是它会出现一个警报视图,但我只是希望在用户使用应用程序时出现横幅。
  • @IssacZH。据我所知,当应用程序处于前台时,无法显示通知栏。但是,您可以自己实现类似的条形图。
  • 但我的应用程序没有进入前台。我没有退出我的申请。我所做的只是触发本地通知
  • @IssacZH。你是什​​么意思'没有进入前台'?如果您的意思是它已经存在,那么答案仍然相同 - 如果您的应用是最前面的应用,则不会显示通知栏。
  • 等等....我有点困惑。如我错了请纠正我。所以按照你说的,即使我的app状态是active,无论如何也不会出现通知栏?
【解决方案2】:

添加并检查

localNotif.fireDate = [NSDate dateWithTimeIntervalSinceNow:2];

【讨论】:

  • 我添加了那行代码,但它仍然没有出现。 =(
  • 您是否在它们触发后处理过通知?我的意思是您是否更新了 appdelegate.m 文件中的代码?
  • 我只处理了 RemoteNotification 而不是 LocalNotification。
  • 在 didFinishLaunchingWithOptions 安排和处理
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-01-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多