0

Je veux envoyer une notification lorsque les utilisateurs connectés à Internet dès que possible et afficher la notification; Même mes applications ne fonctionnent pas en arrière-plan/avant-plan.comment puis-je envoyer une notification push comme facebook alors que mon application n'est pas en cours d'exécution (arrière-plan/premier plan)

notification dès maintenant montrer que lorsque mes applications est en cours d'exécution ...

<service android:name=".Services.MyFirebaseMessagingService"> 
     <intent-filter> 
      <action android:name="com.google.firebase.MESSAGING_EVENT" /> 
     </intent-filter> 
    </service> 
    <service android:name=".Services.MyFirebaseInstanceIDService"> 
     <intent-filter> 
      <action android:name="com.google.firebase.INSTANCE_ID_EVENT" /> 
     </intent-filter> 
    </service> 

    <activity android:name=".ReadNotification" /> 

ici est ma classe java

public class MyFirebaseMessagingService extends com.google.firebase.messaging.FirebaseMessagingService { 

    private static final String TAG = "Android News App"; 

    @Override 
    public void onMessageReceived(RemoteMessage remoteMessage) { 

     //It is optional 
     Log.e(TAG, "From: " + remoteMessage.getFrom()); 
     Log.e(TAG, "Notification Message Body: " + remoteMessage.getNotification().getBody()); 

     //Calling method to generate notification 
     sendNotification(remoteMessage.getNotification().getTitle(), remoteMessage.getNotification().getBody()); 
    } 

    //This method is only generating push notification 
    private void sendNotification(String title, String messageBody) { 
     Intent intent = new Intent(this, MainActivity.class); 
     intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
     PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 
       PendingIntent.FLAG_ONE_SHOT); 

     Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); 
     NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this) 
       .setSmallIcon(R.drawable.icon64) 
       .setContentTitle(title) 
       .setContentText(messageBody) 
       .setAutoCancel(true) 
       .setSound(defaultSoundUri) 
       .setContentIntent(pendingIntent); 

     NotificationManager notificationManager = 
       (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 

     notificationManager.notify(0, notificationBuilder.build()); 
    } 
} 
+1

double possible de [Comment gérer une notification lorsque l'application en arrière-plan dans Firebase] (https: //stackoverflow.com/questions/37711082/how-to-handle-notification-when-app-in-background-in-firebase) –

Répondre

0

Vous devez utiliser la charge utile sur mesure pour cela. par exemple

{ 
    "to" : "bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...", 
    "data" : { 
    "Nick" : "Mario", 
    "body" : "great match!", 
    "Room" : "PortugalVSDenmark" 
    }, 
} 

pour envoyer des données utiles voir here

aussi changer la méthode pour recevoir des données de charge utile

public void onMessageReceived(RemoteMessage remoteMessage) { 
     Log.e(TAG, "From: " + remoteMessage.getFrom()); 

     if (remoteMessage == null) 
      return; 

     // Check if message contains a notification payload. 
     if (remoteMessage.getNotification() != null) { 
      Log.e(TAG, "Notification Body: " + remoteMessage.getNotification().getBody()); 
      handleNotification(remoteMessage.getNotification().getBody()); 
     } 

     // Check if message contains a data payload. 
     if (remoteMessage.getData().size() > 0) { 
      Log.e(TAG, "Data Payload: " + remoteMessage.getData().toString()); 

      try { 
       JSONObject json = new JSONObject(remoteMessage.getData().toString()); 
       handleDataMessage(json); 
      } catch (Exception e) { 
       Log.e(TAG, "Exception: " + e.getMessage()); 
      } 
     } 
    }