关于IOS9的本地通知方法

欢迎使用Markdown编辑器写博客

做stone的本地通知,也就是连接之后,app进入后台或者锁屏,按下stone的按键,手机端需要有本地通知。
实现方法是:
1、需要实现后台APP BLE继续运行:

在info.plist里面这样修改

2、在按键按下的方法里面添加:

UIApplication *app = [UIApplication sharedApplication];

    if ((app.applicationState == UIApplicationStateBackground) ||
        (app.applicationState == UIApplicationStateInactive)) {

        if (keyValue != 0) {
            NSLog(@"Background Key Value %d", keyValue);

            UILocalNotification *localNotif = [[UILocalNotification alloc] init];
            if (localNotif == nil) {
                return;
            }

            localNotif.soundName = UILocalNotificationDefaultSoundName;
            localNotif.applicationIconBadgeNumber = keyValue;
            localNotif.alertBody = [NSString stringWithFormat:@"You pressed button %d", keyValue];
            //localNotif.alertAction = @"Deanna got something for you";
            localNotif.hasAction = NO;
        //注意 :  这里是立刻弹出通知,其实这里也可以来定时发出通知,或者倒计时发出通知
            [app presentLocalNotificationNow:localNotif];
        }

    }

3、更为重要的是需要在AppDelegate.m中来注册通知

  • (BOOL)application:(UIApplication )application didFinishLaunchingWithOptions:(NSDictionary )launchOptions
  • {

  • if ([[UIDevice currentDevice].systemVersion doubleValue]>=8.0) {
    UIUserNotificationSettings* settings=[UIUserNotificationSettings
    settingsForTypes:UIUserNotificationTypeAlert|UIUserNotificationTypeBadge|UIUserNotificationTypeSound
    categories:nil];
    [application registerUserNotificationSettings:settings];

    }

    return YES;
    }

做完上面几个步骤,就可以实现通知了。

你可能感兴趣的:(IOS)