0 votos

La notificación de inserción de dispositivo a dispositivo no funciona correctamente (tema suscrito a Firebase)

Actualmente estoy tratando de crear una app con la que puedes enviar una notificación de inserción a otro dispositivo.

Me han desplegado las funciones a firebase (el nodeJS index.js archivo) y siempre que puedo crear una notificación, se envía con el tema de "notificaciones" en la base avanzada. La secuencia de comandos se activa como puedo ver en los registros. Así que todo va bien.

El problema que me estoy quedando en el hecho de que cuando me suscribo a este tema y enviar una notificación desde el otro dispositivo es muy inestable. A veces me recibirlo inmediatamente, y muchas veces se necesita un tiempo muy largo (unos 5 minutos) para recibir.

Aquí está mi actual archivo Java que deben manejar las notificaciones entrantes:

package nl.c99.notificationtestingapp;

import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.media.RingtoneManager;
import android.net.Uri;
import android.support.v4.app.NotificationCompat;
import android.util.Log;

import com.google.firebase.messaging.FirebaseMessagingService;
import com.google.firebase.messaging.RemoteMessage;

public class FCMService extends FirebaseMessagingService {
    private static final String TAG = "MyFirebaseMsgService";

    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {

        /* There are two types of messages data messages and notification messages. Data messages are handled here in onMessageReceived whether the app is in the foreground or background. Data messages are the type traditionally used with GCM. Notification messages are only received here in onMessageReceived when the app is in the foreground. When the app is in the background an automatically generated notification is displayed. */
        String notificationTitle = null, notificationBody = null;
        String dataTitle = null, dataMessage = null;

        // Check if message contains a data payload.
        if (remoteMessage.getData().size() > 0) {
            Log.d(TAG, "Message data payload: " + remoteMessage.getData().get("message"));
            dataTitle = remoteMessage.getData().get("title");
            dataMessage = remoteMessage.getData().get("message");
        }

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

        // Also if you intend on generating your own notifications as a result of a received FCM
        // message, here is where that should be initiated. See sendNotification method below.
        sendNotification(notificationTitle, notificationBody, dataTitle, dataMessage);
    }
}

¿Qué estoy haciendo mal, es el problema de mi lado?

0voto

Peter Centro Puntos 1

Acabo de resolver este problema configurando timeToLive en el archivo index.js en 0. Este es mi archivo actual ahora. Funciona impecable.

 // imports firebase-functions module
const functions = require('firebase-functions');
// imports firebase-admin module
const admin = require('firebase-admin');

admin.initializeApp(functions.config().firebase);

/* Listens for new messages added to /messages/:pushId and sends a notification to subscribed users */
exports.pushNotification = functions.database.ref('/messages/{pushId}').onWrite((change,context) => {
console.log('Push notification event triggered');
/* Grab the current value of what was written to the Realtime Database */

    var valueObject = change.after.val();
/* Create a notification and data payload. They contain the notification information, and message to be sent respectively */ 
    const payload = {
        notification: {
            title: 'App Name',
            body: "New message",
            sound: "default"
        },
        data: {
            title: valueObject.title,
            message: valueObject.message
        }
    };
    console.log('MESSAGE HAS BEEN RECEIVED: ' + valueObject.title + " -> " + valueObject.message);
/* Create an options object that contains the time to live for the notification and the priority. */
//https://stackoverflow.com/questions/40590282/fcm-push-with-high-priority-to-topic-comes-with-delay
    const options = {
        priority: "high",
        timeToLive: 0
    };

return admin.messaging().sendToTopic("notifications", payload, options);
});```
 

PreguntAndroid.com

PreguntAndroid es una comunidad de usuarios de Android en la que puedes resolver tus problemas y dudas.
Puedes consultar las preguntas de otros usuarios, hacer tus propias preguntas o resolver las de los demás.

Powered by:

X