如何在cocos2d-x中使用本地推送通知

时间:2013-12-03 08:26:10

标签: cocos2d-x

我正在使用cocos2d-x制作游戏,我是初学者所以我不知道如何在我的游戏中推送本地通知。 你能帮我解决这个问题吗? 谢谢大家。

1 个答案:

答案 0 :(得分:0)

要在Ios中设置本地通知,您必须在appcontroller.mm类中编辑applicationDidEnterBackground方法..看看下面的代码......

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


    NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar];

    // Get the current date
    //NSDate *pickerDate = [self.datePicker date];
    NSDate *now = [NSDate date];
    //NSCalendar *calendar = [[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar] autorelease];

    // Break the date up into components
    NSDateComponents *dateComponents = [calendar components:( NSYearCalendarUnit | NSMonthCalendarUnit |  NSDayCalendarUnit )
                                                   fromDate:now];
    NSDateComponents *timeComponents = [calendar components:( NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit )
                                                   fromDate:now];

    // Set up the fire time

    NSDateComponents *dateComps = [[NSDateComponents alloc] init];
    [dateComps setDay:[dateComponents day]];
    [dateComps setMonth:[dateComponents month]];
    [dateComps setYear:[dateComponents year]];
    [dateComps setHour:[timeComponents hour]];
    [dateComps setMinute:[timeComponents minute]+_notificationTimeMinutes];
    [dateComps setSecond:([timeComponents second]+_notificationTimeSeconds)];
    NSDate *itemDate = [calendar dateFromComponents:dateComps];
    [dateComps release];

    UILocalNotification *localNotif = [[UILocalNotification alloc] init];
    if (localNotif == nil)
        return;
    localNotif.fireDate = itemDate;
    localNotif.timeZone = [NSTimeZone defaultTimeZone];

    localNotif.alertBody=@"Your local notification message";
    // Set the action button
    localNotif.alertAction = @"View";
    NSLog(@"setAlarm Called : %@",itemDate);
    //localNotif.hasAction = NO;
    localNotif.soundName = UILocalNotificationDefaultSoundName;
    localNotif.applicationIconBadgeNumber = [UIApplication sharedApplication].applicationIconBadgeNumber+1;
    localNotif.repeatInterval = NSWeekCalendarUnit;
    //[UIApplication sharedApplication].applicationIconBadgeNumber += 1;
    // Specify custom data for the notification
    NSDictionary *infoDict = [NSDictionary dictionaryWithObject:@"someValue" forKey:@"someKey"];
    localNotif.userInfo = infoDict;

    // Schedule the notification
    [[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
    [localNotif release];

在上面的代码中_notificationTimeSeconds以秒为单位,_notificationTimeMinutes以分钟为单位,类似地,您可以添加希望显示通知的小时,天或月。您在“本地通知消息”中添加消息