firebase函数被触发后没有弹出通知

时间:2018-06-08 08:24:14

标签: java android firebase firebase-realtime-database google-cloud-functions

我对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);
    });
    });

我的OnMessageReceived

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());
}
}

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

我认为您正在尝试发送推送通知,因此您需要为firebase云消息传递做一些必要的实施。同样在服务器端,您需要推送通知密钥,以便您的应用程序发送通知。

您可以查看以下文档,了解如何在客户端实施Firebase云消息传递(您也可以在Android Studio中使用Firebase助手工具,您可以在工具&gt; Firebase 中找到它):< / p>

https://firebase.google.com/docs/cloud-messaging/android/client

对于服务器端,要发送通知,您可以按照以下指南进行操作:

https://firebase.google.com/docs/cloud-messaging/admin/send-messages

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

https://www.npmjs.com/package/fcm-push