java - 触发firebase功能后未弹出通知
问题描述
我对 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());
}
}
任何帮助都深表感谢。
解决方案
我认为您正在尝试发送推送通知,因此您需要为 firebase 云消息传递做一些必要的实现。同样在服务器端,您需要推送通知密钥才能让您的应用程序发送通知。
您可以查看以下文档以了解如何在客户端实现 Firebase Cloud Messaging(您也可以在 Android Studio 中使用 Firebase 助手工具,您可以在Tools>Firebase中找到它):
https://firebase.google.com/docs/cloud-messaging/android/client
对于服务器端,要发送通知,您可以按照以下指南进行操作:
https://firebase.google.com/docs/cloud-messaging/admin/send-messages
此外,您还可以使用一些 3rd 方库,例如:
推荐阅读
- python - Is there a way to enter a specific INTEGER (for example, 3) and have python print same amount of characters (like, entering 3 and printing "aaa" out)
- codeigniter - How to do a live user activity tracking function on Codeigniter?
- python - Python recursion didn't give correct result for "sum of left leaves"
- python - Plotly 折线图中的高低线
- javascript - 异步请求完成后Vuejs显示结果
- sql - How do I write this postgres query for a has-many relationship?
- python - Refer related model field in model layer
- reactjs - 更新后 Create-react-app 创建不带组件的 App.js
- firebase - 如何在具有两个条件的文档中查询 firebase 数据库?
- android-service - 如何在android系统中禁用iGnss