我正在我的应用程序中实现第三方服务(FreshChat,该服务可让您在应用程序或网站中集成服务台),并且一切正常,除了聊天的推送通知。 Freshchat通过Firebase工作(firebase推送通知不会出现问题),但是freshat通知不起作用。我遵循了Freshchat的官方文档,但是其中有不推荐使用的方法,并且肯定不起作用(我尝试了很多)
这是Firebase Cloud Messaging代码:
public class FireBase extends FirebaseMessagingService {
@Override
public void onMessageReceived(RemoteMessage message) {
super.onMessageReceived(message);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, "channel_id")
.setContentTitle(message.getNotification().getTitle())
.setContentText(message.getNotification().getBody())
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
.setStyle(new NotificationCompat.BigTextStyle())
.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
.setSmallIcon(R.mipmap.ic_launcher)
.setAutoCancel(true);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, notificationBuilder.build());
}
以及FreshChat文档的摘录:
7.1通过FCM连接Freshchat
如果您尚未集成FCM,请按照此处的说明进行操作。完成FCM集成步骤后,请按照以下两个步骤连接Freshchat和FCM
第1步。发送注册令牌在应用的FirebaseInstanceIdService的实现中,按以下步骤将令牌发送到Freshchat
Java
@Override
public void onTokenRefresh() {
String token = FirebaseInstanceId.getInstance().getToken();
Freshchat.getInstance(this).setPushRegistrationToken(token);
}
第2步:处理FCM消息在应用的FirebaseMessagingService实现中,如果是Freshchat通知,则将RemoteMessage对象传递给Freshchat。
Java
public void onMessageReceived(RemoteMessage remoteMessage) {
if (Freshchat.isFreshchatNotification(remoteMessage)) {
Freshchat.getInstance(this).handleFcmMessage(context, remoteMessage);
} else {
//Handle notifications with data payload for your app
}
}
我尝试通过文档进行操作,但是根本不起作用,您能帮我吗?先感谢您! 这里也是完整的FreshChat文档:https://support.freshchat.com/support/solutions/articles/229319-freshchat-android-sdk-integration-steps
答案 0 :(得分:1)
尝试将其添加到您的默认活动中,它对我有用。
FirebaseInstanceId.getInstance().getInstanceId()
.addOnCompleteListener(new OnCompleteListener<InstanceIdResult>() {
@Override
public void onComplete(@NonNull Task<InstanceIdResult> task) {
if (!task.isSuccessful()) {
Log.e(TAG, "getInstanceId failed", task.getException());
return;
}
// Get new Instance ID token
String token = task.getResult().getToken();
Freshchat.getInstance(this).setPushRegistrationToken(token);
Log.e("Token", token.toString());
}
});
希望这对您有所帮助。编码愉快!