Firebase成功,但没有收到通知
问题描述:
所以我让我的应用程序运行并且通知在第一时间正常工作。但过了一段时间,它就停止了。Firebase成功,但没有收到通知
我试过通过Firebase控制台发送并返回成功,但应用程序未收到通知。该应用程序在后台,它更早的工作,无论是在应用程序或服务器端的代码没有改变。
如果我重新安装了应用程序(获取新的令牌),通知将恢复正常(按预期工作),但如果令牌到期不应该Firebase控制台返回错误(注册令牌错误或其他)?
自从我使用该应用程序以来只有几天,为什么令牌过期(文档说令牌将在6个月内过期)? 而且我知道每次用户运行我的应用程序时实际强制刷新令牌是一种糟糕的方法。
那么我做错了什么?因为如果我调试我的应用程序一切正常。得到了令牌,得到了通知。
是的,通过FCM控制台和通过服务器发送通知产生相同的结果;成功,但没有通知。
的onTokenRefresh代码looklike这样的:
namespace FCMClient
{
[Service]
[IntentFilter(new[] { "com.google.firebase.INSTANCE_ID_EVENT" })]
public class MyFirebaseIIDService : FirebaseInstanceIdService
{
const string TAG = "MyFirebaseIIDService";
public override void OnTokenRefresh()
{
var refreshedToken = FirebaseInstanceId.Instance.Token;
Log.Debug(TAG, "Refreshed token: " + refreshedToken);
SendRegistrationToServer(refreshedToken);
}
void SendRegistrationToServer(string token)
{
//a post request to my server
}
}
}
感谢任何见解。
答
这总是发生在我身上,它是一种错误或者什么,我必须每次都进行清理和重建,这不会在发布时发生,您可以在每次运行应用程序的同时删除Firebase实例并获取新的令牌你正在调试。
,你可以这样写你的OnCreate
Task.Run(() => {
var instanceid = FirebaseInstanceId.Instance;
instanceid.DeleteInstanceId();
Log.Debug("TAG", "{0} {1}", instanceid.Token, instanceid.GetToken(this.GetString(Resource.String.gcm_defaultSenderId), Firebase.Messaging.FirebaseMessaging.InstanceIdScope)); });
嗨Sulaeman。如果没有看到任何代码片段(可能在'onTokenRefresh()'或至少是一个样本有效载荷),很难查明任何东西。另外,您能否指出我在文档中看到“*文档表示令牌将在6个月*“? –
my onTokenrefresh与此处的完全相同:https://developer.xamarin.com/guides/android/application_fundamentals/notifications/remote-notifications-with-fcm/#client_app 和我的文档 –
OnTokenRefresh很少被调用:它在以下情况下用于更新令牌: 当应用程序安装或卸载时 当用户删除应用程序数据时。 当应用程序擦除实例ID时 当令牌的安全性受到影响时。 根据Google的Instance ID文档,FCM实例ID服务会定期(通常每6个月)要求应用刷新其令牌。 这是根据:https://developer.xamarin.com/guides/android/application_fundamentals/notifications/remote-notifications-with-fcm/ –