试图让UILocalNotification立即显示

时间:2014-10-13 21:46:32

标签: ios iphone cocoa-touch uilocalnotification

在我的应用中,我只是尝试触发本地通知立即弹出。它可以在模拟器和我的iOS设备上运行,但我一直在收到用户的电子邮件,说它不适用于他们(并且应用程序不会出现在通知中心)。我在实施本地通知时遗漏了什么吗?

显示警报的代码:

 UILocalNotification* localNotification = [[UILocalNotification alloc] init];
 localNotification.fireDate = [NSDate date];
 localNotification.soundName = UILocalNotificationDefaultSoundName;
 localNotification.alertBody = @"Message goes here";
 localNotification.timeZone = [NSTimeZone defaultTimeZone]; //Could be the problem?
 [[UIApplication sharedApplication] scheduleLocalNotification:localNotification];

App Delegate方法:

- (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 didReceiveLocalNotification:(UILocalNotification *)notification
{
    UIApplicationState state = [application applicationState];
    if (state == UIApplicationStateActive) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Alert"
                                                        message:notification.alertBody
                                                       delegate:self cancelButtonTitle:@"OK"
                                              otherButtonTitles:nil];
        [alert show];
    }

    // Set icon badge number to zero
    application.applicationIconBadgeNumber = 0;
}

也许它是一个timeZone问题,我应该切换到presentLocalNotificationNow?而不是scheduleLocalNotification?

我不明白为什么我的用户在我的设备上运行时遇到此问题。

EDIT *** 我一直遇到iOS7用户的这个问题,不是iOS8 (我明白我必须在app delegate方法的某处注册应用程序)

2 个答案:

答案 0 :(得分:0)

通知和徽章需要iOS 8中的新权限。这可能是问题的根源。

如果用户拒绝通知权限并忘记了,则可能会导致问题。

答案 1 :(得分:0)

对于iOS 8,您必须注册通知(请求用户权限)

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    if ([UIApplication instancesRespondToSelector:@selector(registerUserNotificationSettings:)]){
    [application registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert|UIUserNotificationTypeBadge|UIUserNotificationTypeSound categories:nil]];
    }
    // Override point for customization after application launch.
    return YES;
}