iOS: SDK App Groups setup

OneSignal iOS App Groups setup for Badges and Confirmed Deliveries

📘

Required for certain features

Setting up an App Group in Xcode is required to allow badge incrementing and Confirmed Deliveries.

1. Add Notification Service Extension

If you have not already done so, follow our Mobile SDK Setup Guides for iOS to step up the Notification Service Extension.

2. Enable "App Groups" Capability

  1. In your Main App Target
  2. Go to "Signing & Capabilities" > "All"
  3. Click "+ Capability" if you do not have App Groups in your app yet.
  4. Select App Groups
2282
  1. Under App Groups click the "+" button
  2. Set the "App Groups" container to be group.YOUR_BUNDLE_IDENTIFIER.onesignal where YOUR_BUNDLE_IDENTIFIER is the same as set in "Bundle Identifier"
  3. Press OK
2294
  1. In the OneSignalNotificationServiceExtension Target
  2. Go to "Signing & Capabilities" > "All"
  3. Click "+ Capability" if you do not have App Groups in your app yet.
  4. Select App Groups
2270
  1. Under App Groups click the "+" button
  2. Set the "App Groups" container to be group.YOUR_BUNDLE_IDENTIFIER.onesignal where YOUR_BUNDLE_IDENTIFIER is the same as your Main App Target "Bundle Identifier". Do Not Include OneSignalNotificationServiceExtension.
  3. Press OK
2272

3. Setup OneSignal key in .plist

This step is only required if you don't want to use the default app group name (which is group.{your_bundle_id}.onesignal).

1. Open your Info.plist file and add a new OneSignal_app_groups_key as a String type.
2. Enter the group name you checked in the last step as it's value.
3. Make sure to do the same for the Info.plist under the OneSignalNotificationServiceExtension folder.

1662

👍

Done!

That is all you should need to send iOS Push Notifications with Badges.

If you are sending push and notice the badge count is not incrementing correctly then continue below.

iOS App Groups Troubleshooting

Deployment Target

Make sure your OneSignalNotificationServiceExtension has a Deployment Target of 10 or higher. iOS 10 is when the Notification Service Extension was created for adding Rich Media to your iOS Push Notifications.

Entitlements Match

Your Entitlements file may not match your Capabilities selected in Xcode. Check your .entitlements files: Entitlements-Release.plist file and Entitlements-debug.plist file to make sure you have App Groups enabled for each.

You can find entitlements file location from Xcode > Your Targets > Build Settings > then search Entitlements.

For example, if your .entitlements file or Entitlements-Release.plist and Entitlements-debug.plist do not contain App Groups, make sure to add them to both targets:

2274
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
  <dict>
    <key>aps-environment</key>
    <string>production</string>
    <key>com.apple.security.application-groups</key>
    <array>
      <string>group.YOUR_BUNDLE_IDENTIFIER.onesignal</string> <!--Fill your bundle id-->
    </array>
  </dict>
</plist>

Make sure these files are setup the same for the Main App Target and OneSignalNotificationServiceExtension Target.

2278

More details see this github issue.

Check your Provisioning Profiles are setup

You may need to setup 2 Provisioning Profiles: for your main app target and for the OneSignal Notification Service Extension target.

See Step 5 of Generate an iOS Push Certificate.

Make sure the App Groups in both Provisioning Profiles (your main app target and Notification Service Extension Target) match in Apple Developer Account. You may need to download these files and import into Xcode.

For example, in your Xamarin iOS project, click on the main app target project and go to Options to see if the new provisioning profile appeared. Do the same for the NSE project and the provisioning profile of the NSE should appear. Both, in the Custom Entitlements should be filled with “Entitlements.plist”.

For publishing the app to the App Store, some needed to create two provisioning profiles (associated with the Distribution certificates); one for the main app and the other one for the NSE project.

Logging within the NSE

Badge Counts are saved within the UserDefaults for your App Group. To verify data is being saved correctly, follow these instructions:

1. Add Example User Defaults

In your AppDelegate or first ViewController add the following code replacing the App Group Name ("group.YOUR_BUNDLE_ID.onesignal") with your own:

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
  
        if let userDefaults = UserDefaults(suiteName: "group.YOUR_BUNDLE_ID.onesignal") {
            userDefaults.set("test 1" as AnyObject, forKey: "key1")
            userDefaults.set("test 2" as AnyObject, forKey: "key2")
            userDefaults.synchronize()
        }
        if let userDefaults = UserDefaults(suiteName: "group.YOUR_BUNDLE_ID.onesignal") {
            let value1 = userDefaults.string(forKey: "key1")
            let value2 = userDefaults.string(forKey: "key2")
            print("value1 = ", value1?.description ?? "No value")
            print("value2 = ", value2?.description ?? "No value")
        }
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
	
  NSUserDefaults * shared = [[NSUserDefaults alloc] initWithSuiteName:@"group.YOUR_BUNDLE_ID.onesignal"];
	[shared setObject:@"test 1" forKey:@"key1"];
  [shared setObject:@"test 2" forKey:@"key2"];
	[shared synchronize];

2. Log the Example User Defaults within the NotificationServiceExtension

In your NotificationService file for the NSE, add the following within the bestAttemptContent if-let statement:

if let bestAttemptContent = bestAttemptContent {
    OneSignal.didReceiveNotificationExtensionRequest(self.receivedRequest, with: self.bestAttemptContent)
    if let userDefaults = UserDefaults(suiteName: "group.YOUR_BUNDLE_ID.onesignal") {
        let value1 = userDefaults.string(forKey: "key1")
        let value2 = userDefaults.string(forKey: "key2")
        print("NSE value1 = ", value1?.description ?? "No value")
        print("NSE value2 = ", value2?.description ?? "No value")
        bestAttemptContent.title = value1 ?? "value1 Not Set"
        bestAttemptContent.body = value2 ?? "value2 Not Set"
    }
    contentHandler(bestAttemptContent)
}
[OneSignal didReceiveNotificationExtensionRequest:self.receivedRequest
                   withMutableNotificationContent:self.bestAttemptContent];

NSUserDefaults *shared = [[NSUserDefaults alloc] initWithSuiteName:@"group.YOUR_BUNDLE_ID.onesignal"];
id value1 = [shared valueForKey:@"key1"];
id value2 = [shared valueForKey:@"key2"];

self.bestAttemptContent.title = value1
self.bestAttemptContent.body = value2

self.contentHandler(self.bestAttemptContent);

3. Run the App on Device and send Push

You can now select the OneSignalNotificationServiceExtension Schema and then run the app.

2320

If you don't change the Schema that is ok, you should see the push data change as well.

Send your device a push notification.

If you changed the Scheme, view the log Debug Area, you should see:

NSE value1 =  test 1
NSE value2 =  test 2

And the Push Notification should show test 1 for the title and test 2 for the message body.

If you do not see these values set, the App Group in step 1 is not valid and needs to be setup again.


What’s Next