iOS程序退出后的本地通知

1.在- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions;方法中注册消息通知

//消息推送注册
    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0) {
        [[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings
                                                                             settingsForTypes:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge)
                                                                             categories:nil]];
        [[UIApplication sharedApplication] registerForRemoteNotifications];
    } else {
        [[UIApplication sharedApplication] registerForRemoteNotificationTypes:
         (UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert)];
    }


2.然后在 - ( void )applicationWillTerminate:( UIApplication *)application;方法中调用本地通知时间

- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.\
    
    [self noticefier];
}

- (void)noticefier{
    NSString *uuid = [NSString stringWithFormat:@"%@_upload",@"123123"];
    NSString *message = [NSString stringWithFormat:@"您有%d个未上传的数据包!",3];
    NSDate *alertTime = [NSDate dateWithTimeInterval:10 sinceDate:[NSDate date]];
    [self addNotificationWidthUUID:uuid alertTime:alertTime message:message];
}

-(BOOL)addNotificationWidthUUID:(NSString*)uuid alertTime:(NSDate*)alertTime message:(NSString*)message {
    BOOL retVal = NO;
    @try {
        UILocalNotification *notification= [[UILocalNotification alloc] init];
        if (notification != nil) {
            notification.fireDate = alertTime;
            notification.repeatInterval = 0;
            notification.timeZone = [NSTimeZone defaultTimeZone];
            //            notification.applicationIconBadgeNumber = badgeNumber;
            notification.soundName= UILocalNotificationDefaultSoundName;
            notification.alertBody = message;
            // action button caption
            notification.alertAction = @"OK";
            //是否显示额外的按钮,为no时alertAction消失
            //notification.hasAction = NO;
            NSDictionary *infoDict = [NSDictionary dictionaryWithObject:uuid forKey:@"key"];
            notification.userInfo = infoDict;
            
            [[UIApplication sharedApplication] scheduleLocalNotification:notification];
        }
        retVal = YES;
    }
    @catch (NSException *exception) {
        retVal = NO;
    }
    @finally {
        
    }
    return retVal;
}

3.程序杀死退出等待10秒,会接受到本地的通知,时间自己可以设置



  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值