Ceci est du code de service de messagerie firebase:J'ai reçu une notification de Firebase mais comment puis-je afficher ces messages de notification et les transférer dans ma page principale?
public class MyFirebaseMessagingService extends FirebaseMessagingService{
private static final String TAG="MyFirebaseMsgService";
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
Bundle bundle = new Bundle();
bundle.putString("msgBody", remoteMessage.getNotification().getBody());
Intent new_intent = new Intent();
new_intent.setAction("ACTION_STRING_ACTIVITY");
new_intent.putExtra("msg", bundle);
sendBroadcast(new_intent);
Log.d(TAG,"FROM:"+remoteMessage.getFrom());
//check if the message contains data
if(remoteMessage.getData().size()>0){
Log.d(TAG,"Message data:"+remoteMessage.getData());
//check if the message contains notification
if(remoteMessage.getNotification()!=null){
Log.d(TAG,"Message body:"+remoteMessage.getNotification().getBody());
sendNotification(remoteMessage.getNotification().getBody());
}
}
}
/* * notification d'affichage Ceci est le code pour afficher une notification Ceci est le code pour afficher une notification Ceci est le code pour afficher une notification * */
private void sendNotification(String body) {
Intent intent=new Intent(this,MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent=PendingIntent.getActivity(this,0/*request code*/,intent,PendingIntent.FLAG_ONE_SHOT);
//set sound notification
Uri notifictaionSound= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notifiBuilder=new
NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("Firebase Cloud Messaging")
.setContentText(body)
.setAutoCancel(true)
.setSound(notifictaionSound)
.setContentIntent(pendingIntent);
NotificationManager notificationManager=
(NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0/*ID of
notification*/,notifiBuilder.build());
}
J'ai mis à jour mon code. Pourriez-vous l'éditer à partir de mon code? –
Oui, je viens de faire l'édition –
LocalBroadcastManager.getInstance (context) .sendBroadcast (intention); d'où vient le contexte? –