FirebaseInstanceId.getInstance().getToken() 已经过时。
FirebaseInstanceId.getInstance().getToken() 已经过时。
希望大家都知道这个类,它用于在Firebase通知token被刷新时获取通知token,我们可以从以下方法中获得刷新的token。
@Override public void onTokenRefresh() { // Get updated InstanceID token. String refreshedToken = FirebaseInstanceId.getInstance().getToken(); Log.d(TAG, "Refreshed token: " + refreshedToken); }
为了使用它,我将MyClass从FirebaseInstanceIdService扩展。
但是,它显示FirebaseInstanceIdService过时了。
有人知道吗? 我该使用哪种方法或类来获取刷新后的token,因为这已经过时了。
我使用:implementation \'com.google.firebase:firebase-messaging:17.1.0\'
我检查了相关的文档,没有提到这一点。FCM设置文档
更新
此问题已解决。
由于Google已经废弃了FirebaseInstanceService,
我问了个问题,找到了方法,我们可以从FirebaseMessagingService中获得Token,
以前,当我问问题时,文档没有更新,但现在Google文档已经更新,有关更多信息,请参阅此Google文档:FirebaseMessagingService
旧的:FirebaseInstanceService(已弃用)
@Override public void onTokenRefresh() { // Get updated InstanceID token. String refreshedToken = FirebaseInstanceId.getInstance().getToken(); Log.d(TAG, "Refreshed token: " + refreshedToken); }
新的:FirebaseMessagingService
@Override public void onNewToken(String s) { super.onNewToken(s); Log.d("NEW_TOKEN",s); }
火基著在此。
请检查FirebaseInstanceIdService的参考文档:
该类已过时。
建议使用
FirebaseMessagingService
中的onNewToken
进行重写。一旦实现了这一点,该服务可以安全地删除。
奇怪的是,FirebaseMessagingService
的JavaDoc尚未提及onNewToken
方法。看起来并非所有更新的文档都已经发布。我已经提交了一个内部问题,以便将参考文档的更新发布出来,并更新指南中的示例。
与此同时,旧的/弃用调用和新的调用都应该有效。如果你遇到任何问题,请发表代码,我将帮忙解决。
更新时间:2020年11月12日
现在 FirebaseInstanceId
也被弃用了。
现在我们需要使用 FirebaseMessaging.getInstance().token
。
示例代码:
FirebaseMessaging.getInstance().token.addOnCompleteListener { if(it.isComplete){ firebaseToken = it.result.toString() Util.printLog(firebaseToken) } }
是的,FirebaseInstanceIdService
已被弃用。
来自文档:-该类已被弃用。
建议在FirebaseMessagingService
中覆盖onNewToken
。完成后,可以安全地删除该服务。
无需使用 FirebaseInstanceIdService
服务来获取 FCM 令牌,您可以安全地移除 FirebaseInstanceIdService
服务。
现在我们需要在 FirebaseMessagingService
中 @Override onNewToken
来获取 Token
。
示例代码:
public class MyFirebaseMessagingService extends FirebaseMessagingService { @Override public void onNewToken(String s) { Log.e("NEW_TOKEN", s); } @Override public void onMessageReceived(RemoteMessage remoteMessage) { Mapparams = remoteMessage.getData(); JSONObject object = new JSONObject(params); Log.e("JSON_OBJECT", object.toString()); String NOTIFICATION_CHANNEL_ID = "Nilesh_channel"; long pattern[] = {0, 1000, 500, 1000}; NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, "Your Notifications", NotificationManager.IMPORTANCE_HIGH); notificationChannel.setDescription(""); notificationChannel.enableLights(true); notificationChannel.setLightColor(Color.RED); notificationChannel.setVibrationPattern(pattern); notificationChannel.enableVibration(true); mNotificationManager.createNotificationChannel(notificationChannel); } // to diaplay notification in DND Mode if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { NotificationChannel channel = mNotificationManager.getNotificationChannel(NOTIFICATION_CHANNEL_ID); channel.canBypassDnd(); } NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID); notificationBuilder.setAutoCancel(true) .setColor(ContextCompat.getColor(this, R.color.colorAccent)) .setContentTitle(getString(R.string.app_name)) .setContentText(remoteMessage.getNotification().getBody()) .setDefaults(Notification.DEFAULT_ALL) .setWhen(System.currentTimeMillis()) .setSmallIcon(R.drawable.ic_launcher_background) .setAutoCancel(true); mNotificationManager.notify(1000, notificationBuilder.build()); } }
#编辑
您需要在清单文件中注册您的
FirebaseMessagingService
,如下所示
#如何在您的活动中获取令牌
.getToken();
也已被弃用。
如果您需要在活动中获取令牌,则使用getInstanceId ()
。
现在我们需要使用 getInstanceId ()
来生成令牌。
getInstanceId ()
返回该 Firebase
项目的 ID
和自动生成的令牌。
这会生成 Instance ID(如果尚不存在),该 ID 开始周期性地向 Firebase 后端发送信息。
返回值:
- 一个任务,您可以通过其中持有的
InstanceIdResult
(包含ID
和token
)来查看结果。
示例代码:
FirebaseInstanceId.getInstance().getInstanceId().addOnSuccessListener( MyActivity.this, new OnSuccessListener() { @Override public void onSuccess(InstanceIdResult instanceIdResult) { String newToken = instanceIdResult.getToken(); Log.e("newToken",newToken); } });
##编辑2
以下是 Kotlin 的可用代码
class MyFirebaseMessagingService : FirebaseMessagingService() { override fun onNewToken(p0: String?) { } override fun onMessageReceived(remoteMessage: RemoteMessage?) { val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager val NOTIFICATION_CHANNEL_ID = "Nilesh_channel" if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { val notificationChannel = NotificationChannel(NOTIFICATION_CHANNEL_ID, "Your Notifications", NotificationManager.IMPORTANCE_HIGH) notificationChannel.description = "Description" notificationChannel.enableLights(true) notificationChannel.lightColor = Color.RED notificationChannel.vibrationPattern = longArrayOf(0, 1000, 500, 1000) notificationChannel.enableVibration(true) notificationManager.createNotificationChannel(notificationChannel) } // to diaplay notification in DND Mode if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { val channel = notificationManager.getNotificationChannel(NOTIFICATION_CHANNEL_ID) channel.canBypassDnd() } val notificationBuilder = NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID) notificationBuilder.setAutoCancel(true) .setColor(ContextCompat.getColor(this, R.color.colorAccent)) .setContentTitle(getString(R.string.app_name)) .setContentText(remoteMessage!!.getNotification()!!.getBody()) .setDefaults(Notification.DEFAULT_ALL) .setWhen(System.currentTimeMillis()) .setSmallIcon(R.drawable.ic_launcher_background) .setAutoCancel(true) notificationManager.notify(1000, notificationBuilder.build()) } }