Firebase FCM令牌未生成 [英] Firebase fcm token not generating

查看:74
本文介绍了Firebase FCM令牌未生成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想集成fcm以在我的应用程序中具有推送通知功能.但是问题是我的fcm令牌根本没有生成.我在该项目中使用了与我在新项目中使用的相同的代码,该代码没有任何问题.

I want to integrate fcm for having push notification functionality in my app. But the problem is my fcm token is not getting generated at all. I used the same code I used in this project into a new project and it worked without any issue.

但是在这个项目中,我尝试了所有尝试,但都没有结果.

But with this project I have tried everything to no result at all.

我尝试使用不推荐使用的onTokenRefresh()方法和新的onNewToken()方法,但是没有一个对我有用.

I tried with both the deprecated onTokenRefresh() method as well as the new onNewToken() method but none of them is working for me.

下面是相同的代码.

    public class CustomFirebaseMessagingService extends FirebaseMessagingService {
    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        super.onMessageReceived(remoteMessage);
        // TODO(developer): Handle FCM messages here.

        Log.d("Firebase", "From: " + remoteMessage.getFrom());

        // Check if message contains a data payload.
        if (remoteMessage.getData().size() > 0) {
            Log.d("Firebase", "Message data payload: " + remoteMessage.getData());

        }

        // Check if message contains a notification payload.
        if (remoteMessage.getNotification() != null) {
            Log.d("Firebase", "Message Notification Body: " + remoteMessage.getNotification().getBody());
        }

        // Also if you intend on generating your own notifications as a result of a received FCM
        // message, here is where that should be initiated. See sendNotification method below.

    }

    @Override
    public void onNewToken(String s) {
        super.onNewToken(s);
        Log.d("token",s);
    }
}

这是带有onTokenRefresh()方法的代码

And this is the code with onTokenRefresh() method

    public class MyFirebaseInstanceIdService extends FirebaseInstanceIdService {
    @Override
    public void onTokenRefresh() {
        super.onTokenRefresh();
        String refreshedToken = FirebaseInstanceId.getInstance().getToken();
        Log.d("Firebase", "Refreshed token: " + refreshedToken);
    }
}

我也在清单中声明了这两种服务.

I have declared both services in the manifest too.

<service android:name=".notification.CustomFirebaseMessagingService"
        android:stopWithTask="false">
        <intent-filter>
            <action android:name="com.google.firebase.MESSAGING_EVENT" />
        </intent-filter>
    </service>
    <service
        android:name=".notification.MyFirebaseInstanceIdService">
        <intent-filter>
            <action android:name="com.google.firebase.INSTANCE_ID_EVENT"/>
        </intent-filter>
    </service>

我尝试过的事情

  1. 在控制台上删除项目,然后再次创建
  2. 将整个项目复制到新文件夹,然后在Android Studio中再次打开
  3. 尝试了以上两种服务
  4. 尝试调用FirebaseInstanceId.getToken(),但这会给出空指针异常.
  1. Deleted the project on console and creating again
  2. Copying whole project to a new folder and open again in Android Studio
  3. Tried with both of the above service
  4. Tried calling FirebaseInstanceId.getToken() but this gives null pointer exception.

推荐答案

最终解决了我的问题.当我尝试集成OneSignal通知sdk时得到了这个提示.问题是清单的应用程序标记中包含以下代码.

Finally got the solution to my problem. I got this hint when I was trying to integrate OneSignal notification sdk. The problem was that I had the below code in the application tag of manifest.

tools:node="replace"

这是用OneSignal文档编写的.

This was written in OneSignal docs.

请确保您未将AndroidManifest.xml中的标签替换为tools:node ="replace"

Make sure you are not replacing the tag in your AndroidManifest.xml with tools:node="replace"

由于OneSignal也在内部使用FireBase,因此我想直接尝试使用Firebase,并且在删除它后可以正常工作.

As OneSignal was also internally using FireBase I thought to give it a try with the firebase directly and it worked after I removed it.

希望这对其他人也有帮助

Hope this will help someone else too

这篇关于Firebase FCM令牌未生成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆