简体   繁体   中英

React Native Push Notifications, FCM says token is invalid on iOS only (Android works)

We have a React Native App which is trying to send push notifications using the Firebase Cloud Messaging Service. We are testing this using Insomnia.

To do this, we are using a package called “react-native-push-notification” by zo0r. We've been able to obtain an iOS apn token from a real-life apple device. We are then able to successfully send a POST request to this URL:

https://iid.googleapis.com/iid/v1:batchImport 

to convert the apns token into a registration token for Google's firebase cloud messaging platform.

However, when we try and send a message using the converted token, by sending a POST request to this endpoint:

https://fcm.googleapis.com/fcm/send

using this JSON payload:

{
   "direct_book_ok": true,
    "to": "fTiQ5mg266o:APA91bGQN1s9sYJh9U_B6h1vuwAjpQVrPIJFnCWWsiv4PgKTCOCqLiKIdAD6ls48f-dBbnV3tOdc6NvTScXSRJYrXNHz_5QonSFraEau5jKSJPcw8HYeOb2YnutC39TlaIlKBnD3FpSx",
     "notification": {
                       "title": "Breaking News",
                        "body": "New Story available."
              },
     "priority": "high"
}

AND these headers:

Authorization: key=<OUR_SECRET_KEY> 
and 
Content-Type: application/json

The Google API responds with the following error, indicating that the notification has failed because of an “invalid argument”.

{
 "multicast_id": 3503657462083604439,
 "success": 0,
 "failure": 1,
 "canonical_ids": 0,
 "results": [
   {
     "error": "InvalidParameters: RpcError:INVALID_ARGUMENT"
   }
 ]
}

How would we remedy this? We've tried both sandbox=true and sandbox false.

I'm going to assume that since you used the v1 IID API to convert the APN token that you need to use the v1 FCM API to send a message. There's really no reason to continue to use the legacy API.

POST https://fcm.googleapis.com/v1/projects/<ProjectId>/messages:send

{
    "validate_only": false,
    "message": {
        "token": "fTiQ5mg266o:APA91bGQN1s9sYJh9U_B6h1vuwAjpQVrPIJFnCWWsiv4PgKTCOCqLiKIdAD6ls48f-dBbnV3tOdc6NvTScXSRJYrXNHz_5QonSFraEau5jKSJPcw8HYeOb2YnutC39TlaIlKBnD3FpSx",
        "notification": {
            "title": "Breaking News",
            "body": "New Story available."
        },
        "android": {
            "priority": "high"
        }
    }
}

REST API Docs

Also note that v1 doesn't use a server key like in the legacy API, you need to mint an OAuth token.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM