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'
}
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',
      );
    }
  }
};
Solve-1 :
Update your code for multiple users fcmToken.
**
Get a better response without error::

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

 
    
Top comments (0)