无法获取FCM注册令牌

时间:2018-03-03 18:07:15

标签: java android firebase firebase-cloud-messaging

我试图简单地获取FCM注册令牌或设备令牌,以便从我的服务器向我的应用程序发送推送通知。

事情是我试图像下面显示的代码那样得到它,但即使在卸载并重新安装应用程序后也没有任何反应。我已经确定我的代码管理器正在运行,并且已经在AndroidManifests.xml中设置了所需的所有服务。

有关信息,我已经测试了firebase网站上的推送通知,它的工作正常。但这不是我的目标。我不明白为什么我可以从Firebase发送通知但无法在我的Android Studio控制台上查看/获取FCM注册令牌。我在这里错过了什么吗我是这种事情的初学者,已经被这个问题困扰了大约一个星期,而我的智慧已经结束了。希望有人可以帮助我。提前感谢您的时间。

public class FirebaseIDService  extends FirebaseInstanceIdService {
    private static final String TAG = "MyFirebaseIdService";
    public String tok;

   @Override
   public void onTokenRefresh() {
    // Get updated InstanceID token.
       String refreshedToken = FirebaseInstanceId.getInstance().getToken();
       Log.d(TAG, "Refreshed token: " + refreshedToken);
       // Instance ID token to your app server.

       //sendRegistrationToServer(refreshedToken);
   }

   private void sendRegistrationToServer(String token) {
       // TODO: Implement this method to send token to your app server.
   }
}

AndroidManifests.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.asii_developer.asiisupport">

    <uses-permission android:name="android.permission.INTERNET" />

    <!-- To auto-complete the email text field in the login form with the user's emails -->
    <uses-permission android:name="android.permission.GET_ACCOUNTS" />
    <uses-permission android:name="android.permission.READ_PROFILE" />
    <uses-permission android:name="android.permission.READ_CONTACTS" />


    <application
        android:allowBackup="true"
        android:hardwareAccelerated="false"
        android:icon="@mipmap/planete"
        android:label="@string/app_name"
        android:largeHeap="true"
        android:roundIcon="@mipmap/planete"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">


        <!--
        <service
            android:name=".DeviceToken">
            <intent-filter>

            </intent-filter>
        </service>
        -->

        <meta-data
            android:name="com.google.firebase.messaging.default_notification_color"
            android:resource="@color/colorPrimary" />
        <meta-data
            android:name="com.google.android.gms.version"
            android:value="@integer/google_play_services_version" />
        <activity android:name=".PageBienvenueActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".PagedAccueilActivity" />
        <activity android:name=".FormActivity" />
        <activity android:name=".RecapActivity" />
        <activity android:name=".PageDeConnexion" />
        <activity android:name=".mot_de_passeActivity" />
        <activity
            android:name=".Liste_des_ticketsActivity"
            android:windowSoftInputMode="stateHidden|adjustPan" />
        <activity
            android:name=".ConversationActivity"
            android:windowSoftInputMode="stateHidden|adjustPan" />
        <activity android:name=".NotificationsActivity">
            <intent-filter>
                <action android:name="notifActivity" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>


        <service
            android:name=".MyFirebaseMessagingService"
            android:enabled="true"
            android:exported="false" >
            <intent-filter>

            <action android:name="com.google.Firebase.MESSAGING_EVENT"/>

            </intent-filter>
        </service>
        <service
            android:name=".FirebaseIDService"
            android:enabled="true"
            android:exported="false">
            <intent-filter>

            <action android:name="com.google.Firebase.INSTANCE_ID_EVENT"/>

        </intent-filter>
        </service>



    </application>

</manifest>

MyFireBaseMessagingService.xml

package com.example.asii_developer.asiisupport;

import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.support.v4.app.NotificationCompat;
import android.util.Log;

import com.google.firebase.messaging.FirebaseMessagingService;
import com.google.firebase.messaging.RemoteMessage;

public class MyFirebaseMessagingService extends FirebaseMessagingService {

    private static final String TAG = "FCM Service";

    @Override

    public void onMessageReceived(RemoteMessage remoteMessage) {

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

        Log.d(TAG, "Notification Message Body: " + remoteMessage.getNotification().getBody());

        Intent intent = new Intent(this, NotificationsActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 1,
                intent, PendingIntent.FLAG_ONE_SHOT);

        NotificationCompat.Builder notificationBuilder = (NotificationCompat.Builder) new
                NotificationCompat.Builder(this)
                .setSmallIcon(R.drawable.arrow)
                .setContentTitle("Message")
                .setContentText(remoteMessage.getNotification().getBody())
                .setAutoCancel(true)
                .setContentIntent(pendingIntent);

        NotificationManager notificationManager = (NotificationManager)
                getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.notify(1, notificationBuilder.build());

    }

}

1 个答案:

答案 0 :(得分:0)

您需要先致电FirebaseInstanceId.getToken()(可能在MainActivity或任何相应的初始活动中。

onTokenRefresh()仅在开头的后期触发当且仅当FirebaseInstanceId.getToken()的初始调用返回null时。如FCM docs中所述:

  

每当生成新令牌时都会触发onTokenRefreshcallback,因此在其上下文中调用getToken可确保您访问当前可用的注册令牌。如果尚未生成令牌,FirebaseInstanceID.getToken()将返回null。

相关问题