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秒,会接受到本地的通知,时间自己可以设置



你可能感兴趣的:(ios,推送)