当前位置: 首页 > 工具软件 > Lives2D > 使用案例 >

ios+cocos2d-x推送消息

公良运锋
2023-12-01

这里在程序进入后台和从后台重新启动为例

void AppDelegate::createMyPushNotification()//创建一个推送

{
    int myLife = UserDefault::getInstance()->getIntegerForKey("LifeNumber");
    int needTime = 0;
    
    if (myLife < 5)
    {
        needTime = TimeUnit*(4 - myLife) + 60 * remindMinuteTimeNum + remindSecondTimeNum;
    }
    NSDate *date = [NSDate dateWithTimeIntervalSinceNow:needTime];
    //创建一个本地推送
    UILocalNotification *noti = [[[UILocalNotification alloc] init] autorelease];
    if (noti)
    {
        //设置推送时间
        noti.fireDate = date;
        //设置时区
        noti.timeZone = [NSTimeZone defaultTimeZone];
        //设置重复间隔
        noti.repeatInterval = NSWeekCalendarUnit;
        //推送声音
        noti.soundName = UILocalNotificationDefaultSoundName;
        //内容
        noti.alertBody = @"You lives are full now, start to run now?";
        //显示在icon上的红色圈中的数子
        noti.applicationIconBadgeNumber = 1;
        //设置userinfo 方便在之后需要撤销的时候使用
        NSDictionary *infoDic = [NSDictionary dictionaryWithObject:@"MyPushNotification" forKey:@"pushName"];
        noti.userInfo = infoDic;
        
        //添加推送到uiapplication
        UIApplication *app = [UIApplication sharedApplication];
        [app scheduleLocalNotification:noti];
    }

}


void AppDelegate::deleteMyPushNotification()//删除一个推送
{
    UIApplication *app = [UIApplication sharedApplication];
    //获取本地推送数组
    NSArray *localArr = [app scheduledLocalNotifications];
    //声明本地通知对象
    UILocalNotification *localNoti = NULL;
    if (localArr)
    {
        for (UILocalNotification *noti in localArr)
        {
            NSDictionary *dict = noti.userInfo;
            if (dict)
            {
                NSString *inKey = [dict objectForKey:@"pushName"];
                if ([inKey isEqualToString:@"MyPushNotification"])
                {
                    if (localNoti)
                    {
                        [localNoti release];
                        localNoti = nil;
                    }
                    localNoti = [noti retain];
                    break;
                }
            }
        }
        //判断是否找到已经存在的相同key的推送
//        if (!localNoti)
//        {
//            //不存在 初始化
//            localNoti = [[UILocalNotification alloc] init];
//        }
        if (localNoti)
        {
            //不推送 取消推送
            [app cancelLocalNotification:localNoti];
            [localNoti release];
            return;
        }
    }
}


//这里在程序启动时需要手动设置一下icon右上角红色圆圈的数字  不然会一直显示


    application.applicationIconBadgeNumber = 0;
    [[UIApplication sharedApplication] cancelAllLocalNotifications];

我是在appcontroller中实现的

这里还有几个代理可以实现,在代理方法中可以实现自己想要做的操作

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

    application.applicationIconBadgeNumber = 0;

}

//程序启动时添加以下代码清除推送...

application.applicationIconBadgeNumber = 0;
    [[UIApplication sharedApplication] cancelAllLocalNotifications];

 类似资料: