iOS Statistics App Push rrival rate

cover
As we know,after iOS 10,Apple launched Notification Service Extension,Looking at the doc,the description of UNNotificationServiceExtension is An object that modifies the content of a remote notification before it's delivered to the user.,that is, in a Before the remote notification is displayed to the user, the notification can be modified throughUNNotificationServiceExtension.

Stop talking nonsense and get down to business.

一、How to monitor received push notifications

  1. Create a new Target on the original Project, Select Create
    image-20190304210307444

  2. Add a new field mutable-content": "1" to the content pushed by the server,,at the same level as alert/badge/sound。 For example, when testing push in the push platform, the settings are as follows:image-20190304212029192

  3. Run the main Target of the Project, then run Notification Service Extension,and select the main Target

  4. Send test push,then you can see it execute and enter - (void)didReceiveNotificationRequest:(UNNotificationRequest *)request withContentHandler:(void (^)(UNNotificationContent * _Nonnull))contentHandler

二、How to statistics

  1. If you are connecting a 3rd push platform, you can check whether it supports. For example: Jiguang API: Notification Service Extension

  2. If you implement it yourself, there are two options:

    1. Write network requests in the notification extension project and send the push arrival data to the backend server
    2. Save the push arrival data to the local App through App Group, and then process it in the main Target

三、How to use App Groud to share data

  1. Login to https://developer.apple.com ,Create App Group

  2. Configure in project,target - Capabilites - App groups

  3. Use in code

    1. Use in 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"]);
    2. Use in 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];