> ## 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 SDK バージョン 7.x.x を使用する iOS デバイスで、プッシュ通知の一般的な問題を回避するにはどうすればよいですか？

> iOS SDK 7.x.x で、プッシュキャンペーンのインプレッショントラッキングと画像レンダリングの問題を解決します。MoEngage のセットアップで appgroupid を設定する手順を説明します。

## 問題

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

<Info>
  * インプレッションをトラッキングするには、アプリに Notification Service Extension が実装されていることを確認してください。
  * また、MoEngage ダッシュボードで **Settings → Channel → Push → IOS** に移動し、**Show iOS impressions** トグルを有効にしてください。
      <img alt="iOS imp.png" src="https://mintcdn.com/moengage/QRy8rcYszbTJNwGr/images/moengage_b3e15a.png?fit=max&auto=format&n=QRy8rcYszbTJNwGr&q=85&s=d07ef45cca12791fbc220b3c6a4d4846" width="2744" height="1582" data-path="images/moengage_b3e15a.png" />
</Info>

## 解決策

以下の手順を実行します。

1. **各構成について、プロジェクトのメインターゲットに app\_group\_id を追加する**:
   構成間で同じ app\_group\_id を追加することも、異なる app\_group\_id を追加することもできます。
   <Info>
     構成で異なる Bundle Identifier を使用しながら同じ app\_group\_id を共有している場合、問題が発生する可能性があります。具体的には、Bundle Identifier が異なり app\_group\_id が同じ 2 つのアプリが同じデバイスにインストールされている場合、Notification Service Extension が失敗する可能性があります。
     これを回避するために複数の構成で同じ app\_group\_id を使用できますが、その場合は 1 台のデバイスですべてのアプリを同時にテストできないことに注意してください。
   </Info>
   <img src="https://mintcdn.com/moengage/7H2dU4CG2W0DQCPI/images/moengage_6f8ef6.png?fit=max&auto=format&n=7H2dU4CG2W0DQCPI&q=85&s=38f5992106ab5ade5961dba89eca8647" alt="app group.png" width="1920" height="709" data-path="images/moengage_6f8ef6.png" />
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:**
     <img alt="app id.png" src="https://mintcdn.com/moengage/eF2J43N9Ckga5Apm/images/moengage_2f7c53.png?fit=max&auto=format&n=eF2J43N9Ckga5Apm&q=85&s=846320e12dae5ccd184fb7cae0cd293e" width="1920" height="888" data-path="images/moengage_2f7c53.png" />
   **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")
   ```
   ```swift 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 ターゲットを作成する**:
   <img alt="NSE.png" src="https://mintcdn.com/moengage/Ci8j4F7VOEUnUuwd/images/moengage_ecada5.png?fit=max&auto=format&n=Ci8j4F7VOEUnUuwd&q=85&s=19a23e755eee40a9d38045ad3c39ce0d" width="1432" height="1008" data-path="images/moengage_ecada5.png" />
   * **拡張ターゲットに名前を付ける**: 拡張ターゲットの名前を選択します。Swift は Objective-C プロジェクトともシームレスに連携するため、言語として Swift を推奨します。 <img alt="name.png" src="https://mintcdn.com/moengage/QVZKszouQuxXOzFr/images/moengage_1a574b.png?fit=max&auto=format&n=QVZKszouQuxXOzFr&q=85&s=35f2a4711aa4946713023c0ce874a08c" width="1440" height="1012" data-path="images/moengage_1a574b.png" />
   * **プッシュ通知を有効にする**: 拡張ターゲットの Signing & Capabilities セクションで、Push Notifications を有効にし、AppGroupId を追加します。
     <img alt="enable.png" src="https://mintcdn.com/moengage/prFQLkdcYp4xSt9t/images/moengage_67a2f5.png?fit=max&auto=format&n=prFQLkdcYp4xSt9t&q=85&s=f046012d94179d872ba8d2fd40ca60b1" width="1920" height="706" data-path="images/moengage_67a2f5.png" />
   * **最小デプロイ iOS バージョンを揃える**: Notification Service Extension の最小デプロイ iOS バージョンが、メインアプリの iOS バージョンと一致していることを確認します。 <img alt="support.png" src="https://mintcdn.com/moengage/xeb_76Bworf1GG6P/images/moengage_9719f4.png?fit=max&auto=format&n=xeb_76Bworf1GG6P&q=85&s=c8874b37d35d14390194840a12678909" width="1920" height="563" data-path="images/moengage_9719f4.png" />
4. **MORichNotification フレームワークを Notification Service Extension ターゲットに統合する**: podfile で、Notification Service Extension を別のターゲットとして追加し、MORichNotification フレームワークをインストールします。
   ```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** オプションがオフになっていることを確認します。
   <img alt="copy.png" src="https://mintcdn.com/moengage/4hNKOAjPrnhpJB2_/images/moengage_7feeda.png?fit=max&auto=format&n=4hNKOAjPrnhpJB2_&q=85&s=3e95fad688bc658eac356225c9dc29ee" width="1920" height="652" data-path="images/moengage_7feeda.png" />
7. **構成間で appGroupId を統一する**: プロジェクト内のすべてのスキームと構成（Debug/Release/QA/UAT など）で appGroupId が統一されていることを確認します。
8. **ビルド構成を揃える**: プロジェクトを実行またはアーカイブする際は、メインターゲットと Notification Service Extension ターゲットの両方の Build Configuration が同じスキーム/構成を指していることを確認します。
   <img alt="run.png" src="https://mintcdn.com/moengage/7H2dU4CG2W0DQCPI/images/moengage_6fb391.png?fit=max&auto=format&n=7H2dU4CG2W0DQCPI&q=85&s=7448f5acd2ae201cbafdeb2bdb2b42c3" width="1750" height="556" data-path="images/moengage_6fb391.png" />
   <img alt="build.png" src="https://mintcdn.com/moengage/T-dEj4C-7pywzBS3/images/moengage_a30419.png?fit=max&auto=format&n=T-dEj4C-7pywzBS3&q=85&s=9ffeb0463badd6fd595c6b1510c69258" width="1832" height="446" data-path="images/moengage_a30419.png" />
