【问题标题】:iphone local notification in simulator模拟器中的iphone本地通知
【发布时间】:2011-04-05 01:48:44
【问题描述】:

我刚刚下载了 xcode 并尝试制作本地通知示例。问题是本地通知是否适用于模拟器?

谢谢

【问题讨论】:

    标签: iphone notifications local


    【解决方案1】:

    是的,本地通知适用于模拟器。但是,如果您想在应用处于前台时看到通知,请确保在应用委托中实现 application:didreceiveLocalNotification

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

    否则,请确保将通知安排在未来某个时间,然后关闭应用程序,以便查看 Apple 示例工作:

    UILocalNotification *localNotif = [[UILocalNotification alloc] init];
    if (localNotif == nil) return;
    NSDate *fireTime = [[NSDate date] addTimeInterval:10]; // adds 10 secs
    localNotif.fireDate = fireTime;
    localNotif.alertBody = @"Alert!";
    [[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
    [localNotif release];
    

    很容易认为您没有正确实现测试代码,只是在应用运行时没有处理事件。

    【讨论】:

      【解决方案2】:

      对于偶然发现这个老问题的任何人,您可能会发现另一个问题:iOS 8 引入了新的通知权限;并且您的应用程序必须明确要求它们。

      在你的AppDeligate.m:

      - (BOOL)application:(UIApplication *)application 
                didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
          //register local notifications
          if ([UIApplication instancesRespondToSelector:@selector(registerUserNotificationSettings:)]){
              [application registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert|UIUserNotificationTypeBadge|UIUserNotificationTypeSound categories:nil]];
          }
      
          //the rest of your normal code
      
          return YES;
      }
      

      如果您不这样做,您的通知将永远不会触发,并且您会在日志中收到类似这样的精彩消息: "Attempting to schedule a local notification <UIConcreteLocalNotification: 0x7ae51b10>{... alert details ...} with an alert but haven't received permission from the user to display alerts"

      【讨论】:

        【解决方案3】:

        本地通知在模拟器上工作,推送通知不

        【讨论】:

          【解决方案4】:

          要在 iPhone 模拟器中测试本地通知,请按以下步骤操作:

          1. 由于模拟器时间正是您机器中的时间,因此请将您的机器时间更改为所需时间的前 1 分钟(当您希望本地通知触发时)
          2. 重启模拟器(这很尴尬,但有时iPhone模拟器似乎无法立即更新时间)
          3. 再次运行模拟器(可能通过从 xcode 运行您的应用程序,在这种情况下,您必须按主页按钮将您的应用程序发送到后台)。到达时间后,您应该会看到通知

          这些步骤帮助我始终获得成功的本地通知。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2015-10-30
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2011-08-05
            相关资源
            最近更新 更多