【发布时间】:2023-03-03 07:03:18
【问题描述】:
我对 Android Dev't 比较陌生并且遇到了问题。当某些数据保存到我的 firebase 实时数据库时,我尝试实现 firebase 功能以向我的 android 应用程序发送通知。但是在保存数据并执行功能后,它在日志中说它已成功发送,但我的android测试设备上没有收到任何弹出通知。可能是什么问题呢?下面是我的代码。
我的 JS 代码。
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp();
exports.sendNotification = functions.database.ref('/Lecture_Materials/{MIS}/{MISId}/name')
.onWrite(( change,context) =>{
// Grab the current value of what was written to the Realtime Database.
var eventSnapshot = change.after.val();
var str1 = "Lecture material uploaded is: " + eventSnapshot.name;
console.log(eventSnapshot);
var topic = "Management.Information.System";
var payload = {
data: {
name: str1,
}
};
// Send a message to devices subscribed to the provided topic.
return admin.messaging().sendToTopic(topic, payload)
.then(function (response) {
// See the MessagingTopicResponse reference documentation for the
// contents of response.
console.log("Successfully sent message:", response);
return;
})
.catch(function (error) {
console.log("Error sending message:", error);
});
});
我的 OnMessage 已收到
package com.dreamlazerstudios.gtuconline;
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 com.google.firebase.messaging.FirebaseMessagingService;
import com.google.firebase.messaging.RemoteMessage;
public class MyFirebaseMessagingService extends FirebaseMessagingService {
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
// Check if message contains a data payload.
if (remoteMessage.getData().size() > 0) {
showNotification(remoteMessage.getData().get("name"));
}
// Check if message contains a notification payload.
if (remoteMessage.getNotification() != null) {
}
}
private void showNotification(String name) {
Intent intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
PendingIntent.FLAG_ONE_SHOT);
Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setContentTitle("Lecture note uploaded is: " + name)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentText("Lecture Notes")
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
}
}
非常感谢任何帮助。
【问题讨论】:
-
你在 JS 中发送到一个主题,但不要在 Android 中收听该主题。因此,您应该收听并订阅您发送的主题。 firebase.google.com/docs/cloud-messaging/android/…
-
任何代码 sn-p 可以帮助我吗?
-
我该怎么做??
-
您可以在我发送的链接中找到。
-
很难理解
标签: java android firebase firebase-realtime-database google-cloud-functions