如何从android中的`onRefreshToken`外部访问firebase设备令牌

时间:2016-07-18 10:33:01

标签: android firebase firebase-cloud-messaging

我想从一个活动中访问firebase生成的令牌,并在我喜欢的函数内部访问:

async_write

我知道我可以在private void getFirebaseDeviceToken() { //get firebase token } 内获得此令牌,当令牌更改或生成全新令牌时,我的应用程序要求我在此服务之外和我的活动中访问此令牌发生。

我不知道该怎么做,如果有人能帮助我,我将不胜感激

4 个答案:

答案 0 :(得分:1)

您可以从应用中的任何位置获取令牌:

FirebaseInstanceId.getInstance().getToken(); 

如果您将令牌也存储在某个其他系统上(例如SharedPreferences或Firebase数据库中),请务必同时处理onTokenRefresh()以便您的辅助系统总是有最新的令牌。

答案 1 :(得分:0)

一种方法是在生成新令牌时将令牌存储在SharedPreferences中。

您可以从自定义方法中检索该令牌。

如果我们能够理解这种方法的功能和需要,那就更合适了。

答案 2 :(得分:0)

我做的是,我刚刚使用我的util将它保存在SharedPreferences中,如下所示:

Dim b1ps As Integer = 356 ' this is the weight of a bag of 1p's
Dim b2ps As Integer = 356 'this is the weight of a bad of 2p's
Dim b5ps As Integer = 325 'this is the weight of a bag of 5p's
Dim b10ps As Integer = 325 'this is the weight of a bag of 10p's
Dim b20ps As Integer = 250 'this is the weight of a bag of 20p's
Dim b50ps As Integer = 160 'this is the weight of a bag of 50p's
Dim b1pounds As Integer = 190 'this is the weight of a bag of £1's
Dim b2pounds As Integer = 120 'this is the weight of a bag of £2's

Dim GramsDifferent As Double 'this is a variable ready to use with working the difference in weight.
Dim WeightEntered As Double = WeightEnteredtxt.Text 'this is to store the weight entered.
Dim RemoveOrAdd As Double 'this is to store the number of coins to be taken out or added.

Dim removed As Integer = 0

If CoinType.Text = "1" Then
    ShouldWeigh.Text = b1ps
    GramsDifferent = 356 - WeightEntered
    OverBy.Text = GramsDifferent
    RemoveOrAdd = GramsDifferent / 3.56
    numdiff.Text = Math.Round(RemoveOrAdd, 0)
End If
If CoinType.Text = "2" Then
    ShouldWeigh.Text = b2ps
    GramsDifferent = 356 - WeightEntered
    OverBy.Text = GramsDifferent
    RemoveOrAdd = GramsDifferent / 7.12
    numdiff.Text = Math.Round(RemoveOrAdd, 0)
End If
If CoinType.Text = "5" Then
    ShouldWeigh.Text = b5ps
    GramsDifferent = 325 - WeightEntered
    OverBy.Text = GramsDifferent
    RemoveOrAdd = GramsDifferent / 3.25
    numdiff.Text = Math.Round(RemoveOrAdd, 0)
End If
If CoinType.Text = "10" Then
    ShouldWeigh.Text = b10ps
    GramsDifferent = 325 - WeightEntered
    OverBy.Text = GramsDifferent
    RemoveOrAdd = GramsDifferent / 6.5
    numdiff.Text = Math.Round(RemoveOrAdd, 0)
End If
If CoinType.Text = "20" Then
    ShouldWeigh.Text = b2ps
    GramsDifferent = 250 - WeightEntered
    OverBy.Text = GramsDifferent
    RemoveOrAdd = GramsDifferent / 5.0
    numdiff.Text = Math.Round(RemoveOrAdd, 0)
End If
If CoinType.Text = "50" Then
    ShouldWeigh.Text = b2ps
    GramsDifferent = 160 - WeightEntered
    OverBy.Text = GramsDifferent
    RemoveOrAdd = GramsDifferent / 8.0
    numdiff.Text = Math.Round(RemoveOrAdd, 0)
End If
If CoinType.Text = "£1" Then
    ShouldWeigh.Text = b2ps
    GramsDifferent = 190 - WeightEntered
    OverBy.Text = GramsDifferent
    RemoveOrAdd = GramsDifferent / 9.5
    numdiff.Text = Math.Round(RemoveOrAdd, 0)
End If
If CoinType.Text = "£2" Then
    ShouldWeigh.Text = b2ps
    GramsDifferent = 120 - WeightEntered
    OverBy.Text = GramsDifferent
    RemoveOrAdd = GramsDifferent / 12.0
    numdiff.Text = Math.Round(RemoveOrAdd, 0)
End If
Dim added As Integer = 0
If Val(numdiff.Text) > 0 Then
    posneg.Text = "Add"
    added = added + Val(numdiff.Text)
    TAdded.Text = added
End If
If numdiff.Text < 0 Then
    posneg.Text = "Remove"
End If

NumBagsChecked.Text = Val(NumBagsChecked.Text) + 1

每当我想获得那个令牌时,我也为此写了一个方法:

var testnr = document.getElementById("test").value;
testnr = testnr.replace(' ', '.');
alert(testnr)

答案 3 :(得分:0)

FirebaseInstanceId.getInstance()。getToken(); -> 不赞成使用getToken方法

您应该使用:

FirebaseInstanceId.getInstance().getInstanceId() 

1。这将返回一个Task回调,您可以在其上添加侦听器:

    FirebaseInstanceId.getInstance().getInstanceId()
    .addOnSuccessListener(this, //activity context
        instanceIdResult -> {instanceIdResult.getToken()
                             //do what you what with the token here  
                             });

}

*您应该传递Activity上下文,以便在活动结束时监听器将注销。

顺便提倡onRefreshToken()。 您现在应该在FcmMessagingService check out the docs

中使用onNewToken