ProNoob
ProNoob

Reputation: 47

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>

Upvotes: 1

Views: 669

Answers (2)

Ali Faris
Ali Faris

Reputation: 18592

you need to override onMessageReceived in MyFirebaseMessagingService class to do some actions when notification is pushed.

doc

By overriding the method FirebaseMessagingService.onMessageReceived, you can perform actions based on the received RemoteMessage object and get the message data

@Override
public void onMessageReceived(RemoteMessage remoteMessage) 
{

    Log.d(TAG, "From: " + "Notification Received");


    // TODO(developer): Handle FCM messages here.
    Log.d(TAG, "From: " + remoteMessage.getFrom());

    // Check if message contains a data payload.
    if (remoteMessage.getData().size() > 0) 
    {
        Log.d(TAG, "Message data payload: " + remoteMessage.getData());
    }

    // Check if message contains a notification payload.
    if (remoteMessage.getNotification() != null) {
        Log.d(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody());
    }

    // Also if you intend on generating your own notifications as a result of a received FCM
    // message, here is where that should be initiated. See sendNotification method below.

    //You may want show notification
}

keep in mind onMessageReceived will only triggered if the app in foreground

When your app is in the background, Android directs notification messages to the system tray. A user tap on the notification opens the app launcher by default.

Upvotes: 0

Upendra Joshi
Upendra Joshi

Reputation: 611

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

Upvotes: 1

Related Questions