简体   繁体   中英

FCM Notification not received on device

I am getting token and sending notification through firebase Notification composer. It was sent successfully, but it is not received in onMessageReceived method.I dont know what is the problem here.I did every steps which is mentioned in firebase console.Please help me. My manifest is like this

    <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 is

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 is

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());
    }
}

The problem for me was that I didn't specify the SHA-1 hash of my debug certificate. It's not explicitly mentioned in the docs .

You can get the hash for your debug certificate as follows:

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

Find more about it, eg how to get the hash for your release certificate at Authenticating Your Client

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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