无法获取FCM注册令牌

问题描述 投票:0回答:2

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

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

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

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.
   }
}

AndroidManifest.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());

    }

}
java android firebase firebase-cloud-messaging
2个回答
0
投票

试试这个:

    // [START refresh_token]
    @Override
    public void onTokenRefresh() {
        // Get updated InstanceID token.
        String refreshedToken = FirebaseInstanceId.getInstance().getToken();
        Log.d(TAG, "Refreshed token: " + refreshedToken);
        Toast.makeText(this, "Refreshed token: " + refreshedToken, Toast.LENGTH_LONG).show();

        // Implement this method to send token to your app's server
       sendRegistrationToServer(refreshedToken);

    }
// [END refresh_token]

还有一件事:

如果要从服务器发送推送通知,则需要调用sendRegistrationToServer()方法,该方法将更新服务器上的令牌。

注意:

在以下情况下生成新的Firebase令牌(onTokenRefresh()被调用):

  • 该应用程序删除实例ID
  • 该应用程序将在新设备上恢复
  • 用户卸载/重新安装应用程序
  • 用户清除应用数据。

希望这可以帮助!祝好运!


0
投票

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

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

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

© www.soinside.com 2019 - 2024. All rights reserved.