I can not notify Push via firebase

0

I'm trying to create an application that notifies through firebase and from what I researched I got to a part that I no longer want help to see if I'm doing it right and to correct my mistake. follow my Codes

  

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>

    <meta-data
        android:name="com.google.firebase.messaging.default_notification_icon"
        android:resource="@drawable/ic_novis" />
    <!-- Set color used with incoming notification messages. This is used when no color is set for the incoming
         notification message. See README(*LINK*) for more. -->
    <meta-data
        android:name="com.google.firebase.messaging.default_notification_color"
        android:resource="@color/colorAccent" />

</application>
  

MyFirebaseMessagingService

package imperiogamerplay.deepcooee;

import com.google.firebase.iid.FirebaseInstanceIdReceiver;

public class MyFirebaseMessagingService extends FirebaseInstanceIdReceiver { }

  

MyFirebaseInstanceIDService

package imperiogamerplay.deepcooee;

import com.google.firebase.messaging.FirebaseMessagingService;

public class MyFirebaseInstanceIDService extends FirebaseMessagingService { }

    
asked by anonymous 15.12.2018 / 17:48

1 answer

2

First, do the Firebase imports in build.grad (Module: app), assuming you are already using firebase in your project, as there are other basic steps needed to work (those that firebase teaches you to configure when you create the project in the console):

implementation 'com.google.firebase:firebase-messaging:12.0.1'

You will need these following parameters in your manifest within the application tag (along with the activity statement).

    <!--notificação -->
    <service
        android:name=".FCM.FCMInstanceIdService">
        <intent-filter>
            <action android:name="com.google.firebase.INSTANCE_ID_EVENT"/>
        </intent-filter>
    </service>
    <service
        android:name=".FCM.FCMService" >
        <intent-filter>
            <action android:name="com.google.firebase.MESSAGING_EVENT"/>
        </intent-filter>
    </service>

Next you will need to create the classes for Firebase Cloud Messaging (FCM)

FCMInstanceIdService.java: to generate the unique token of the device. This record token, is to write to my own server, if not use delete this class):

/**
 * Criado por Leonardo Figueiredo em 20/08/2018.
 */
public class FCMInstanceIdService extends FirebaseInstanceIdService {

    public static String TAG = "FirebaseLog";
    String refreshedToken;

    @Override
    public void onTokenRefresh() {
        super.onTokenRefresh();
        refreshedToken = FirebaseInstanceId.getInstance().getToken();
        gravaToken();
        Log.d(TAG, "Token: " + refreshedToken);
    }

    public void gravaToken() {
        TokenModel tokenModel = new TokenModel();
        tokenModel.setTokenDevice(refreshedToken);
        tokenModel.setSo("Android");

        Call<TokenModel> call = new RetrofitConfig().getApiCandidato().gravaToken(tokenModel.getTokenDevice(), tokenModel.getSo());
        call.enqueue(new Callback<TokenModel>()
        {
            @Override
            public void onResponse(Call<TokenModel> call, Response<TokenModel> response) {
                //sucesso
            }

            @Override
            public void onFailure(Call<TokenModel> call, Throwable t) {
                //falha
            }
        });
    }
}

You will now need the FCMService.java class: you will be responsible for receiving and handling the notification.

/**
 * Criado por Leonardo Figueiredo em 20/08/2018.
 */
public class FCMService extends FirebaseMessagingService {

    public static String TAG = "FirebaseLog";

    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        super.onMessageReceived(remoteMessage);

        Log.v(TAG, "recebeu mensagem do firebase");

        //verifica se recebeu alguma notificacao
        if (remoteMessage.getNotification() != null) {
            String mensagem = remoteMessage.getNotification().getBody();

            Intent intent = new Intent(getBaseContext(), PrincipalActivity.class);

            PendingIntent pendingIntent = PendingIntent.getActivity(getBaseContext(), 0, intent, 0);

            NotificationCompat.Builder builder = new NotificationCompat.Builder(getBaseContext())
                    .setContentTitle(String.valueOf(getResources().getString(R.string.app_name)))
                    .setSmallIcon(R.drawable.ic_notificacao)
                    .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_logo_color))
                    .setDefaults(Notification.DEFAULT_VIBRATE | Notification.DEFAULT_SOUND)
                    .setContentIntent(pendingIntent)
                    .setStyle(new NotificationCompat.BigTextStyle().bigText(mensagem))
                    .setContentText(mensagem)
                    .setAutoCancel(true);

            NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
            notificationManager.notify(99, builder.build());
            Log.d(TAG, "Menssagem: " + remoteMessage.getNotification().getBody());
        }
    }
}

Now just go to your firebase console, go to the menu Enlarge - > Cloud Messaging

Fill in the details and send and be happy.

NOTE: Put a breakpoint in the FCMInstanceIdService class and verify that it has generated a hash, this is the unique id of your device (it only happens the first time you install the App).

Before sending, place a breakpoint in the FCMService class because it is the message that will arrive in the class.

    
19.12.2018 / 15:41