First do the Firebase Imports in build.Gradle (Module: app), assuming that you are already using firebase in your project, because 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 activitys declaration).
<!--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 the FCM (Firebase Cloud Messaging)
Fcminstanceidservice.java: to generate the device’s unique token. This write token, is to write to my own server, in case you do 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
}
});
}
}
Now you need the Fcmservice.java class: you are 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 Zoom menu -> Cloud Messaging
Fill in the data and send it and be happy.
OBS.:
Put a breakpoint in the Fcminstanceidservice class and make sure it generated a hash, this is your device’s unique id (it only happens the first time you install the App).
Before sending, place a breakpoint in the Fcmservice class, as this is where the message will arrive.
vc has to generate the token by overriding the method
onTokenRefresh
, save this to your server and when sending the notification use this token– Eduardo Dornel