- mongodb - 在 MongoDB mapreduce 中,如何展平值对象?
- javascript - 对象传播与 Object.assign
- html - 输入类型 ="submit"Vs 按钮标签它们可以互换吗?
- sql - 使用 MongoDB 而不是 MS SQL Server 的优缺点
希望大家都知道这个类,用于在firebase通知 token 刷新时获取通知 token ,我们从这个类获取刷新的 token ,通过以下方法。
@Override
public void onTokenRefresh() {
// Get updated InstanceID token.
String refreshedToken = FirebaseInstanceId.getInstance().getToken();
Log.d(TAG, "Refreshed token: " + refreshedToken);
}
为了使用它来实现 FCM,我从 FirebaseInstanceIdService
但是,显示 FirebaseInstanceIdService 已弃用
有人知道吗?我应该使用什么方法或类来代替它来获取刷新的 token ,因为它已被弃用。
我正在使用:implementation 'com.google.firebase:firebase-messaging:17.1.0'
我检查了文件是否相同,没有提到这一点。 : FCM SETUP DOCUMENT
更新
此问题已修复。
由于 Google 弃用了 FirebaseInstanceService
,
我问这个问题是为了找到方法,我知道我们可以从 FirebaseMessagingService 获取 Token,
和以前一样,当我询问问题文档未更新但现在 Google 文档已更新因此有关更多信息时,请参阅此 google 文档:FirebaseMessagingService
OLD 来自: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);
}
最佳答案
2020 年 11 月 12 日更新
现在 FirebaseInstanceId
也被弃用了
现在我们需要使用 FirebaseMessaging.getInstance().token
示例代码
FirebaseMessaging.getInstance().token.addOnCompleteListener {
if(it.isComplete){
firebaseToken = it.result.toString()
Util.printLog(firebaseToken)
}
}
Yes FirebaseInstanceIdService
is deprecated
FROM DOCS :- This class was deprecated.In favour of
overriding onNewToken
inFirebaseMessagingService
. Once that has been implemented, this service can be safely removed.
无需使用 FirebaseInstanceIdService
服务来获取 FCM token 您可以安全地移除 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) {
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
<service
android:name=".MyFirebaseMessagingService"
android:stopWithTask="false">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
#如何在您的 Activity 中获取 token
.getToken();
is also deprecatedif you need to get token in your activity than UsegetInstanceId ()
现在我们需要使用 getInstanceId ()
生成 token
getInstanceId ()
返回此 Firebase
项目的 ID
和自动生成的 token 。
如果实例 ID 尚不存在,则会生成一个实例 ID,并开始定期向 Firebase 后端发送信息。
返回
InstanceIdResult
查看结果,其中包含 ID
和 token
。示例代码
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 的工作代码
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())
}
}
关于android - FirebaseInstanceIdService 已弃用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51123197/
我是一名优秀的程序员,十分优秀!