# expo notificationでPUSH_TOO_MANY_EXPERIENCE_IDSのエラーが出たときの対処方法

> expo notificationで、PUSH_TOO_MANY_EXPERIENCE_IDSのエラーの詳細や対処方法について解説しています。

- 公開日: 2024-09-27
- 著者: そうま
- タグ: Expo, Rails, ReactNative
- URL: https://tech.anycloud.co.jp/articles/fix-expo-notifications-error

---

[expo-notifications](https://www.npmjs.com/package/expo-notifications)と[expo-server-sdk-ruby](https://github.com/expo-community/expo-server-sdk-ruby)を使用して、プッシュ通知の送信を実装していたのですが、プッシュ通知送信時に以下のようなエラーが発生して、プッシュ通知を送信できないことがあったので、備忘録として対処方法を載せておきます。

```json
Unknown error format: {
  "errors":[{
    "code":"PUSH_TOO_MANY_EXPERIENCE_IDS",
    "message":"All push notification messages in the same request must be for the same project; check the details field to investigate conflicting tokens.",
    "details":{
      "@project1":["ExponentPushToken[token1]","ExponentPushToken[token2]","ExponentPushToken[token3]","ExponentPushToken[token4"],
      "@project2":["ExponentPushToken[userToken1]","ExponentPushToken[userToken2]"]
    },
    "isTransient":false
  }]
}
```

## 前提

-   [expo-notifications](https://www.npmjs.com/package/expo-notifications)を使用している
-   [expo-server-sdk-ruby](https://github.com/expo-community/expo-server-sdk-ruby)を使用している
-   DBにexpo tokenを保存しているかつ、プロジェクトの情報は保存していない

## エラーの詳細

このエラーのコードは`PUSH_TOO_MANY_EXPERIENCE_IDS`で、プッシュ通知を複数のexpoプロジェクトに送信すると発生します。

[expo-server-sdk-ruby](https://github.com/expo-community/expo-server-sdk-ruby)だと以下のように、一度に複数のtokenに対してプッシュ通知を送信することができます。

```ruby
client = Exponent::Push::Client.new
expo_tokens = ["ExponentPushToken[token1]", "ExponentPushToken[token2]"]

client.send_messages({ to: expo_tokens, title: 'title', body: 'body' })
```

このときに、`ExponentPushToken[token1]` と`ExponentPushToken[token2]` が別のexpoプロジェクトに紐付いていると、今回のエラーが発生します。

参考

<div class="link-card-wrap"><a class="link-card" href="https://docs.expo.dev/push-notifications/sending-notifications/" target="_blank" rel="noopener noreferrer"><span class="link-card-body"><span class="link-card-title">Send notifications with the Expo Push Service</span><span class="link-card-description">Learn how to call Expo Push Service API to send push notifications from your server.</span><span class="link-card-meta"><img class="link-card-favicon" src="./linkcard-01-favicon.ico" alt=""><span class="link-card-domain">docs.expo.dev</span></span></span><img class="link-card-image" src="./linkcard-01-image.webp" alt=""></a></div>

## 解決策

解決するには、単純にプッシュ通知送信時に、別プロジェクトのtokenが混じらないようにするしかありません。

僕らのプロダクトでは、DBにtokenを保存していたため、DBから別プロジェクトのtokenを削除する必要がありました。

しかし、tokenからプロジェクトの情報を取得する方法が提供されていないため、`PUSH_TOO_MANY_EXPERIENCE_IDS` のエラーをハンドリングして、エラー詳細から別プロジェクトのtokenを取得し、削除するようにしました。

以下のようにエラーメッセージに、プロジェクトごとのtokenの情報があるため、エラーメッセージをパースして、別プロジェクトのtokenを削除してから、プッシュ通知の再送信を行うようにしています。

```json
Unknown error format: {
  "errors":[{
    "code":"PUSH_TOO_MANY_EXPERIENCE_IDS",
    "message":"All push notification messages in the same request must be for the same project; check the details field to investigate conflicting tokens.",
    "details":{
      // プロジェクトごとの情報
      "@project1":["ExponentPushToken[token1]","ExponentPushToken[token2]","ExponentPushToken[token3]","ExponentPushToken[token4"],
      "@project2":["ExponentPushToken[userToken1]","ExponentPushToken[userToken2]"]
    },
    "isTransient":false
  }]
}
```

注意点としては、`exponent-server-sdk-ruby` の最新リリースでは、エラーメッセージにプロジェクトの情報が表示されませんが、最新のコミットでは表示されるようになっているため、コミットを指定してgemをインストールする必要があります。

```ruby
gem 'exponent-server-sdk', git: '<https://github.com/expo-community/expo-server-sdk-ruby.git>', ref: '9f1e2397e528484375f1fc76e3929c6cd8060a76'
```

## まとめ

かなりエッジケースかもしれませんが、今回のエラーの日本語のドキュメントが見つからなかったため、書いてみました。

プロジェクト情報を取得するために、エラーメッセージをパースするという、少し辛いことをしているので、もっと良い解決策をご存知の方は教えていただけると嬉しいです。。

また、これからexpo-notificationsを使用して、プッシュ通知の実装をする方がいれば、tokenとprojectIdをセットで保存しておくと、とても楽に対処できるのでおすすめです。

## 参考

同様のIssue

<div class="link-card-wrap"><a class="link-card" href="https://github.com/expo-community/expo-server-sdk-ruby/issues/39" target="_blank" rel="noopener noreferrer"><span class="link-card-body"><span class="link-card-title">Exponent::Push::UnknownError: Unknown error format: #&lt;Typhoeus::Response... · Issue #39 · expo-community/expo-server-sdk-ruby</span><span class="link-card-description">I know this is similar to issues #33, #37, and #38; however, it has slightly different symptoms (different top expo SDK stack frames, only sending a single message, etc.). The following shows my ap...</span><span class="link-card-meta"><img class="link-card-favicon" src="./linkcard-02-favicon.svg" alt=""><span class="link-card-domain">github.com</span></span></span><img class="link-card-image" src="./linkcard-02-image.webp" alt=""></a></div>

公式ドキュメント

<div class="link-card-wrap"><a class="link-card" href="https://docs.expo.dev/push-notifications/sending-notifications/" target="_blank" rel="noopener noreferrer"><span class="link-card-body"><span class="link-card-title">Send notifications with the Expo Push Service</span><span class="link-card-description">Learn how to call Expo Push Service API to send push notifications from your server.</span><span class="link-card-meta"><img class="link-card-favicon" src="./linkcard-03-favicon.ico" alt=""><span class="link-card-domain">docs.expo.dev</span></span></span><img class="link-card-image" src="./linkcard-03-image.webp" alt=""></a></div>

<div class="link-card-wrap"><a class="link-card" href="https://docs.expo.dev/push-notifications/sending-notifications/" target="_blank" rel="noopener noreferrer"><span class="link-card-body"><span class="link-card-title">Send notifications with the Expo Push Service</span><span class="link-card-description">Learn how to call Expo Push Service API to send push notifications from your server.</span><span class="link-card-meta"><img class="link-card-favicon" src="./linkcard-04-favicon.ico" alt=""><span class="link-card-domain">docs.expo.dev</span></span></span><img class="link-card-image" src="./linkcard-04-image.webp" alt=""></a></div>
