Mit Cloud Messaging Service starten

Sokdante

Mitglied
Guten Tag,
meine App hat eine Main Activity und zwei Service's, eine, die die Message empfängt und eine die ausgeführt werden soll, wenn der FirebaseMessagingService diese Message empfängt..

Die Nachrichten kommen an, wenn die App geschlossen ist und der Service wird auch gestartet. Allerdings nur dann, wenn die App geöffnet ist, also im Vordergrund.
Im Background passiert, (ausgenommen die ankommende Nachricht), leider nichts.

Woran kann das liegen?

Hier gibts einmal den Firebase Service Code:



Code:
import...

import com.google.firebase.messaging.FirebaseMessagingService;

import com.google.firebase.messaging.RemoteMessage;







public class MyFirebaseService extends FirebaseMessagingService {







   // private static final String TAG = "MyFirebaseMsgService";

    public void onMessageReceived(RemoteMessage remoteMessage) {







        try {

            startService(new Intent(this, MyService.class));

        }

        catch (Exception e) {

            e.printStackTrace();

        }



        // ...



        // TODO(developer): Handle FCM messages here.

        // Not getting messages here? See why this may be: https://goo.gl/39bRNJ

        Log.d(ContentValues.TAG, "From: " + remoteMessage.getFrom());



        // Check if message contains a data payload.

        if (remoteMessage.getData().size() > 0) {

            Log.d(ContentValues.TAG, "Message data payload: " + remoteMessage.getData());



            if (/* Check if data needs to be processed by long running job */ true) {

                // For long-running tasks (10 seconds or more) use Firebase Job Dispatcher.

                scheduleJob();



            } else {



                handleNow();

            }



        }



        // Check if message contains a notification payload.

        if (remoteMessage.getNotification() != null) {

            Log.d(ContentValues.TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody());





        }

        // Also if you intend on generating your own notifications as a result of a received FCM

        // message, here is where that sh



    }



    private void handleNow() {

    }



    private void scheduleJob() {

    }





    /**

     * Called if InstanceID token is updated. This may occur if the security of

     * the previous token had been compromised. Note that this is called when the InstanceID token

     * is initially generated so this is where you would retrieve the token.

     */

    @Override

    public void onNewToken(String token) {

        Log.d(ContentValues.TAG, "Refreshed token: " + token);



        // If you want to send messages to this application instance or

        // manage this apps subscriptions on the server side, send the

        // Instance ID token to your app server.

        sendRegistrationToServer(token);

    }



    private void sendRegistrationToServer(String token) {



    }



}
 

krgewb

Top Contributor
Mit Syntax-Highlighting und weniger Leerzeilen:
Java:
import...
import com.google.firebase.messaging.FirebaseMessagingService;
import com.google.firebase.messaging.RemoteMessage;

public class MyFirebaseService extends FirebaseMessagingService {
    // private static final String TAG = "MyFirebaseMsgService";
    public void onMessageReceived(RemoteMessage remoteMessage) {
        try {
            startService(new Intent(this, MyService.class));
        }
        catch (Exception e) {
            e.printStackTrace();
        }
        // ...
        // TODO(developer): Handle FCM messages here.
        // Not getting messages here? See why this may be: https://goo.gl/39bRNJ
        Log.d(ContentValues.TAG, "From: " + remoteMessage.getFrom());
        // Check if message contains a data payload.
        if (remoteMessage.getData().size() > 0) {
            Log.d(ContentValues.TAG, "Message data payload: " + remoteMessage.getData());
            if (/* Check if data needs to be processed by long running job */ true) {
                // For long-running tasks (10 seconds or more) use Firebase Job Dispatcher.
                scheduleJob();
            } else {
                handleNow();
            }
        }
        // Check if message contains a notification payload.
        if (remoteMessage.getNotification() != null) {
            Log.d(ContentValues.TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody());
        }
        // Also if you intend on generating your own notifications as a result of a received FCM
        // message, here is where that sh
    }

    private void handleNow() {

    }

    private void scheduleJob() {

    }

    /**
     * Called if InstanceID token is updated. This may occur if the security of
     * the previous token had been compromised. Note that this is called when the InstanceID token
     * is initially generated so this is where you would retrieve the token.
     */
    @Override
    public void onNewToken(String token) {
        Log.d(ContentValues.TAG, "Refreshed token: " + token);
        // If you want to send messages to this application instance or
        // manage this apps subscriptions on the server side, send the
        // Instance ID token to your app server.
        sendRegistrationToServer(token);
    }

    private void sendRegistrationToServer(String token) {

    }

}
 
K

kneitzel

Gast
Was genau sendest Du denn an Nachricht. Du solltest dir das Firebase Cloud Messaging im Detail ansehen und die Seiten genau durchlesen.

Ich vermute, dass du eine Nachricht sendest die entweder eine reine Notification ist oder eine Notification mit Data Part. Wenn Du aber möchtest, das die App die Nachricht immer verarbeitet, dann darf die Nachricht keine Notification enthalten.

Das Verhalten unter Android findest du unter:
https://firebase.google.com/docs/cloud-messaging/android/receive (Handling Messages hat eine Tabelle)
Aber Du solltest generell halt alles ab https://firebase.google.com/docs/cloud-messaging lesen - da ist die Introduction und es folgt noch deutlich mehr ....
 

Ähnliche Java Themen

Neue Themen


Oben