Utt*_*ara 181 android firebase firebase-cloud-messaging
希望大家都知道这个类,用于在刷新通知令牌时刷新通知令牌,我们从这个类中获取刷新的令牌,来自以下方法.
@Override
public void onTokenRefresh() {
// Get updated InstanceID token.
String refreshedToken = FirebaseInstanceId.getInstance().getToken();
Log.d(TAG, "Refreshed token: " + refreshedToken);
}
Run Code Online (Sandbox Code Playgroud)
要使用它,我想实现FCM,我扩展了MyClass FirebaseInstanceIdService
但是,显示不推荐使用FirebaseInstanceIdService
有没有人知道这个?,我应该使用什么方法或类来代替这个来获取刷新令牌,因为这是不推荐使用的.
我正在使用 : implementation 'com.google.firebase:firebase-messaging:17.1.0'
我检查了相同的文件没有提到这个.:FCM设置文件
UPDATE
这个问题已被解决.
谷歌不赞成FirebaseInstanceService,
我问问题是找到方法,然后我知道我们可以从FirebaseMessagingService获取令牌,
和以前一样,当我问问题文档没有更新但是现在谷歌文档更新以获取更多信息时,请参阅此谷歌文档:FirebaseMessagingService
旧版自:FirebaseInstanceService(已弃用)
@Override
public void onTokenRefresh() {
// Get updated InstanceID token.
String refreshedToken = FirebaseInstanceId.getInstance().getToken();
Log.d(TAG, "Refreshed token: " + refreshedToken);
}
Run Code Online (Sandbox Code Playgroud)
新来自:FirebaseMessagingService
@Override
public void onNewToken(String s) {
super.onNewToken(s);
Log.d("NEW_TOKEN",s);
}
Run Code Online (Sandbox Code Playgroud)
谢谢.
Nil*_*hod 121
是的, FirebaseInstanceIdService已弃用
来自DOCS: - 此类已弃用.赞成
overriding onNewToken在FirebaseMessagingService.一旦实施,就可以安全地删除此服务.
无需使用FirebaseInstanceIdService服务来获取FCM令牌您可以安全地删除FirebaseInstanceIdService服务
现在,我们需要@Override onNewToken 得到Token的FirebaseMessagingService
示例代码
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());
}
}
Run Code Online (Sandbox Code Playgroud)
您需要
FirebaseMessagingService在此清单文件中注册
<service
android:name=".MyFirebaseMessagingService"
android:stopWithTask="false">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
Run Code Online (Sandbox Code Playgroud)
.getToken();如果您需要在活动中获取令牌而不是使用,也会弃用getInstanceId ()
现在我们需要使用getInstanceId ()生成令牌
getInstanceId ()返回ID此Firebase项目的自动生成的令牌.
如果实例ID尚不存在,则会生成该实例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);
}
});
Run Code Online (Sandbox Code Playgroud)
这是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())
}
}
Run Code Online (Sandbox Code Playgroud)
Fra*_*len 103
这里有一个firebaser
查看参考文档FirebaseInstanceIdService:
这个类已被弃用.
赞成压倒一切的
onNewToken在FirebaseMessagingService.一旦实施,就可以安全地删除此服务.
奇怪的是,JavaDoc for FirebaseMessagingService还没有提到这个onNewToken方法.看起来并非所有更新的文档都尚未发布.我已经提交了一个内部问题来获取已发布的参考文档的更新,并且还可以更新指南中的示例.
与此同时,旧的/已弃用的呼叫和新呼叫都应该有效.如果您遇到任何一个问题,请发布代码,我会看看.
Ale*_*nov 11
还有这个:
FirebaseInstanceId.getInstance().getInstanceId().getResult().getToken()
Run Code Online (Sandbox Code Playgroud)
假设是弃用的解决方案:
FirebaseInstanceId.getInstance().getToken()
Run Code Online (Sandbox Code Playgroud)
编辑
FirebaseInstanceId.getInstance().getInstanceId().getResult().getToken()
如果任务尚未完成,则会产生异常,因此Nilesh Rathod描述的方法(带.addOnSuccessListener)是正确的方法.
科特林:
FirebaseInstanceId.getInstance().instanceId.addOnSuccessListener(this) { instanceIdResult ->
val newToken = instanceIdResult.token
Log.e("newToken", newToken)
}
Run Code Online (Sandbox Code Playgroud)
nir*_*997 11
只需调用此方法即可获取 Firebase 消息传递令牌
public void getFirebaseMessagingToken ( ) {
FirebaseMessaging.getInstance ().getToken ()
.addOnCompleteListener ( task -> {
if (!task.isSuccessful ()) {
//Could not get FirebaseMessagingToken
return;
}
if (null != task.getResult ()) {
//Got FirebaseMessagingToken
String firebaseMessagingToken = Objects.requireNonNull ( task.getResult () );
//Use firebaseMessagingToken further
}
} );
}
Run Code Online (Sandbox Code Playgroud)
在 build.gradle 文件中添加此依赖项后,上述代码运行良好
implementation 'com.google.firebase:firebase-messaging:21.1.0'
Run Code Online (Sandbox Code Playgroud)
注意:这是对上述依赖项所做的代码修改,以解决弃用问题。(截至 2021 年 5 月 9 日的工作代码)
FirebaseinstanceIdService已弃用。所以必须使用“FirebaseMessagingService”
海图请:
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);
}
}
Run Code Online (Sandbox Code Playgroud)
Kotlin 允许使用比其他答案中显示的代码更简单的代码。
要在刷新时获取新令牌:
class MyFirebaseMessagingService: FirebaseMessagingService() {
override fun onNewToken(token: String?) {
Log.d("FMS_TOKEN", token)
}
...
}
Run Code Online (Sandbox Code Playgroud)
在运行时从任何地方获取令牌:
FirebaseInstanceId.getInstance().instanceId.addOnSuccessListener {
Log.d("FMS_TOKEN", it.token)
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
73772 次 |
| 最近记录: |