iPhone iOS:本地通知未出现

时间:2021-07-26 14:02:07

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. It may be because of my objectForKey area. I have 2 nibs (one for iPhone, one for iPad) named ImportantDatesViewController_iPhone1 and ImportantDatesViewController_iPad1. Should I change the objectForKey area to the nib name instead of just "ImportantDatesViewController"? And my .h and .m file names are simply ImportantDatesViewController also. Sorry, I'm still very new at this and learning as I go. Here is all the code in my project that deals with the notification center, this is what I put in my view controller:

我正在编写一个应用程序,当事件日期临近时,通过通知中心向用户发送警报。但是,当我在日期选择器中设置日期并关闭应用程序时,通知不会出现。我已在配置文件中启用了推送通知。这可能是因为我的objectForKey区域。我有2个nib(一个用于iPhone,一个用于iPad),名为ImportantDatesViewController_iPhone1和ImportantDatesViewController_iPad1。我应该将objectForKey区域更改为nib名称而不仅仅是“ImportantDatesViewController”吗?我的.h和.m文件名也只是ImportantDatesViewController。对不起,我还是很陌生,在我学习的同时学习。以下是我的项目中处理通知中心的所有代码,这是我在视图控制器中放置的内容:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
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:-60*60*60];
localNotif.timeZone = [NSTimeZone defaultTimeZone];

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

localNotif.alertAction = nil;

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

return YES;

}

}

I also added this code in my didFinishLaunchingWithOptions method in the App Delegate at the bottom, and I thought it would do the trick:

我还在底部的App Delegate中的didFinishLaunchingWithOptions方法中添加了这段代码,我认为它可以解决这个问题:

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

Any help is much appreciated, thank you!

非常感谢任何帮助,谢谢!

3 个解决方案

#1


4  

Your telling it to fire a notification immediately when you set it and your setting it for a time before now by setting (NOW)-60*60*60.The notification has passed already.

您通过设置(NOW)-60 * 60 * 60来设置它并在之前设置一段时间后立即发出通知。通知已经过去了。

[[UIApplication sharedApplication]presentLocalNotificationNow:localNotif];  

if you want to set it for a specific time:

如果要将其设置为特定时间:

 [[UIApplication sharedApplication] scheduleLocalNotification:localNotif];

If the specified value is nil or is a date in the past, the notification is delivered immediately. As a side note make sure to set the timeZone and Locale to the ones you want.

如果指定的值为nil或者是过去的日期,则会立即传递通知。作为旁注,请确保将timeZone和Locale设置为您想要的。

#2


1  

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

   if (localNotif == nil) return;
  NSDate *fireTime = [[NSDate date]   dateByAddingTimeInterval:900] ;  //15 min

            localNotif.fireDate = fireTime;
            localNotif.alertBody = @"Parking your CAR 15 Minutes reached..";
            //    localNotif.repeatInterval=kCFCalendarUnitMinute;
            [[UIApplication sharedApplication] scheduleLocalNotification:localNotif];

#3


1  

Here is sample code for LocalNotification that worked for my project.

以下是适用于我的项目的LocalNotification的示例代码。

This code block in AppDelegate file :

AppDelegate文件中的此代码块:

 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    {
        [launchOptions valueForKey:UIApplicationLaunchOptionsLocalNotificationKey];
        // Override point for customization after application launch.
        return YES;
    }

    // This code block is invoked when application is in foreground (active-mode) 
    -(void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification {

        UIAlertView *notificationAlert = [[UIAlertView alloc] initWithTitle:@"Notification"    message:@"This local notification" 
        delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];

        [notificationAlert show];
       // NSLog(@"didReceiveLocalNotification");
    }

This code block in .m file of any ViewController :

任何ViewController的.m文件中的此代码块:

-(IBAction)startLocalNotification {  // Bind this method to UIButton action
    NSLog(@"startLocalNotification");

    UILocalNotification *notification = [[UILocalNotification alloc] init];
    notification.fireDate = [NSDate dateWithTimeIntervalSinceNow:7];
    notification.alertBody = @"This is local notification!";
    notification.timeZone = [NSTimeZone defaultTimeZone];
    notification.soundName = UILocalNotificationDefaultSoundName;
    notification.applicationIconBadgeNumber = 10;

    [[UIApplication sharedApplication] scheduleLocalNotification:notification];    
}

The above code display an AlertView after time interval of 7 seconds when pressed on button that binds “startLocalNotification” If application is in background then it displays BadgeNumber as 10 and with default notification sound.

上面的代码在7秒的时间间隔后显示一个AlertView,当按下按钮时绑定“startLocalNotification”如果应用程序在后台,则它将BadgeNumber显示为10并具有默认通知声音。

#1


4  

Your telling it to fire a notification immediately when you set it and your setting it for a time before now by setting (NOW)-60*60*60.The notification has passed already.

您通过设置(NOW)-60 * 60 * 60来设置它并在之前设置一段时间后立即发出通知。通知已经过去了。

[[UIApplication sharedApplication]presentLocalNotificationNow:localNotif];  

if you want to set it for a specific time:

如果要将其设置为特定时间:

 [[UIApplication sharedApplication] scheduleLocalNotification:localNotif];

If the specified value is nil or is a date in the past, the notification is delivered immediately. As a side note make sure to set the timeZone and Locale to the ones you want.

如果指定的值为nil或者是过去的日期,则会立即传递通知。作为旁注,请确保将timeZone和Locale设置为您想要的。

#2


1  

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

   if (localNotif == nil) return;
  NSDate *fireTime = [[NSDate date]   dateByAddingTimeInterval:900] ;  //15 min

            localNotif.fireDate = fireTime;
            localNotif.alertBody = @"Parking your CAR 15 Minutes reached..";
            //    localNotif.repeatInterval=kCFCalendarUnitMinute;
            [[UIApplication sharedApplication] scheduleLocalNotification:localNotif];

#3


1  

Here is sample code for LocalNotification that worked for my project.

以下是适用于我的项目的LocalNotification的示例代码。

This code block in AppDelegate file :

AppDelegate文件中的此代码块:

 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    {
        [launchOptions valueForKey:UIApplicationLaunchOptionsLocalNotificationKey];
        // Override point for customization after application launch.
        return YES;
    }

    // This code block is invoked when application is in foreground (active-mode) 
    -(void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification {

        UIAlertView *notificationAlert = [[UIAlertView alloc] initWithTitle:@"Notification"    message:@"This local notification" 
        delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];

        [notificationAlert show];
       // NSLog(@"didReceiveLocalNotification");
    }

This code block in .m file of any ViewController :

任何ViewController的.m文件中的此代码块:

-(IBAction)startLocalNotification {  // Bind this method to UIButton action
    NSLog(@"startLocalNotification");

    UILocalNotification *notification = [[UILocalNotification alloc] init];
    notification.fireDate = [NSDate dateWithTimeIntervalSinceNow:7];
    notification.alertBody = @"This is local notification!";
    notification.timeZone = [NSTimeZone defaultTimeZone];
    notification.soundName = UILocalNotificationDefaultSoundName;
    notification.applicationIconBadgeNumber = 10;

    [[UIApplication sharedApplication] scheduleLocalNotification:notification];    
}

The above code display an AlertView after time interval of 7 seconds when pressed on button that binds “startLocalNotification” If application is in background then it displays BadgeNumber as 10 and with default notification sound.

上面的代码在7秒的时间间隔后显示一个AlertView,当按下按钮时绑定“startLocalNotification”如果应用程序在后台,则它将BadgeNumber显示为10并具有默认通知声音。