FirebaseInstanceIdService已经过时。
FirebaseInstanceIdService已经过时。
希望大家都知道这个类,它用于在 Firebase 通知令牌刷新时获取通知令牌,我们可以从以下方法中获取刷新的令牌。
@Override public void onTokenRefresh() { // Get updated InstanceID token. String refreshedToken = FirebaseInstanceId.getInstance().getToken(); Log.d(TAG, "Refreshed token: " + refreshedToken); }
为了使用它,我将 MyClass 从 FirebaseInstanceIdService
中扩展了出来。
但是,它显示FirebaseInstanceIdService已过时。
有人知道吗?我应该使用哪个方法或类来代替它以获取刷新的令牌,因为它已经过时了。
我正在使用:implementation \'com.google.firebase:firebase-messaging:17.1.0\'
我已经检查了相同的文档,但是没有提及这个问题:FCM设置文档
更新
此问题已经得到解决。
由于谷歌已经废弃了 FirebaseInstanceService
,
我提出了这个问题以寻找解决办法,我知道我们可以从 FirebaseMessagingService 中获取令牌。
以前,当我提出这个问题时,文档没有更新,但是现在谷歌文档已经更新,所以请参考谷歌文档以获取更多信息: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); }
(原文直接翻译)
这里是Firebase团队
请查看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
服务
现在我们需要@Override onNewToken
在FirebaseMessagingService
中获取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
和自动生成的令牌。如果它还不存在,这将生成一个实例ID,该实例开始周期性地向Firebase后端发送信息。
返回值
- 任务,您可以通过其中持有
ID
和token
的InstanceIdResult
查看结果。示例代码
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()) } }