简体   繁体   English

如何获得firebase令牌?

[英]how to get firebase token?

I didn't program android for a while and i see the firebase messaging service got deprecated. 我有一段时间没有编程android了,我发现Firebase消息传递服务已被弃用。 I implemented the new FirebaseMessagingService class and i have overridden the onNewToken in order to get the token, but the method is not even being called, as the documentation said this method should be ran automaticlly. 我实现了新的FirebaseMessagingService类,并且为了获取令牌而重写了onNewToken,但是该方法甚至没有被调用,因为文档说该方法应该自动运行。 but now i got confused, why the token is not generated? 但是现在我很困惑,为什么不生成令牌? here is what i implemented 这是我实现的

public class FireBsaeTest extends FirebaseMessagingService {
    public FireBsaeTest(){
    }
    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {

        if (remoteMessage.getData().size() > 0) {

            } else {

            }

        }


        if (remoteMessage.getNotification() != null) {
        }

    }
    @Override
    public void onNewToken(String token) {
        System.out.println(token);

    }}

and here is the manifest 这是清单

 <service android:name=".FireBsaeTest">
            <intent-filter>
                <action android:name="com.google.firebase.MESSAGING_EVENT" />
            </intent-filter>
        </service>

did i miss something ? 我错过了什么 ?

UPDATE UPDATE

i tried to call it manually from my activity it thorw new Illegal exception firebaseNotInilized 我尝试从我的活动中手动调用它,以阻止新的非法异常firebaseNotInilized

 FirebaseApp.initializeApp(this);
        FirebaseInstanceId.getInstance().getInstanceId().addOnSuccessListener(new OnSuccessListener<InstanceIdResult>() {
            @Override
            public void onSuccess(InstanceIdResult instanceIdResult) {
                System.out.println(instanceIdResult.getToken());
            }
        });

MyFirebaseMessagingService MyFirebaseMessagingService

 public class MyFirebaseMessagingService extends FirebaseMessagingService {
        private static final String TAG = MyFirebaseMessagingService.class.getSimpleName();


        @Override
        public void onNewToken(String s) {
            super.onNewToken(s);
            Log.i(TAG, "onNewToken:    >>>  " + s);
        }
     @Override
        public void onMessageReceived(RemoteMessage remoteMessage) {
           }
    }

MyFirebaseInstanceIDService.java MyFirebaseInstanceIDService.java

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

            // Notify UI that registration has completed, so the progress indicator can be hidden.

        }
    }

Manifest.xml 的Manifest.xml

    <service   android:name=".services.MyFirebaseMessagingService"
 android:exported="false"
  android:stopWithTask="false">
    <intent-filter>
       <action android:name="com.google.firebase.MESSAGING_EVENT"/>
                </intent-filter>
            </service>
            <service
                    android:name=".services.MyFirebaseInstanceIDService"
                    android:exported="false">
                <intent-filter>
                    <action android:name="com.google.firebase.INSTANCE_ID_EVENT"/>
                </intent-filter>
            </service>

Gradle 摇篮

implementation 'com.google.firebase:firebase-core:16.0.6'
implementation 'com.google.firebase:firebase-messaging:17.3.4'

I just knew where is the problem, the problem was the implementation of root build.gradle file with class-path, android studio configring the dependecies like the following 我只是知道问题出在哪里,问题build.gradle使用class-path实施root build.gradle文件,android studio配置如下依赖项

dependencies {
        classpath 'com.android.tools.build:gradle:3.3.0'
        classpath 'com.google.gms:google-services:4.1.0'

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }

google-services should be configured classpath 'com.google.gms:google-services:4.0.1' once i changed the class path it works and now im able to get the token 一旦我更改了有效的类路径,并且现在无法获取令牌,则应将google-services配置为classpath 'com.google.gms:google-services:4.0.1'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM