Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/182.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 通知没有';firebase函数启动后,不会弹出_Java_Android_Firebase_Firebase Realtime Database_Google Cloud Functions - Fatal编程技术网

Java 通知没有';firebase函数启动后,不会弹出

Java 通知没有';firebase函数启动后,不会弹出,java,android,firebase,firebase-realtime-database,google-cloud-functions,Java,Android,Firebase,Firebase Realtime Database,Google Cloud Functions,我对安卓开发相对来说比较陌生,有一个问题。当某些数据保存到firebase实时数据库时,我尝试实现firebase函数向android应用程序发送通知。但在保存数据并执行函数后,它在日志中说它已成功发送,但在我的android测试设备上没有收到任何弹出通知。有什么问题吗?下面是我的代码 我的JS代码 const functions = require('firebase-functions'); const admin = require('firebase-admin'); admin.in

我对安卓开发相对来说比较陌生,有一个问题。当某些数据保存到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);
    });
    });
我的电子邮件收到了

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云消息传递(也可以在Android Studio中使用Firebase助手工具,您可以在工具>Firebase中找到它):

对于服务器端,要发送通知,可以遵循以下指南:

此外,您还可以使用一些第三方库,如:


你在JS中发送一个主题,但在Android中不要收听该主题。所以你应该收听并订阅你发送的主题。有什么代码片段可以帮助我吗?我该怎么做?你可以在我发送的链接中找到。很难理解我使用的方法应该是有效的。文件有点含糊不清。