> ## Documentation Index
> Fetch the complete documentation index at: https://moengage.com/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# iOS Swift バージョン 7.x.x を使用する iOS デバイスでよくあるプッシュ通知の問題を回避するには

> Swift SDK 7.x.x を使用する iOS でのプッシュインプレッションのトラッキングと画像レンダリングの問題を修正します。MoEngage プロジェクト設定で appgroupid の構成を確認してください。

## 問題

Swift バージョン 7.x.x を使用するデバイスにプッシュ通知が正常に配信されているにもかかわらず、プッシュキャンペーンのインプレッションがトラッキングされない、画像がレンダリングされないなど、プッシュ通知に関するよくある問題が発生しています。

> [!NOTE]
>
> プッシュキャンペーンのインプレッションをトラッキングするには、MoEngage ダッシュボードで **Show iOS impressions** トグルがオンになっていることを確認してください。
>
> ![iOS imp.png](https://mintcdn.com/moengage/T-dEj4C-7pywzBS3/images/moengage_a3edc2.png?fit=max\&auto=format\&n=T-dEj4C-7pywzBS3\&q=85\&s=bc3cc82b6d292d041851753f65793971)

## 解決策

次の手順を実行します。

1. **各構成でプロジェクトのメインターゲットに app\_group\_id があるか確認する**：
   構成間で同じ app\_group\_id を追加することも、異なるものを追加することもできます。
   > [!NOTE]
   >
   > 構成で異なる Bundle Identifier を使用しながら同じ app\_group\_id を共有している場合、問題が発生する可能性があります。具体的には、Bundle Identifier が異なり app\_group\_id が同じ 2 つのアプリが同じデバイスにインストールされている場合、Notification Service Extension が失敗することがあります。
   > これを回避するために、複数の構成で同じ app\_group\_id を使用できますが、その場合は 1 台のデバイスですべてのアプリを同時にテストすることはできない点に注意してください。
   ![app group.png](https://mintcdn.com/moengage/S5VM4C7aiU6sct6c/images/moengage_053674.png?fit=max\&auto=format\&n=S5VM4C7aiU6sct6c\&q=85\&s=9ff176b82bdf0f2cf750597f5e1cb4ca)
2. **SDK の初期化時に app\_group\_id があるか確認する**：
   * info.plist を使用して SDK を初期化している場合は、info.plist ファイルの MoEngage キー内に APP\_GROUP\_ID キーが存在している必要があります。
   * sdkConfig を使用して SDK を初期化している場合は、AppDelegate ファイル内の sdkConfig 変数に APP\_GROUP\_ID が存在している必要があります。\
     **info.plist 内の app\_group\_id：**

     ![app id.png](https://mintcdn.com/moengage/prFQLkdcYp4xSt9t/images/moengage_667e35.png?fit=max\&auto=format\&n=prFQLkdcYp4xSt9t\&q=85\&s=bf6324154507b577d72be8e630926192)

     **sdkConfig 変数内の app\_group\_id：**
   ```swift Swift theme={null}
   var sdkConfig = MOSDKConfig.init(appID: "XXXXXXXXXXXXXXXX")
   sdkConfig.moeDataCenter = DATA_CENTER_0X
   sdkConfig.appGroupID = "group.com.XXXXXXXXXXXXXXXX"
   //if SDK initialized without sdkConfig in AppDelegate file
   MoEngage.setAppGroupID("group.com.moengage.MIPL567.demo")
   ```
   ```Objective-C Objective-C theme={null}
   MOSDKConfig\* sdkConfig = [[MOSDKConfig alloc] initWithAppID: @"XXXXXXXXXXXXXXXX"];
   sdkConfig.moeDataCenter = DATA_CENTER_0X;
   sdkConfig.appGroupID = @"group.com.XXXXXXXXXXXXXXXX";
   //if SDK initialized without sdkConfig in AppDelegate file
   [MoEngage setAppGroupID: @"group.com.XXXXXXXXXXXXXXXX"];
   ```
3. **Notification Service Extension ターゲットがあるか確認する**：
   * **Push Notifications を有効にする**：Extension ターゲットの Signing & Capabilities セクションで、必ず Push Notifications を有効にし、AppGroupId を追加してください。

     ![enable.png](https://mintcdn.com/moengage/8EX_gHAg3q4zA1kw/images/moengage_23f29a.png?fit=max\&auto=format\&n=8EX_gHAg3q4zA1kw\&q=85\&s=45c8cd1f8f353d3e001d74018a76af09)
   * **最小デプロイ iOS バージョンを揃える**：Notification Service Extension の最小デプロイ iOS バージョンが、メインアプリの iOS バージョンと一致していることを確認します。

     ![support.png](https://mintcdn.com/moengage/-X0av6ek9AEOUtsy/images/moengage_cb3695.png?fit=max\&auto=format\&n=-X0av6ek9AEOUtsy\&q=85\&s=8cde3e02fc5ab39f803ef3da413d8e2b)
4. MORichNotification フレームワークを Notification Service Extension ターゲットに統合します。
   ```swift Swift theme={null}
   target 'MoEngageDemo' do
     use_frameworks!
     pod 'MoEngage-iOS-SDK'
   end
   target 'MoEngageNotificationService' do
     use_frameworks! #use use_frameworks only if included in main target as in above scenario
     pod 'MORichNotification'
   end
   ```
5. NotificationService.swift ファイルは次のようになります。
   ```swift Swift theme={null}
   import UserNotifications
   import MORichNotification
    
   class NotificationService: UNNotificationServiceExtension {
    
       var contentHandler: ((UNNotificationContent) - Void)?
       var bestAttemptContent: UNMutableNotificationContent?
    
       override func didReceive(_ request: UNNotificationRequest, withContentHandler contentHandler: @escaping (UNNotificationContent) - Void) {	
         MORichNotification.setAppGroupID("group.com.XXXXXXXXXXXXXXXX")
         self.contentHandler = contentHandler
         bestAttemptContent = (request.content.mutableCopy() as? UNMutableNotificationContent)
         MORichNotification.handle(request, withContentHandler: contentHandler)
       }
       
       override func serviceExtensionTimeWillExpire() {
           // Called just before the extension will be terminated by the system.
           // Use this as an opportunity to deliver your "best attempt" at modified content, otherwise the original push payload will be used.
           if let contentHandler = contentHandler, let bestAttemptContent =  bestAttemptContent {
               contentHandler(bestAttemptContent)
           }
       }
   }
   ```
6. **メインアプリターゲットの Build Phases を確認する**：メインアプリターゲットの Build Phases セクションで、Embed App Extensions / Embed Foundation Extensions セクションを見つけます。**Copy only when installing** オプションのチェックが外れていることを確認します。

![copy.png](https://mintcdn.com/moengage/xaH8HoR16_YVDek5/images/moengage_cfdd8e.png?fit=max\&auto=format\&n=xaH8HoR16_YVDek5\&q=85\&s=de18a2fe829e17cc0cb6484074d584fc)

7. **構成間で appGroupId を統一する**：プロジェクト内のすべてのスキームと構成（例：Debug/Release/QA/UAT）で appGroupId が一致していることを確認します。
8. **ビルド構成を揃える**：プロジェクトを実行またはアーカイブする際は、メインターゲットと Notification Service Extension ターゲットの Build Configuration が同じスキーム/構成を指していることを確認します。

![run.png](https://mintcdn.com/moengage/o0kXRAcOq2mxzDnY/images/moengage_ef15c5.png?fit=max\&auto=format\&n=o0kXRAcOq2mxzDnY\&q=85\&s=11ade9afaae65618cf3532aa5e8b50a4)

![build.png](https://mintcdn.com/moengage/H6NVBgn3z5fYOOBq/images/moengage_7a3664.png?fit=max\&auto=format\&n=H6NVBgn3z5fYOOBq\&q=85\&s=473f6f90a29e22202599ec72bfe9763b)


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.