设备未收到FCM通知

时间:2019-02-01 09:03:01

标签: android android-fragments firebase-cloud-messaging android-notifications firebase-notifications

我正在获取令牌并通过Firebase Notification Composer发送通知。它已成功发送,但在onMessageReceived方法中未收到。我不知道这里是什么问题。我做了firebase控制台中提到的所有步骤。请帮助我。 我的清单就是这样

    <uses-permission android:name="android.permission.INTERNET" />
                    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
                    <uses-permission android:name="android.permission.GET_ACCOUNTS" />
                    <uses-permission android:name="ANDROID.PERMISSION.RECORD_AUDIO" />
                    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
                    <uses-permission android:name="android.permission.GET_TASKS" />
                    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
                    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
                    <!-- <uses-permission android:name="android.permission.SET_ALARM" /> -->
                    <!-- <uses-feature android:name="android.hardware.Camera1" /> -->
                    <uses-permission android:name="android.permission.CAMERA" />
            <uses-feature
                    android:name="android.hardware.camera"
                    android:required="true" />

                <uses-permission android:name="android.permission.WAKE_LOCK" />
                <uses-permission android:name="android.permission.VIBRATE" />
                <!--<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />-->
                <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
                <uses-permission android:name="android.permission.WRITE_SETTINGS" />
                <!--<uses-permission android:name="${applicationId}.permission.C2D_MESSAGE" />-->
                <uses-permission android:name="android.permission.READ_PHONE_STATE" />
                <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
                <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
                <uses-permission android:name="android.permission.READ_INTERNAL_STORAGE" />
          <uses-feature
                android:glEsVersion="0x00020000"
                android:required="true" />

            <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
            <!--
             The following two permissions are not required to use
             Google Maps Android API v2, but are recommended.
            -->
            <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
            <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

            <permission
                android:name="${applicationId}.permission.MAPS_RECEIVE"
                android:protectionLevel="signature" />

            <uses-permission android:name="${applicationId}.permission.MAPS_RECEIVE" />
            <application
                android:name=".activities.KaROAppController"
                android:allowBackup="true"
                android:hardwareAccelerated="false"
                android:icon="@mipmap/karo_logo"
                android:label="@string/app_name"
                android:largeHeap="true"
                android:supportsRtl="true"
                android:theme="@style/AppTheme"
                tools:node="replace">
     <service android:name=".services.KaROFirebaseMessagingService">
                <intent-filter>
                    <action android:name="com.google.firebase.MESSAGING_EVENT" />
                </intent-filter>
            </service>
            <service android:name=".services.KaROFirebaseInstanceIDService">
                <intent-filter>
                    <action android:name="com.google.firebase.INSTANCE_ID_EVENT" />
                </intent-filter>
            </service>
  <meta-data
        android:name="com.google.firebase.messaging.default_notification_icon"
            android:resource="@drawable/karo_splash" />
  <meta-data
            android:name="com.google.firebase.messaging.default_notification_icon"
            android:resource="@drawable/karo_splash" />

KaROFirebaseInstanceIDService为

public class KaROFirebaseInstanceIDService extends FirebaseInstanceIdService {
    private static final String TAG = "MyFirebaseIIDService";

    /**
     * Called if InstanceID token is updated. This may occur if the security of
     * the previous token had been compromised. Note that this is called when the InstanceID token
     * is initially generated so this is where you would retrieve the token.
     */
    // [START refresh_token]
    @Override
    public void onTokenRefresh() {
        // Get updated InstanceID token.
        String refreshedToken = FirebaseInstanceId.getInstance().getToken();
//        KaHOAppController.setLoggedInUserDeviceID(refreshedToken);
        Log.d(TAG, "Refreshed token: " + refreshedToken);

        // If you want to send messages to this application instance or
        // manage this apps subscriptions on the server side, send the
        // Instance ID token to your app server.
        sendRegistrationToServer(refreshedToken);
    }
    // [END refresh_token]

    /**
     * Persist token to third-party servers.
     * <p>
     * Modify this method to associate the user's FCM InstanceID token with any server-side account
     * maintained by your application.
     *
     * @param token The new token.
     */
    private void sendRegistrationToServer(String token) {
        // TODO: Implement this method to send token to your app server.
    }
}

KaROFirebaseMessagingService为

public class KaROFirebaseMessagingService extends FirebaseMessagingService {
    private static final String TAG = "FCM Service";
    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        // TODO: Handle FCM messages here.
        // If the application is in the foreground handle both data and notification messages here.
        // Also if you intend on generating your own notifications as a result of a received FCM
        // message, here is where that should be initiated.
        Log.d(TAG, "From: " + remoteMessage.getFrom());
        Log.d(TAG, "Notification Message Body: " + remoteMessage.getNotification().getBody());
    }
}

1 个答案:

答案 0 :(得分:0)

对我来说,问题是我没有指定调试证书的SHA-1哈希。 docs中没有明确提及。

您可以按以下方式获取调试证书的哈希值:

keytool -list -v \
-alias androiddebugkey -keystore ~/.android/debug.keystore

找到更多相关信息,例如如何在Authenticating Your Client

获取发布证书的哈希值