Search code examples
androidfirebasefirebase-notifications

Firebase Push Notifications does not receive


I'm trying firebase push notification. I did everything it said in the tutorial, but it isn't working.

FirebaseMessagingService:

  package com.example.firebasenf.firebasenf;

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

public class MyFirebaseMessagingService extends FirebaseMessagingService {
}

FirebaseInstanceIdService:

package com.example.firebasenf.firebasenf;

import com.google.firebase.iid.FirebaseInstanceIdService;

public class MyFirebaseInstanceIDService extends FirebaseInstanceIdService {
}

Manifest:

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

Solution

  • you need to add some methods to your code :

    FirebaseMessagingService:

        package com.example.firebasenf.firebasenf;
    
    import android.app.NotificationManager;
    import android.app.PendingIntent;
    import android.content.Context;
    import android.content.Intent;
    import android.support.v4.app.NotificationCompat;
    
    import com.google.firebase.messaging.FirebaseMessagingService;
    import com.google.firebase.messaging.RemoteMessage;
    
    public class MyFirebaseMessagingService extends FirebaseMessagingService {
    
        @Override
        public void onMessageReceived(RemoteMessage remoteMessage){
            Intent intent = new Intent(this,MainActivity.class);
            intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            PendingIntent pendingIntent = PendingIntent.getActivity(this,0,intent,PendingIntent.FLAG_ONE_SHOT);
            NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this);
            notificationBuilder.setContentTitle("Application Title");
            notificationBuilder.setContentText(remoteMessage.getNotification().getBody());
            notificationBuilder.setAutoCancel(true);
            notificationBuilder.setSmallIcon(R.mipmap.ic_launcher);
            notificationBuilder.setContentIntent(pendingIntent);
            NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
            notificationManager.notify(0,notificationBuilder.build());
        }
    }
    

    FirebaseInstanceIdService :

    package com.example.firebasenf.firebasenf;
    
    import android.util.Log;
    
    import com.google.firebase.iid.FirebaseInstanceId;
    import com.google.firebase.iid.FirebaseInstanceIdService;
    
    public class MyFirebaseInstanceIDService extends FirebaseInstanceIdService {
        private static final String REG_TOKEN = "REG_TOKEN";
    
        @Override
        public void onTokenRefresh(){
            String recent_token = FirebaseInstanceId.getInstance().getToken();
            Log.d(REG_TOKEN,recent_token);
        }
    }