2017-09-12 9 views
-1

私は一日中ソリューションを探していましたが、問題を解決できませんでした。以下のコードではデバイストークンは読み取れません。Firebase機能 - デバイストークンが空になった

以下にmy db構造体が含まれています。私はログを受け取ることができます:「あなたに新しいニュースがあります。私は新しいポストを追加しましたが、私は、ログを受信した「に送るべき通知トークンはありません。」それはものがすでに存在しているにもかかわらず、デバイストークンを検出できないことを意味し、どの。私は間違って何をしていますか?

{ 
 

 
    "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

'getDeviceTokensPromise'を作成するコードを投稿してください。それはいつ実行されますか? –

+0

申し訳ありませんが、私はそれを含めるのを忘れていました。//デバイス通知トークンのリストを取得します。 。 CONST getDeviceTokensPromise = admin.database()REF( '/ユーザ/ $ {followedUid}/notificationTokens').once( '値'); –

+0

データベースに構造体がないか、コードで期待される値が含まれています。追加のヘルプを得るには、失敗したユーザーID(トークンなどのプライベートデータのプレースホルダー付き)に対して、エクスポートされたJSONを投稿する必要があります。 Firebaseコンソールからエクスポートを実行できます。 –

答えて

0

私は私のfirebase DBときに、ユーザー登録またはログ内に格納し、デバイストークンを取得するには、私のfirebase funciton用として

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); 
               } 
              }); 

:。

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

編集した質問をもう一度チェックできますか?私は以前に関数を追加するのを忘れていました。 –

関連する問題