背景中的本地通知

时间:2013-05-29 10:26:26

标签: iphone ios objective-c notifications uilocalnotification

任何人都可以告诉我如何在我的应用在后台时获得UILocalNotification

我在这里发布我的代码。提前谢谢。

UILocalNotification *localNotif = [[UILocalNotification alloc] init];
if (localNotif == nil)
    return;
localNotif.fireDate =[startDate addTimeInterval:60];
NSLog(@"%@",localNotif.fireDate);

localNotif.timeZone = [NSTimeZone defaultTimeZone];     
localNotif.alertAction = @"View";
localNotif.soundName = UILocalNotificationDefaultSoundName;

NSString *notifStr=[NSString stringWithFormat:@"Hey looks like you're meeting up with %@, why don't you let your other friends know what fun they're missing out on? Share a photo :)",[itemDict objectForKey:@"fname"]];

NSDictionary *infoDict = [NSDictionary dictionaryWithObjectsAndKeys:notifStr ,@"notifKey",nil];
localNotif.userInfo = infoDict;

[[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
[localNotif release];

3 个答案:

答案 0 :(得分:19)

-(void)insert:(NSDate *)fire
{
    [[UIApplication sharedApplication]cancelAllLocalNotifications];
    self.localNotification = [[UILocalNotification alloc] init];
    if (self.localNotification == nil)
    {
        return;
    }
    else
    {
        self.localNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:60];
        self.localNotification.alertAction = nil;
        self.localNotification.soundName = UILocalNotificationDefaultSoundName;
        self.localNotification.alertBody = @"Hey looks like you're meeting up with %@, why don't you let your other friends know what fun they're missing out on? Share a photo :)";
        self.localNotification.alertAction = NSLocalizedString(@"Read Msg", nil);
        self.localNotification.applicationIconBadgeNumber=1;
        self.localNotification.repeatInterval=0;
        [[UIApplication sharedApplication] scheduleLocalNotification:self.localNotification];
    }
}

- (void)application:(UIApplication *)app didReceiveLocalNotification:(UILocalNotification *)notif
{
    [[UIApplication sharedApplication]cancelAllLocalNotifications];
    app.applicationIconBadgeNumber = notif.applicationIconBadgeNumber -1;

    notif.soundName = UILocalNotificationDefaultSoundName;

    [self _showAlert:[NSString stringWithFormat:@"%@",Your msg withTitle:@"Title"];

}

- (void) _showAlert:(NSString*)pushmessage withTitle:(NSString*)title
{
    [self.alertView_local removeFromSuperview];
    self.alertView_local = [[UIAlertView alloc] initWithTitle:title message:pushmessage delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
    [self.alertView_local show];

    if (self.alertView_local)
    {
    }
}

希望这会对你有所帮助:)。

答案 1 :(得分:1)

在iOS 4.0中不推荐使用

addTimerInterval,请确保您的部署目标。你可以使用。

- (id)dateByAddingTimeInterval:(NSTimeInterval)ti

还要确保为fireDate

设置正确的值

答案 2 :(得分:1)

请使用以下代码。

 self.localNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:60];
    self.localNotification.alertAction = nil;
    self.localNotification.soundName = UILocalNotificationDefaultSoundName;
    self.localNotification.alertBody = @"Hey looks like you're meeting up with %@, why don't you let your other friends know what fun they're missing out on? Share a photo :)";
    self.localNotification.alertAction = NSLocalizedString(@"Read Msg", nil);
    self.localNotification.applicationIconBadgeNumber=1;
    self.localNotification.repeatInterval=0;