iPhone本地通知不会出现 [英] iPhone Local Notifications Won't Appear

查看:92
本文介绍了iPhone本地通知不会出现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:
UILocalNotification根本不起作用

Possible Duplicate:
UILocalNotification isn't working at all

我正在编写一个应用程序,当临近活动日期时,该应用程序会通过通知中心向用户发送警报.但是,当我在日期选择器中设置日期并关闭应用程序时,不会出现通知.我已经在配置文件中启用了推送通知.这是我项目中处理通知中心的所有代码,这是我的视图控制器文件中处理日期选择器的所有代码:

I'm writing an app that sends the user an alert through the Notification Center when an event date is approaching. But when I set the date in the date picker and close the app, the notification doesn't appear. I already enabled Push Notifications in my provisioning profiles. This is all the code in my project that deals with the notification center,This is all the code in my view controller file dealing with the date picker:

- (IBAction)dateChanged:(id)sender
{
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

    NSDate *selectedDate = [self.datePicker date];

    [defaults setObject:selectedDate forKey:@"ImportantDatesViewController.selectedDate"];
    [defaults synchronize];

}

- (void)viewDidLoad {
    NSDate *storedDate = [[NSUserDefaults standardUserDefaults] 
                          objectForKey:@"ImportantDatesViewController.selectedDate"];
    if (storedDate == nil) {
        storedDate = [NSDate date];
    }

    [self.datePicker setDate:storedDate animated:NO];

}

这就是我的App委托中处理本地通知的所有内容:

And this is everything in my App delegate dealing with local notifications:

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

[[UIApplication sharedApplication] registerForRemoteNotificationTypes:
     UIRemoteNotificationTypeBadge |
     UIRemoteNotificationTypeAlert |
     UIRemoteNotificationTypeSound];

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

    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"mm'/'dd'/'yyyy"];

    NSDate *eventDate = [[NSUserDefaults standardUserDefaults] objectForKey:@"ImportantDatesViewController.selectedDate"];



    localNotif.fireDate = [eventDate dateByAddingTimeInterval:-13*60*60];
    localNotif.timeZone = [NSTimeZone defaultTimeZone];


    localNotif.alertBody = @"Event in three days!";

    localNotif.alertAction = nil;

    localNotif.soundName = UILocalNotificationDefaultSoundName;
    localNotif.applicationIconBadgeNumber = 0;
    [[UIApplication sharedApplication] scheduleLocalNotification:localNotif];    

    return YES;  

}

- (void)application:(UIApplication*)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData*)deviceToken
{
    NSString* pushToken = [[[[deviceToken description] 
                             stringByReplacingOccurrencesOfString:@"<"withString:@""] 
                            stringByReplacingOccurrencesOfString:@">" withString:@""] 
                           stringByReplacingOccurrencesOfString: @" " withString: @""];

    NSLog(@"%@", pushToken);

}

- (void)application:(UIApplication*)application didFailToRegisterForRemoteNotificationsWithError:(NSError*)error {

    NSLog(@"error: %@", error);
}

非常感谢您的帮助,谢谢!

Any help is much appreciated, thank you!

推荐答案

以下代码用于本地通知.

following code is use for the local notification.

-(IBAction)buttonPressed:(UIButton *)button
{
    UILocalNotification *localNotification = [[UILocalNotification alloc] init];

    if (!localNotification)
        return;

    // Current date
    NSDate *date = [NSDate date]; 

    // Add one minute to the current time
    NSDate *dateToFire = [date dateByAddingTimeInterval:20];

    // Set the fire date/time
    [localNotification setFireDate:dateToFire];
    [localNotification setTimeZone:[NSTimeZone defaultTimeZone]];

    // Create a payload to go along with the notification
    NSArray *array = [NSArray arrayWithObjects:@"Value 1", @"Value 2", nil];
    NSDictionary *data = [NSDictionary dictionaryWithObject:array forKey:@"payload"];
    [localNotification setUserInfo:data];

    if (button == buttonAlert || button == buttonAll)
    {
        // Setup alert notification
        [localNotification setAlertBody:@"Incoming Local Notification" ];
        [localNotification setAlertAction:@"Open App"];
        [localNotification setHasAction:YES];
    }

    if (button == buttonBadge || button == buttonAll)
    {
        // Set badge notification, increment current badge value
        [localNotification setApplicationIconBadgeNumber:[[UIApplication sharedApplication] applicationIconBadgeNumber] + 1];
    }

    if (button == buttonSound || button == buttonAll)
    {
        // Setup sound notification
        [localNotification setSoundName:UILocalNotificationDefaultSoundName];
    }

    // Schedule the notification
    [[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
}

这篇关于iPhone本地通知不会出现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆