Я новичок в использовании уведомления firebase. Я уже могу подписаться на topi c и сгенерировать токен с помощью fcm.
то, что я делаю сейчас, пытается получить заголовок и тело, но я не знаю, работает ли моя MyFirebaseMessagingService Activity. Я следил за документацией, но все еще не могу зарегистрировать тело и заголовок.
вот мой код:
MyFirebaseMessagingService. java
//class extending FirebaseMessagingService
public class MyFirebaseMessagingService extends FirebaseMessagingService {
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);
//if the message contains data payload
//It is a map of custom keyvalues
//we can read it easily
if(remoteMessage.getData().size() > 0){
//handle the data message here
}
Log.d("am_i_here","here_na");
//getting the title and the body
String title = remoteMessage.getNotification().getTitle();
String body = remoteMessage.getNotification().getBody();
Log.d("title_notification",title);
Log.d("body_notification",body);
//then here we can use the title and body to build a notification
}
}
MyNotificationManager. java
public class MyNotificationManager {
private Context mCtx;
private static MyNotificationManager mInstance;
private MyNotificationManager(Context context) {
mCtx = context;
}
public static synchronized MyNotificationManager getInstance(Context context) {
if (mInstance == null) {
mInstance = new MyNotificationManager(context);
}
return mInstance;
}
public void displayNotification(String title, String body) {
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(mCtx, Constant.CHANNEL_ID)
.setSmallIcon(R.drawable.logo)
.setContentTitle(title)
.setContentText(body);
Intent resultIntent = new Intent(mCtx, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(mCtx, 0, resultIntent, PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.setContentIntent(pendingIntent);
NotificationManager mNotifyMgr =
(NotificationManager) mCtx.getSystemService(NOTIFICATION_SERVICE);
if (mNotifyMgr != null) {
mNotifyMgr.notify(1, mBuilder.build());
}
}
}
MainActivity. java
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
/*
* If the device is having android oreo we will create a notification channel
* */
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
NotificationManager mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
int importance = NotificationManager.IMPORTANCE_HIGH;
NotificationChannel mChannel = new NotificationChannel(Constant.CHANNEL_ID, Constant.CHANNEL_NAME, importance);
mChannel.setDescription(Constant.CHANNEL_DESCRIPTION);
mChannel.enableLights(true);
mChannel.setLightColor(Color.RED);
mChannel.enableVibration(true);
mChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
mNotificationManager.createNotificationChannel(mChannel);
}
/*
* Displaying a notification locally
*/
MyNotificationManager.getInstance(this).displayNotification("Greetings", "Hello how are you?");
AndroidManifest
<service
android:name="MyFirebaseMessagingService"
android:exported="false"
android:directBootAware="true">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
я уже могу получить уведомление, но когда я нажимаю на уведомление, заголовок и Тело по-прежнему не отображается в журнале. Что я делаю не так, может ли кто-нибудь мне помочь? Мне действительно нужна помощь, это действительно меня напрягало и я пытался понять свою ошибку. Большое спасибо за помощь.