本地通知UILocalNotification


#pragma mark - 私有方法


- (void)showLocalNotification:(NSString *)nid withMsg:(NSString *)msg andSound:(NSString *)sound {

    UILocalNotification *notification = [[UILocalNotification alloc] init];

    for (UILocalNotification *noti in [[UIApplication sharedApplication] scheduledLocalNotifications]) {

        NSString *notiID = noti.userInfo[@"nid"];

        if ([notiID isEqualToString:nid]) {

//取消该本地通知

            [[UIApplication sharedApplication] cancelLocalNotification:notification];

            notification = noti;

            break;

        }

    }

    

    NSDate *currentDate = [NSDate date];

//设置本地通知的触发时间(如果要立即出发,则无需设置)

    notification.fireDate = [currentDate dateByAddingTimeInterval:1.0];

//设置重复间隔,0表示不重复

    notification.repeatInterval = 0;

//设置提醒的文字内容

//alertBody是设备接收到通知后横额或锁屏上显示的主要内容

    notification.alertBody = msg;

//alertAction是锁屏时Slide to后面的内容

    notification.alertAction = NSLocalizedString(@"s10", nil);

//设置提示音

    notification.soundName = sound;

//设置应用程序右上角的提醒个数

    notification.applicationIconBadgeNumber = 1;

    //设置通知的userInfo,用来标识通知

    NSMutableDictionary *userInfo = [[NSMutableDictionary alloc] init];

    userInfo[@"nid"] = nid;

    notification.userInfo = userInfo;

    //将通知添加到系统中去

    [[UIApplication sharedApplication] scheduleLocalNotification:notification];

}

//APPDelegate

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

    


    

    NSString *notiID = notification.userInfo[@"nid"];

    

    if ([notiID isEqualToString:@"HighTemperature"]) {

        [[NSNotificationCenter defaultCenter] postNotificationName:@"isHighTemperatureAlert" object:nil userInfo:nil];

    }

    if ([notiID isEqualToString:@"DeviceDisconnect"]) {

        [[NSNotificationCenter defaultCenter] postNotificationName:@"isDeviceDisconnectAlert" object:nil userInfo:nil];

    }

    

}


你可能感兴趣的:(杂记)