美文网首页
iOS BLE的后台数据接收和本地通知处理

iOS BLE的后台数据接收和本地通知处理

作者: 爱薄荷 | 来源:发表于2018-01-03 15:24 被阅读216次

最近在开发BLE交互的App时,往往出现这样的场景,设备状态发生改变,App可能处于后台模式,这个时候就要通过本地通知将状态提示给用户。场景的处理过程如下。

1.设置后台数据交互

首先,我们需要使App能够在后台接受到数据,在项目的info.plist文件中,新建一行Required background modes,然后在这一行下加入App shares data using CoreBluetoothApp communicates using CoreBluetooth两项。

info.plist示例图: infoplist示例.png

2.注册本地通知

通知的注册应该写在AppDelegate中,为了方便管理,可以写一个AppDelegate的分类。

在iOS10之后,苹果将通知相关的API统一,我们应该使用UserNotifications.framework来集中管理和使用 iOS 系统中的通知功能。

所以首先我们要#import <UserNotifications/UserNotifications.h>

然后注册通知的相关代码如下:

   //注册通知
    UNUserNotificationCenter *localNotiCenter = [UNUserNotificationCenter currentNotificationCenter];
    localNotiCenter.delegate = self;
    [localNotiCenter requestAuthorizationWithOptions:(UNAuthorizationOptionBadge | UNAuthorizationOptionSound | UNAuthorizationOptionAlert) completionHandler:^(BOOL granted, NSError * _Nullable error) {
        if (granted) {
            NSLog(@"request authorization successed!");
        }
    }];
    
    //iOS10之后, apple 开放了可以直接获取用户的通知设定信息的API。
    [localNotiCenter getNotificationSettingsWithCompletionHandler:^(UNNotificationSettings * _Nonnull settings) {
        NSLog(@"%@",settings);
    }];

3.创建本地推送

在接受到设备发来的数据后,我们要创建本地通知。
创建通知的代码如下:

- (void)creatLocalNotificationWithTitle:(NSString *)title WithBody:(NSString *)body {
    UNMutableNotificationContent *notiContent = [[UNMutableNotificationContent alloc] init];
    notiContent.title = title;
    notiContent.body = body;
    notiContent.badge = @1;
    notiContent.sound = [UNNotificationSound defaultSound];
    
    UNTimeIntervalNotificationTrigger *trigger1 = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:3 repeats:NO];
    
    UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:requertIdentifier content:notiContent trigger:trigger1];
    [[UNUserNotificationCenter currentNotificationCenter] addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {
        NSLog(@"Error:%@",error);
    }];
}

以上,基本完成了从接受数据到本地通知的处理。

4.备注

  • AppDelegate- (void)applicationWillEnterForeground:(UIApplication *)application- (void)applicationWillEnterForeground:(UIApplication *)application方法中添加[application setApplicationIconBadgeNumber:0];将通知产生的App图标上的标记去掉。

  • iOS中通知的详细剖析文章,请参见活久见的重构 - iOS 10 UserNotifications 框架解析

相关文章

  • iOS BLE的后台数据接收和本地通知处理

    最近在开发BLE交互的App时,往往出现这样的场景,设备状态发生改变,App可能处于后台模式,这个时候就要通过本地...

  • Android上传图片到服务器(使用base64字节流的形式)

    Android端图片处理:(传Bitmap对象) Android端发送数据: 后台接收数据: 后台图片处理: 最后...

  • iOS开发- 蓝牙后台接收数据(BLE4.0)

    转自:https://blog.csdn.net/hitwhylz/article/details/28664939

  • NSDictionary中读取到'null'值时

    在iOS app开发中,偶尔会出现这种场景,由于后台人员对于接口数据没有做空值处理,导致客户端接收到的有些数据为空...

  • 蓝牙ble上传byte数据手机

    用蓝牙ble传输数据时,手机端要对接收到的数据进行处理,byte的数值空间为-128到+127; 所以如果传感...

  • ios通知小结

    调试时发现,iOS 处理推送通知的方式在 iOS 9 和 iOS 10 上面有所不同。接收消息推送有两个代理方法:...

  • 推送通知

    基本介绍 iOS中分两种推送通知:本地推送 和 远程推送 推送通知的作用:就是可以让不在前台运行的app接收到消息...

  • 收藏博客

    iOS: iOS VOIP后台处理 python: tornado 翻译 tornado 框架 数据库: 数据库基...

  • 关于友盟推送测试无法接收

    在测试环境下,iOS 跟安卓接收通知的机制是不一样的。iOS 必须要在友盟后台添加测试设备,才能正常接收推送。

  • CoreBluetooth

    iOS-BLE蓝牙开发持续更新 - 简书 蓝牙打印小票 一个第三方 IOS BLE4.0蓝牙和外设连接和收发数据的...

网友评论

      本文标题:iOS BLE的后台数据接收和本地通知处理

      本文链接:https://www.haomeiwen.com/subject/gguhnxtx.html