DEV Community

Cover image for Firebase push Notification Error: ⚠️ Auth error from APNS or Web Push Service
Sarwar Hossain
Sarwar Hossain

Posted on

18

Firebase push Notification Error: ⚠️ Auth error from APNS or Web Push Service

When Push Notification from Nodejs Express server by firebase -admin.

Your Error::

(node:internal/process/task_queues:95:5) {
  errorInfo: {
    code: 'messaging/third-party-auth-error',
    message: 'Auth error from APNS or Web Push Service'
  },
  codePrefix: 'messaging'
}
Enter fullscreen mode Exit fullscreen mode

Old Code::

import * as admin from 'firebase-admin';
// import path from 'path';
import ApiError from '../../../errors/ApiError';
import httpStatus from 'http-status';
import config from '../../../config';

admin.initializeApp({
  credential: admin.credential.cert(
    // path.join(
    //   __dirname,
    //   './jornee-66109-firebase-adminsdk-tf0wd-f34c47323d.json',
    // ),
    firebaseConfig as any,
  ),
});

type NotificationPayload = {
  title: string;
  body: string;
  data?: { [key: string]: string };
};

export const sendNotification = async (
  fcmToken: string,
  payload: NotificationPayload,
): Promise<any> => {
  // console.log(fcmToken, 'fcmTokenfcmToken');
  // console.log(payload, 'payload');
  try {
    const response = await admin.messaging().send({
      token: fcmToken,
      notification: {
        title: payload.title,
        body: payload.body,
      },
      // data: payload?.data,
    });
    // console.log('Successfully sent message:', response);
    return response;
  } catch (error: any) {
    console.error('Error sending message:', error);
    if (error?.code === 'messaging/third-party-auth-error') {
      // console.error('Skipping iOS token due to auth error:', error);
      return null;
    } else {
      console.error('Error sending message:', error);
      throw new ApiError(
        httpStatus.NOT_IMPLEMENTED,
        'Failed to send notification',
      );
    }
  }
};
Enter fullscreen mode Exit fullscreen mode

Solve-1 :

Update your code for multiple users fcmToken.

**

Image description

Get a better response without error::
Image description

Solve-2 :

  1. check your Firebase app settings and create Android and iOS apps.
  2. enable cloud messaging
  3. to provide correct fcmToken
  4. generate fcmToken from the production iOs app.

Image of Datadog

The Essential Toolkit for Front-end Developers

Take a user-centric approach to front-end monitoring that evolves alongside increasingly complex frameworks and single-page applications.

Get The Kit

Top comments (0)

Hostinger image

Get n8n VPS hosting 3x cheaper than a cloud solution

Get fast, easy, secure n8n VPS hosting from $4.99/mo at Hostinger. Automate any workflow using a pre-installed n8n application and no-code customization.

Start now

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay