通知时间不工作。在ios中立即发送通知

通知时间不工作。在ios中立即发送通知,ios,objective-c,iphone,notifications,Ios,Objective C,Iphone,Notifications,我使用以下代码在特定时间发送本地通知。当从一个方法调用时,它工作正常,但当我从另一个方法调用时,它不工作 代码如下: -(void)notificationUserInfo:(NSDictionary *)notificationData { UILocalNotification *notification = [[UILocalNotification alloc] init]; NSDateFormatter *dateFormat = [[NSDateFormatter alloc] i

我使用以下代码在特定时间发送本地通知。当从一个方法调用时,它工作正常,但当我从另一个方法调用时,它不工作

代码如下:

-(void)notificationUserInfo:(NSDictionary *)notificationData
{
UILocalNotification *notification = [[UILocalNotification alloc] init];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"dd/MM/yyyy HH:mm"];

NSString *tipid = [notificationData objectForKey:@"receivedTipsId"];
NSString *currentDate = [notificationData objectForKey:@"receivedDate"];
NSString *tipsCategoryid = [notificationData objectForKey:@"categoryId"];
NSString *todayDateTime = [notificationData objectForKey:@"todayDate"];
NSString *categoryName = [notificationData objectForKey:@"categoryName"];
NSString *tipsForNotification = [notificationData objectForKey:@"tipsForNotification"];
NSString *cardType = [notificationData objectForKey:@"cardType"];

//Assigning the notification contents
NSDate *updatedDateFormat = [dateFormat dateFromString:todayDateTime];
notification.fireDate = updatedDateFormat;
notification.userInfo = [NSDictionary dictionaryWithObjectsAndKeys:tipid, @"receivedTipsId", currentDate, @"receivedDate", tipsCategoryid, @"categoryId", cardType, @"cardType", nil];
notification.alertBody = [NSString stringWithFormat:@"%@: %@",categoryName, tipsForNotification];
notification.soundName = UILocalNotificationDefaultSoundName;
notification.applicationIconBadgeNumber = [[UIApplication sharedApplication] applicationIconBadgeNumber] + 1;
[[UIApplication sharedApplication] scheduleLocalNotification:notification];
}

提前感谢。

如果您在iOS 8中使用此功能,则必须注册您的应用程序以获得本地通知

应用程序内代理

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { /*...*/ }

#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 80000
if ([UIApplication instancesRespondToSelector:@selector(registerUserNotificationSettings:)])
{
    [[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound categories:nil]];
}
然后

- (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings

- (void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forRemoteNotification:(NSDictionary *)userInfo completionHandler:(void(^)())completionHandler

使用上述代码。

这是因为时间和日期格式存在问题。当我使用此方法时,我将firedate指定为过去日期。例如,我使用了
MM/dd/yyyy
而不是
dd/MM/yyyy

因此,
2014年11月12日
返回为
2014年12月11日
。因此,通知将在预定的通知之后立即收到

    UILocalNotification *localNotification = [[UILocalNotification alloc] init];
    if (!localNotification) {
        break;
    }
    **localNotification.timeZone = [NSTimeZone defaultTimeZone];**

    [localNotification setFireDate:[NSDate dateWithTimeIntervalSinceNow:20]];
    localNotification.alertBody = @"message";
    [[UIApplication sharedApplication] scheduleLocalNotification:localNotification];