Skip to content

angular-universal | firebase-admin | code: 'app/invalid-credential' |socket hang up on #72

@Skillnter

Description

@Skillnter

[REQUIRED] Step 2: Describe your environment

  • Operating System version: Windows 10
  • Firebase SDK version: 5.1.0
  • Library version: <What?>
  • Firebase Product: Cloud Messaging

[REQUIRED] Step 3: Describe the problem

import * as admin from 'firebase-admin';
var serviceAccount = require('./keys/keyfile.json');
admin.initializeApp({
  credential: admin.credential.cert(serviceAccount),
  databaseURL: "https://test.firebaseio.com"
});

var registrationToken = "--some-key--";

var payload = {
  notification: {
    title: "$GOOG up 1.43% on the day",
    body: "$GOOG gained 11.80 points to close at 835.67, up 1.43% on the day."
  },
  data: {
    stock: "GOOG",
    open: "829.62",
    close: "635.67"
  }
};


function panelMessage(){
admin.messaging().sendToDevice(registrationToken, payload)
  .then(function(response) {
    // See the MessagingDevicesResponse reference documentation for
    // the contents of response.
    console.log("Successfully sent message:", response);
  })
  .catch(function(error) {
    // console.log(serviceAccount);
    var date = new Date();
    console.log(date.toString());
    console.log("Error sending message:", error);
  });
}

I run this code through node and it work but when i add it in angular-universal

server.ts

file.. it give the following error..

Error sending message: { Error: Credential implementation provided to initializeApp() via the "credential" property failed to fetch a valid Google OAuth2 access token with the following error: "socket hang up". at FirebaseAppError.Error (native) at FirebaseAppError.FirebaseError [as constructor] (D:\angular2MH\firebase - panel\panel\node_modules\firebase-admin\lib\utils\error.js:39:28) at new FirebaseAppError (D:\angular2MH\firebase - panel\panel\node_modules\firebase-admin\lib\utils\error.js:84:23) at D:\angular2MH\firebase - panel\panel\node_modules\firebase-admin\lib\firebase-app.js:119:23 at ZoneDelegate.invoke (D:\angular2MH\firebase - panel\panel\node_modules\zone.js\dist\zone-node.js:232:26) at Zone.run (D:\angular2MH\firebase - panel\panel\node_modules\zone.js\dist\zone-node.js:114:43) at D:\angular2MH\firebase - panel\panel\node_modules\zone.js\dist\zone-node.js:502:57 at ZoneDelegate.invokeTask (D:\angular2MH\firebase - panel\panel\node_modules\zone.js\dist\zone-node.js:265:35) at Zone.runTask (D:\angular2MH\firebase - panel\panel\node_modules\zone.js\dist\zone-node.js:154:47) at drainMicroTaskQueue (D:\angular2MH\firebase - panel\panel\node_modules\zone.js\dist\zone-node.js:401:35)
errorInfo: { code: 'app/invalid-credential', message: 'Credential implementation provided to initializeApp() via the "credential" property failed to fetch a valid Google OAuth2 access token with the following error: "socket hang up".' } }

I checked the time (read at many place that could be the issue) and they are all synced.. Can anyone help me out with the issue? I'm using this repository https://github.com/Skillnter/angular-universal

Steps to reproduce:
1). Get the angular-universal from git https://github.com/Skillnter/angular-universal
2). open file \src\server.ts
3). write the following code with respective values of keyfile and

import * as admin from 'firebase-admin';
var serviceAccount = require('./keys/keyfile.json');
admin.initializeApp({
  credential: admin.credential.cert(serviceAccount),
  databaseURL: "https://test.firebaseio.com"
});

var registrationToken = "--some-key--";

var payload = {
  notification: {
    title: "$GOOG up 1.43% on the day",
    body: "$GOOG gained 11.80 points to close at 835.67, up 1.43% on the day."
  },
  data: {
    stock: "GOOG",
    open: "829.62",
    close: "635.67"
  }
};


function panelMessage(){
admin.messaging().sendToDevice(registrationToken, payload)
  .then(function(response) {
    // See the MessagingDevicesResponse reference documentation for
    // the contents of response.
    console.log("Successfully sent message:", response);
  })
  .catch(function(error) {
    // console.log(serviceAccount);
    var date = new Date();
    console.log(date.toString());
    console.log("Error sending message:", error);
  });
}

and use the panelMessage function after server started..

let server = app.listen(app.get('port'), () => {
  
  console.log(`Listening on: http://localhost:${server.address().port}`, __dirname);

  panelMessage();
  
});

4). open CMD(windows)/terminal(Linux) and run "npm start" in the working directory of angular-universal

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions