FirebaseInstanceIdService is deprecated
希望大家都知道这个类,用于在刷新Firebase通知令牌时获取通知令牌,我们从此类中从以下方法获取刷新的令牌。
1 2 3 4 5 6 | @Override public void onTokenRefresh() { // Get updated InstanceID token. String refreshedToken = FirebaseInstanceId.getInstance().getToken(); Log.d(TAG,"Refreshed token:" + refreshedToken); } |
要在实现FCM时使用它,我从
但是,表明FirebaseInstanceIdService已过时
有人知道吗?
我不建议使用什么方法或类代替它来获取刷新的令牌。
我正在使用:
我检查了文档是否相同,对此没有提及。 :FCM设置文件
更新
这个问题已被解决。
由于Google不推荐使用
我问了这个问题以找到方法,我知道我们可以从FirebaseMessagingService获取令牌,
和以前一样,当我问问题文档未更新但现在Google文档已更新以便获取更多信息时,请参阅此google文档:FirebaseMessagingService
OLD从:FirebaseInstanceService(已弃用)
1 2 3 4 5 6 | @Override public void onTokenRefresh() { // Get updated InstanceID token. String refreshedToken = FirebaseInstanceId.getInstance().getToken(); Log.d(TAG,"Refreshed token:" + refreshedToken); } |
来自:FirebaseMessagingService
1 2 3 4 5 | @Override public void onNewToken(String s) { super.onNewToken(s); Log.d("NEW_TOKEN",s); } |
谢谢。
是
FROM DOCS :- This class was deprecated.
In favour ofoverriding onNewToken inFirebaseMessagingService . Once that has been implemented, this service can be safely removed.
无需使用
现在我们需要
样本代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 | public class MyFirebaseMessagingService extends FirebaseMessagingService { @Override public void onNewToken(String s) { Log.e("NEW_TOKEN", s); } @Override public void onMessageReceived(RemoteMessage remoteMessage) { Map<String, String> params = 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()); } } |
编辑
You need to register your
FirebaseMessagingService in manifest file like this
1 2 3 4 5 6 7 8 | <service android:name=".MyFirebaseMessagingService" android:stopWithTask="false"> <intent-filter> </intent-filter> </service> |
如何在活动中获取令牌
.getToken(); is also deprecated
if you need to get token in your activity than UsegetInstanceId ()
现在我们需要使用
如果实例ID不存在,则会生成一个实例ID,并开始定期向Firebase后端发送信息。
退货
-
您可以通过包含
ID 和Token 的InstanceIdResult 查看结果的任务。
样本代码
1 2 3 4 5 6 7 8 | FirebaseInstanceId.getInstance().getInstanceId().addOnSuccessListener( MyActivity.this, new OnSuccessListener<InstanceIdResult>() { @Override public void onSuccess(InstanceIdResult instanceIdResult) { String newToken = instanceIdResult.getToken(); Log.e("newToken",newToken); } }); |
编辑2
这是kotlin的工作代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 | 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()) } } |
在这里放火
查看参考文档中的
This class was deprecated.
In favour of overriding
onNewToken inFirebaseMessagingService . Once that has been implemented, this service can be safely removed.
奇怪的是,
同时,旧的/已弃用的呼叫和新的呼叫均应工作。如果您遇到任何麻烦,请发布代码,然后看看。
还有这个:
1 | FirebaseInstanceId.getInstance().getInstanceId().getResult().getToken() |
假设是不建议使用的解决方案:
1 | FirebaseInstanceId.getInstance().getToken() |
编辑
如果任务尚未完成,则可能会产生异常,因此,女巫Nilesh Rathod(使用
科特林:
1 2 3 4 | FirebaseInstanceId.getInstance().instanceId.addOnSuccessListener(this) { instanceIdResult -> val newToken = instanceIdResult.token Log.e("newToken", newToken) } |
Kotlin所允许的代码甚至比其他答案中显示的代码更简单。
要在刷新时获取新令牌:
1 2 3 4 5 6 7 | class MyFirebaseMessagingService: FirebaseMessagingService() { override fun onNewToken(token: String?) { Log.d("FMS_TOKEN", token) } ... } |
要在运行时从任何地方获取令牌:
1 2 3 | FirebaseInstanceId.getInstance().instanceId.addOnSuccessListener { Log.d("FMS_TOKEN", it.token) } |
因此必须使用" FirebaseMessagingService"
海图请:
1 2 3 4 5 6 7 8 9 10 11 12 13 | public class MyFirebaseMessagingService extends FirebaseMessagingService { @Override public void onNewToken(String s) { super.onNewToken(s); Log.e("NEW_TOKEN",s); } @Override public void onMessageReceived(RemoteMessage remoteMessage) { super.onMessageReceived(remoteMessage); } } |
在KOTLIN中:-如果要将令牌保存到数据库或共享首选项中,请在FirebaseMessagingService中覆盖onNewToken
1 2 3 | override fun onNewToken(token: String?) { super.onNewToken(token) } |
在运行时获取令牌,使用
1 2 3 4 5 | FirebaseInstanceId.getInstance().instanceId .addOnSuccessListener(this@SplashActivity) { instanceIdResult -> val mToken = instanceIdResult.token println("printing fcm token: $mToken") } |
FCM实施类别:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | public class MyFirebaseMessagingService extends FirebaseMessagingService { @Override public void onMessageReceived(RemoteMessage remoteMessage) { Map<String, String> data = remoteMessage.getData(); if(data != null) { // Do something with Token } } } // FirebaseInstanceId.getInstance().getToken(); @Override public void onNewToken(String token) { super.onNewToken(token); if (!token.isEmpty()) { Log.e("NEW_TOKEN",token); } } } |
并在Activity或APP中调用其初始化:
1 2 3 4 5 6 7 8 9 | FirebaseInstanceId.getInstance().getInstanceId().addOnSuccessListener( instanceIdResult -> { String newToken = instanceIdResult.getToken(); }).addOnFailureListener(new OnFailureListener() { @Override public void onFailure(@NonNull Exception e) { Log.i("FireBaseToken","onFailure :" + e.toString()); } }); |
AndroidManifest.xml:
1 2 3 4 5 6 | <service android:name="ir.hamplus.MyFirebaseMessagingService" android:stopWithTask="false"> <intent-filter> </intent-filter> </service> |
**如果您添加了" INSTANCE_ID_EVENT",则不要忘记禁用它。