2017-09-12 1 views
-1

Ich brauche Hilfe, ich habe den ganzen Tag nach Lösungen gesucht, aber ich kann mein Problem nicht beheben, der folgende Code liest die Tokens nicht.Firebase-Funktion - Geräte-Token leer

Darunter enthält meine db-Struktur. Ich schaffe es, das Protokoll zu erhalten: "Wir haben eine neue Nachricht für Sie." Als ich einen neuen Beitrag hinzugefügt habe, aber ich erhielt das Protokoll "Es gibt keine Benachrichtigungstoken zu senden." Was bedeutet, es kann die Geräte Tokens nicht erkennen, obwohl es bereits solche gibt. Was mache ich falsch?

{ 
 

 
    "Newsv2" : { 
 
"All" : { 
 

 
    "-Ktr7ZkuChCjsUIMb_4f" : { 
 
    "title" : "", 
 
    "type" : "", 
 
    } 
 
}, 
 

 
"Usersv2" : { 
 

 
"h0RzzpdO7nZVLpAR4fi7xRWUqsT2" : { 
 
    "device_token" : "", 
 
    "name" : "", 
 
    "user_no" : "" 
 
} 
 
    }, 
 
    
 
    } 
 

 

 

 
/--News 
 
    --All 
 
     --name 
 
     --desc 
 

 
/--Usersv2 
 
    --{userID} 
 
     --device_token 
 

 

 
exports.sendNotif = functions.database.ref('/Newsv2/All/{newsID}').onWrite(event => { 
 
    const newsID = event.params.newsID; 
 
    const userID = event.params.userID; 
 

 
    if (!event.data.val()) { 
 
    return console.log('News! ', newsID); 
 
    } 
 
    console.log('We have a new News for you!',newsID); 
 

 
    // Get the list of device notification tokens. 
 
    const getDeviceTokensPromise = admin.database().ref(`/Usersv2/${userid}/device_token`).once('value'); 
 

 
    return Promise.all([getDeviceTokensPromise]).then(results => { 
 
    const tokensSnapshot = results[0]; 
 
    //const follower = results[1]; 
 

 
    // Check if there are any device tokens. 
 
    if (!tokensSnapshot.hasChildren()) { 
 
     return console.log('There are no notification tokens to send to.'); 
 
    } 
 
    console.log('There are', tokensSnapshot.numChildren(), 'tokens to send notifications to.'); 
 
    //console.log('Fetched follower profile', follower); 
 

 
    // Notification details. 
 
    const payload = { 
 
     notification: { 
 
     title: 'Test Message', 
 
     body: '', 
 
     icon: '' 
 
     } 
 
    }; 
 

 
    // Listing all tokens. 
 
    const tokens = Object.keys(tokensSnapshot.val()); 
 

 
    // Send notifications to all tokens. 
 
    return admin.messaging().sendToDevice(tokens, payload).then(response => { 
 
     // For each message check if there was an error. 
 
     const tokensToRemove = []; 
 
     response.results.forEach((result, index) => { 
 
     const error = result.error; 
 
     if (error) { 
 
      console.error('Failure sending notification to', tokens[index], error); 
 
      // Cleanup the tokens who are not registered anymore. 
 
      if (error.code === 'messaging/invalid-registration-token' || 
 
       error.code === 'messaging/registration-token-not-registered') { 
 
      tokensToRemove.push(tokensSnapshot.ref.child(tokens[index]).remove()); 
 
      } 
 
     } 
 
     }); 
 
     return Promise.all(tokensToRemove); 
 
    }); 
 
    }); 
 
});

+0

Geben Sie den Code ein, der 'getDeviceTokensPromise' erstellt. Wann läuft es? –

+0

Entschuldigung, ich habe vergessen, es einzuschließen. // Holen Sie sich die Liste der Gerätebenachrichtigungstoken. const getDeviceTokensPromise = admin.database() .ref ('/ users/$ {followedUid}/notificationTokens') .once ('value'); –

+0

Ihre Datenbank hat nicht die Struktur oder enthält die Werte, die Ihr Code erwartet. Um zusätzliche Hilfe zu erhalten, müssen Sie den exportierten JSON für die Benutzer-ID veröffentlichen, die fehlschlägt (mit Platzhaltern für private Daten, z. B. Token). Sie können den Export über die Firebase-Konsole ausführen. –

Antwort

0

Um das Gerät Token zu erhalten lagere ich es in meiner Feuerbasis DB, wenn ein Benutzer registriert oder einloggt

private DatabaseReference mUserDatabase; 
    mUserDatabase = FirebaseDatabase.getInstance().getReference().child("Users/"); 
    //and if the login/register is successful 
    mUserDatabase.child("device_token").setValue(deviceToken).addOnSuccessListener(new OnSuccessListener<Void>() { 
                @Override 
                public void onSuccess(Void aVoid) { 
                Intent intent = new Intent(application.getApplicationContext(), MainActivity.class); 
                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |Intent.FLAG_ACTIVITY_NEW_TASK); 
                application.startActivity(intent); 
               } 
              }); 

wie für meine Feuerbasis funciton.

const deviceToken = admin.database().ref(`/Users/${unique_id}/device_token`).once('value'); 
+0

Können Sie meine bearbeitete Frage noch einmal überprüfen? Ich habe vergessen, die Funktion früher hinzuzufügen. –

Verwandte Themen