Es una aplicación que recibe notificación cuando otra aplicación escribe en la base de datos de Firebase. El error:
Unable to instantiate service firebase.MyFirebaseMessagingService:
java.lang.ClassNotFoundException: Didn't find class "firebase.MyFirebaseMessagingService" on path: DexPathList[[zip file…..
El manifest:
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".activities.MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service
android:name="firebase.MyFirebaseMessagingService">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT"/>
</intent-filter>
</service>
<service
android:name="firebase.MyFirebaseIdService">
<intent-filter>
<action android:name="com.google.firebase.INSTANCE_ID_EVENT"/>
</intent-filter>
</service>
</application>
El index.js
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp();
exports.respuesta = functions.database.ref('/pedido/{pedidoId}').onCreate((snap, context) => {
const createdData = snap.val(); // data that was created
var topic="rotiseria";
console.log(“”+context.params.id);
console.log(""+idPedido);
var payload = {
data: {
idPedido: 'pedido1',
remitente: 'Raquel Mansilla'
},
topic: topic
};
admin.messaging().send(payload)
.then((response) => {
// Response is a message ID string.
console.log('Successfully sent message:', response);
})
.catch((error) => {
console.log('Error sending message:', error);
});
return null;
});
Y la clase MyFirebaseMessagingService:
public class MyFirebaseMessagingService extends FirebaseMessagingService {
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
// Check if message contains a data payload.
if (remoteMessage.getData().size() > 0) {
Log.d("Message data payload: ", ""+remoteMessage.getData());
}
// Check if message contains a notification payload.
if (remoteMessage.getNotification() != null) {
Log.d("Message Notification Body: ","" + remoteMessage.getData());
}
showNotification(remoteMessage.getData().get("idPedido"),remoteMessage.getData().get("remitente"));
}
@Override
public void onMessageSent(String s) {
super.onMessageSent(s);
}
private void showNotification(String idPedido,String remitente) {
String titulo = (idPedido == null || idPedido.isEmpty()) ? "Notificación importante" : idPedido;
Intent notIntent = new Intent(getApplicationContext(), PedidosFragment.class);
notIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
Log.d("Notificacion",idPedido);
PendingIntent contIntent = PendingIntent.getActivity(getApplicationContext(), 0, notIntent, 0);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setContentIntent(contIntent)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle(titulo)
.setContentText(remitente)
.setAutoCancel(true);
notificationBuilder.setContentIntent(contIntent);
Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
notificationBuilder.setSound(alarmSound);
NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, notificationBuilder.build());
}
}
Bueno, saludos!