background

Background Settings of the badge is 1, their Allies push over news no matter how many App horns are shown as 1 requirement: corner mark number increases with increase in the number of unread messages status: the background of notification message is sent if the single message set badge number, so each need to take a number of unread messages, lead to slow down the message queue

The implementation of

The number of unread messages is saved locally. Each time the number of unread messages is increased in the background of the mobile phone, and the number of unread messages is cleared in the foreground.

So where do I put the Angle increment?

Project support is iOS10 or above, so there are two agent methods using UNUserNotificationCenter. There is no way to receive push messages in the background, so the number of messages cannot be assigned

Void userNotificationCenter (UNUserNotificationCenter *) Center willPresentNotification (UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler __API_AVAILABLE (macos (10.14), the ios (10.0), watchos (3.0), tvos (10.0)); Void userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void(^)(void))completionHandler __API_AVAILABLE (macos (10.14), the ios (10.0), watchos (3.0)) __API_UNAVAILABLE (tvos);Copy the code

So in this case we can do the increment, but there’s a condition that we can only handle the increment in the background when the App starts, but if the App kills then we can’t do it.

# When the App is running, Receives the background news will enter the - (void) application: (UIApplication *) application didReceiveRemoteNotification: (the userInfo NSDictionary *) fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandlerCopy the code

NotificationServiceExtension preparation of App to create Groups

Since NotificationServiceExtension is a Target, and the main Target of the data is not Shared, so use NSUserDefaults data, at the same time cannot be Shared, using AppGroups.

  • Create AppGroups in Apple Developer Identifiers
  • In the main App and NotificationServiceExtension Identifier open App Groups function, configuration choice Groups created by the first step
  • In Xcode main App and the Targets of NotificationServiceExtension Signing & add App Groups, respectively, in the “Capabilities, and check the first step in Groups

Use of App Groups

NSUserDefaults

NSUserDefaults *userDefaults = [[NSUserDefaults alloc] initWithSuiteName:@" XXX (group name)"]; Data is retrieved and stored as usual, and must be created in the format aboveCopy the code

NSUserDefaults does not share files

Couldn't read values in CFPrefsPlistSource
Copy the code

NotificationServiceExtension create

File->New->Target Search for Service and select

  • After the Target Bundleid is created successfully, the corresponding description file needs to be created in Developer. I did not select push
  • After the creation, three files are created



If network request is required, the info.plist will also need App Transport Security Settings

  • The iOS of the new Target Deployment info should be the same as that of the main Target
  • Xcode-debug-attach to Process by PID or Name Select the Name of the new Target and Attach

NotificationServiceExtension use

Equivalent to interception notification request, you can modify the content of the notification, badge and other content, but also can do App did not start the payment sound playback note: the server push message structure aps internal parameters need to be added, the same as the badge

mutable-content:1
Copy the code

Find it in notificationService. m (I’m using file sharing here, NSUserDefaults sharing has a problem)

- (void)didReceiveNotificationRequest:(UNNotificationRequest *)request withContentHandler:(void (^)(UNNotificationContent * _Nonnull))contentHandler { self.contentHandler = contentHandler; self.bestAttemptContent = [request.content mutableCopy]; // Modify the notification content here... NSURL *groupURL = [[NSFileManager defaultManager] containerURLForSecurityApplicationGroupIdentifier:@"group.4TYNTWZC9Y.com.netrain.meinian"]; NSURL *fileURL = [groupURL URLByAppendingPathComponent:@"badgeNumber.txt"]; NSString *badgeString = [NSString stringWithContentsOfURL:fileURL encoding:NSUTF8StringEncoding error:nil]; // Add NSString *newBadgeString = [NSString stringWithFormat:@"%ld",[badgeString integerValue] + 1]; [newBadgeString writeToURL:fileURL atomically:YES encoding:NSUTF8StringEncoding error:nil]; NSLog(@" number of messages added ===== %@",newBadgeString); self.bestAttemptContent.badge = [NSNumber numberWithInteger:[newBadgeString integerValue]]; self.contentHandler(self.bestAttemptContent); }Copy the code

In the AppDelegate applicationDidBecomeActive method for Angle mark clean up

- (void)applicationDidBecomeActive:(UIApplication *)application{ NSURL *groupURL = [[NSFileManager defaultManager] containerURLForSecurityApplicationGroupIdentifier:@"group.4TYNTWZC9Y.com.netrain.meinian"]; NSURL *fileURL = [groupURL URLByAppendingPathComponent:@"badgeNumber.txt"]; Before NSLog (@ "clear message = = = = = % @", [nsstrings stringWithContentsOfURL: fileURL encoding: NSUTF8StringEncoding error: nil]); [@"0" writeToURL:fileURL atomically:YES encoding:NSUTF8StringEncoding error:nil]; }Copy the code

reference

IOS ShareExtension Couldn ‘t read values in CFPrefsPlistSource iOS UNNotificationServiceExtension app and the extension of the communication