iOS Statistics App Push rrival rate
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
Create a new Target on the original Project, Select
Create
Add a new field
mutable-content": "1"
to the content pushed by the server,,at the same level asalert/badge/sound
。 For example, when testing push in the push platform, the settings are as follows:Run the main Target of the Project, then run
Notification Service Extension
,and select the main TargetSend test push,then you can see it execute and enter
- (void)didReceiveNotificationRequest:(UNNotificationRequest *)request withContentHandler:(void (^)(UNNotificationContent * _Nonnull))contentHandler
二、How to statistics
If you are connecting a 3rd push platform, you can check whether it supports. For example: Jiguang API: Notification Service Extension
If you implement it yourself, there are two options:
- Write network requests in the notification extension project and send the push arrival data to the backend server
- 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
Login to https://developer.apple.com ,Create
App Group
Configure in project,
target - Capabilites - App groups
Use in code
Use in
NSUserDefaults
1
2
3
4
5
6NSUserDefaults *userDefaults = [[NSUserDefaults alloc] initWithSuiteName:@"group.company.appGroupName"];
// write data
[userDefaults setValue:@"value" forKey:@"key"];
//read data
NSLog(@"%@", [userDefaults valueForKey:@"key"]);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];