2017-08-14 1 views
0

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

} 

Répondre

0

Dans votre service onMessageReceived() appeler la méthode

if(remoteMessage.getNotification()!=null){ 
     Log.d(TAG,"Message body:"+remoteMessage.getNotification().getBody()); 
     sendNotification(remoteMessage.getNotification().getBody()); 
     sendMessageToActivity(); 

    } 

et ajoutez cela comme une méthode

private static void sendMessageToActivity(String msg) { 
    Intent intent = new Intent("Notification"); 
    // You can also include some extra data. 
    intent.putExtra("Status", msg); 
    LocalBroadcastManager.getInstance(context).sendBroadcast(intent); 
} 

Dans l'activité que vous souhaitez recevoir

private BroadcastReceiver mMessageReceiver = new BroadcastReceiver() { 
    @Override 
    public void onReceive(Context context, Intent intent) { 
     // Get extra data included in the Intent 
     String message = intent.getStringExtra("Status"); 
     //do what you want with the notification 

    } 
}; 
LocalBroadcastManager.getInstance(getActivity()).registerReceiver(
      mMessageReceiver, new IntentFilter("Notification")); 
+0

J'ai mis à jour mon code. Pourriez-vous l'éditer à partir de mon code? –

+0

Oui, je viens de faire l'édition –

+0

LocalBroadcastManager.getInstance (context) .sendBroadcast (intention); d'où vient le contexte? –