0%

iOS统计Push推送到达率

cover
众所周知,iOS 10 以后,苹果官方推出了Notification Service Extension,查看文档,UNNotificationServiceExtension的说明是,An object that modifies the content of a remote notification before it's delivered to the user.,也就是在一个远程通知展示给用户之前,可以通过UNNotificationServiceExtension来修改这个通知。

废话少说,直接开干吧。

一、如何监听收到推送

  1. 在原有的项目上新建一个 Target,如图,选择创建
    image-20190304210307444

  2. 在服务端推送的内容中新增一个字段mutable-content": "1",与alert/badge/sound处于同一层级。例如在推送平台中测试推送时,设置如下:image-20190304212029192

  3. 先运行项目的主Target,然后再运行Notification Service Extension,手动选择主Target

  4. 发送测试推送,可以看到执行进入 Target 项目中的- (void)didReceiveNotificationRequest:(UNNotificationRequest *)request withContentHandler:(void (^)(UNNotificationContent * _Nonnull))contentHandler

二、如何统计

  1. 如果是接入第三方推送平台,可以查看是否支持。如 极光的 Notification Service Extension 相关接口
  2. 如果自己实现的话,有以下两种方案
    1. 在通知扩展项目中写网络请求,将推送到达数据发送到后端服务器
    2. 将推送到达数据通过App Group保存到 App 的本地,在主 Target 中再处理

三、如何使用 App Groud 实现数据共享

  1. https://developer.apple.com 登录,创建 App Group

  2. 在项目中配置,target - Capabilites - App groups

  3. 代码中使用

    1. NSUserDefaults 中使用

      1
      2
      3
      4
      5
      6
      NSUserDefaults *userDefaults = [[NSUserDefaults alloc] initWithSuiteName:@"group.company.appGroupName"];
      // write data
      [userDefaults setValue:@"value" forKey:@"key"];

      //read data
      NSLog(@"%@", [userDefaults valueForKey:@"key"]);
  1. NSFileManager 中使用

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    // write data
    NSURL *groupURL = [[NSFileManager defaultManager] containerURLForSecurityApplicationGroupIdentifier:@"group.domain.groupName"];
    NSURL *fileURL = [groupURL URLByAppendingPathComponent:@"fileName"];

    NSString *text = @"Go amonxu.com";
    if (![[NSFileManager defaultManager] fileExistsAtPath:fileURL.path]) {
    [text writeToURL:fileURL atomically:YES encoding:NSUTF8StringEncoding error:nil];
    } else {
    NSFileHandle *fileHandle = [NSFileHandle fileHandleForUpdatingURL:fileURL error:nil];
    [fileHandle seekToEndOfFile];
    [fileHandle writeData:[text dataUsingEncoding:NSUTF8StringEncoding]];
    [fileHandle closeFile];
    }


    // read data
    NSURL *groupURL = [[NSFileManager defaultManager] containerURLForSecurityApplicationGroupIdentifier:@"group.domain.groupName"];
    NSURL *fileURL = [groupURL URLByAppendingPathComponent:@"fileName"];
    NSString *fileContent = [NSString stringWithContentsOfURL:fileURL encoding:NSUTF8StringEncoding error:nil];
Amon wechat
扫一扫关注我,有惊喜不迷路
(转载本站文章请注明作者和出处: 猛猛如玉
网址: https://amonxu.com 微信公众号: itcraft
可以请我喝瓶水吗:)