本地通知问题?

Posted

技术标签:

【中文标题】本地通知问题?【英文标题】:Local Notification Issue? 【发布时间】:2012-08-21 09:45:53 【问题描述】:

我是 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];

【问题讨论】:

以及当您的通知弹出时您的应用程序必须关闭,如果应用程序处于打开状态则通知不会弹出... 【参考方案1】:

您好,我不明白您的问题,但在这里我发布了我的 LocalNotification 示例代码,只是比较它或使用此示例....

这里当用户点击完成按钮然后调用“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;

做一些改变,你得到你的输出

希望这个回答对你有帮助...

:)

【讨论】:

【参考方案2】:

如果您想多次按下按钮,请删除此行。

[[UIApplication sharedApplication] cancelAllLocalNotifications];  

因为它会删除您之前的所有通知。

【讨论】:

以上是关于本地通知问题?的主要内容,如果未能解决你的问题,请参考以下文章

丰富的本地通知

静默本地通知 IOS

如果在 iOS 设置中禁用通知访问时安排本地通知,则不会在 iOS 13 上触发本地通知

定期快速触发本地通知

显示没有声音的本地通知[重复]

为啥我收到远程通知时没有发送本地通知?