0

イベントの日付が近づくと、通知センターを介してユーザーにアラートを送信するアプリを作成しています。しかし、日付ピッカーで日付を設定してアプリを閉じると、通知が表示されません。プロビジョニング プロファイルでプッシュ通知を有効にしました。objectForKey 領域が原因である可能性があります。ImportantDatesViewController_iPhone1 と ImportantDatesViewController_iPad1 という名前の 2 つのペン先 (iPhone 用と iPad 用) があります。「ImportantDatesViewController」だけでなく、objectForKey 領域を nib 名に変更する必要がありますか? また、私の .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;

}

また、下部の App Delegate の didFinishLaunchingWithOptions メソッドにこのコードを追加しました。これでうまくいくと思いました。

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

どんな助けでも大歓迎です、ありがとう!

4

3 に答える 3

4

設定するとすぐに通知を起動するように指示し、(NOW)-60*60*60 を設定して、今より前の時間に設定します。通知は既に渡されています。

[[UIApplication sharedApplication]presentLocalNotificationNow:localNotif];  

特定の時間に設定したい場合:

 [[UIApplication sharedApplication] scheduleLocalNotification:localNotif];

指定された値が nil または過去の日付である場合、通知はすぐに配信されます。補足として、timeZone と Locale を必要なものに設定してください。

于 2012-01-22T02:03:26.617 に答える
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];
于 2012-07-31T10:59:45.017 に答える
1

これは、私のプロジェクトで機能した LocalNotification のサンプル コードです。

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");
    }

任意の 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];    
}

上記のコードは、「startLocalNotification」をバインドするボタンを押すと、7 秒後に AlertView を表示します。アプリケーションがバックグラウンドにある場合、BadgeNumber を 10 として表示し、デフォルトの通知音を鳴らします。

于 2014-03-26T11:44:55.643 に答える