0

iPhoneアプリ開発初心者です。アラームアプリをやっています。このアプリケーションでは、ローカル通知を使用しています。完了ボタンアクションで通知メソッドを呼び出しています。完了ボタンをクリックすると、通知が正しく発行されました。その後、もう一度クリックすると発射されたという意味もあり、何度も押すと誤動作することになりますが、私は通知発射日時を同じ時刻に使用しています。この時点で完了したことは、ユーザーが再び完了ボタンをクリックしたことを意味し、通知が再びトリガーされたことを意味します。

その特定の時間に発射したい。手伝ってくれませんか。

ここでは、このソース コードを使用しています。

-(IBAction)doneButton
{

    [self scheduledNotification];

}

-(void)scheduledNotification
{
    Resource *resourceLoader = [[Resource alloc] init];

     NSDictionary *prefDic = [resourceLoader getPlistDataAsDictionary:@"preference"];

    if ([@"ON" isEqualToString:[prefDic objectForKey:@"alarm"]]) 
    {

        [[UIApplication sharedApplication] cancelAllLocalNotifications];

        Class cls = NSClassFromString(@"UILocalNotification");
        if (cls != nil) 
        {
            NSString *musicName;
            //NSDate *selectedDateTime = [[NSDate alloc]init];

            selectedDateTime = [prefDic objectForKey:@"alarmtime"];
            NSLog(@"saravanan periyasamy %@", selectedDateTime);

            musicName = [NSString stringWithFormat:@"%@%@",[prefDic objectForKey:@"alarmmusic"],@".wav" ];

            NSLog(@"musicname %@", musicName);

            NSLog(@"saravanan periyasamy123 %@", selectedDateTime);
            UILocalNotification *notif = [[cls alloc] init];

            notif.fireDate = selectedDateTime;  /* time for fireDate*/

            NSLog(@"selectedtime %@",selectedDateTime);

            notif.timeZone = [NSTimeZone systemTimeZone];

            notif.alertBody = @"Alarm";
            notif.alertAction = @"Show me";
            notif.repeatInterval = 0;

            notif.soundName = musicName;
            notif.applicationIconBadgeNumber = 1;
            NSDictionary *userDict = [NSDictionary dictionaryWithObject:@"saravanan"
                                                                 forKey:kRemindMeNotificationDataKey];
            notif.userInfo = userDict;
            [[UIApplication sharedApplication] scheduleLocalNotification:notif];
            [notif release];
        }
    }
}


AppDelegate.m



- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
   Class cls = NSClassFromString(@"UILocalNotification");

    if (cls) {
        UILocalNotification *notification = [launchOptions objectForKey:
                                             UIApplicationLaunchOptionsLocalNotificationKey];

        if (notification) {
            NSString *reminderText = [notification.userInfo 
                                      objectForKey:kRemindMeNotificationDataKey];
            //[self.viewController showReminder:reminderText];
            [self.settingViewController showReminder1:reminderText];
        }
    }
    application.applicationIconBadgeNumber = 0;   
    return YES;    

}

- (void)applicationWillEnterForeground:(UIApplication *)application {

    application.applicationIconBadgeNumber = 0;
}

- (void)application:(UIApplication *)application 
didReceiveLocalNotification:(UILocalNotification *)notification {



    application.applicationIconBadgeNumber = 0;
    NSString *reminderText = [notification.userInfo
                              objectForKey:kRemindMeNotificationDataKey];
    //[self.viewController showReminder:reminderText];
    [self.settingViewController showReminder1:reminderText];
}
4

2 に答える 2

0

こんにちは、私はあなたの質問を理解していませんが、ここにLocalNotificationのサンプルコードを1つ投稿するだけで、それを比較するか、この例を使用してください....

ここで、ユーザーが完了ボタンをクリックすると、「btnGo_Clicked」メソッドが呼び出されます

-(IBAction)btnGo_Clicked:(id)sender{

    NSLog(@"\n ----------->>Go Clicked");
    Class cls = NSClassFromString(@"UILocalNotification");
    if (cls != nil) {
        NSString *kRemindMeNotificationDataKey = @"kRemindMeNotificationDataKey";


        NSDateComponents *dc = [[NSCalendar currentCalendar] components:NSDayCalendarUnit|NSMonthCalendarUnit|NSYearCalendarUnit|NSHourCalendarUnit|NSMinuteCalendarUnit|NSSecondCalendarUnit|NSQuarterCalendarUnit fromDate:[exdatePicker date]];
        [dc setDay:dc.day - 1];
        NSDate *noticeDate = [[NSCalendar currentCalendar] dateFromComponents:dc];


        NSDateFormatter* dateFormatterstring = [[NSDateFormatter alloc] init];
        [dateFormatterstring setDateFormat:@"dd-MM-yyyy hh:mm:ss a"];
        NSString *dateString = [dateFormatterstring stringFromDate:noticeDate];

        txtExpirayDate.text = dateString;
        UILocalNotification *notification = [[cls alloc] init];

        notification.fireDate = noticeDate;
        notification.timeZone = [NSTimeZone defaultTimeZone];
        notification.alertBody = [NSString stringWithFormat:@"%@ your Licence Expiry Date is Tommorow",txtLicence.text];
        notification.alertAction = @"Show me";
        notification.soundName = UILocalNotificationDefaultSoundName;
        notification.applicationIconBadgeNumber = 1;
        NSDictionary *userDict = [NSDictionary dictionaryWithObject:txtLicence.text forKey:kRemindMeNotificationDataKey];
        notification.userInfo = userDict;
        [[UIApplication sharedApplication] scheduleLocalNotification:notification];
        [notification release];
    }
    exdatePicker.hidden=YES;
}

いくつかの変更を行うと、出力が得られます

この回答がお役に立てば幸いです...

:)

于 2012-08-21T09:51:35.250 に答える
0

ボタンを複数回押したい場合は、この行を削除してください。

[[UIApplication sharedApplication] cancelAllLocalNotifications];  

以前のすべての通知を削除するためです。

于 2012-08-21T09:51:40.540 に答える