Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/217.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/40.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
Android 通知未出现在应用程序的后台状态和已终止状态_Android_Node.js_Firebase_Firebase Cloud Messaging_Google Cloud Functions - Fatal编程技术网

Android 通知未出现在应用程序的后台状态和已终止状态

Android 通知未出现在应用程序的后台状态和已终止状态,android,node.js,firebase,firebase-cloud-messaging,google-cloud-functions,Android,Node.js,Firebase,Firebase Cloud Messaging,Google Cloud Functions,我正在尝试在上传帖子时向应用程序的最终用户推送通知。当应用程序位于前台时,它可以正常工作,但当应用程序位于后台或被杀死时,它不会显示。当应用程序被终止或在后台运行时,是否有任何方式显示通知。 下面是我正在使用的node.js代码 const functions = require('firebase-functions'); let admin = require('firebase-admin'); admin.initializeApp(functions.config().firebase

我正在尝试在上传帖子时向应用程序的最终用户推送通知。当应用程序位于前台时,它可以正常工作,但当应用程序位于后台或被杀死时,它不会显示。当应用程序被终止或在后台运行时,是否有任何方式显示通知。 下面是我正在使用的node.js代码

const functions = require('firebase-functions');
let admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);
exports.sendPush = functions.database.ref('/promos').onWrite(event => {
var topic = "deals_notification";
let projectStateChanged = false;
let projectCreated = false;
let projectData = event.data.val();

if (((event.data.numChildren())-(event.data.previous.numChildren()))>0) {
    let msg="notification arrived"
  let payload = {
        notification: {
            title: 'Firebase Notification',
            body: msg,
            sound: 'default',
            badge: '1'
        }
    };

admin.messaging().sendToTopic(topic, payload).then(function(response) {
// See the MessagingTopicResponse reference documentation for the
// contents of response.
console.log("Successfully sent message:", response);
}).catch(function(error) {
console.log("Error sending message:", error);
});
}
});
下面是MyFirebaseMessageService:

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import com.google.firebase.messaging.FirebaseMessagingService;
import com.google.firebase.messaging.RemoteMessage;
public class MyFirebaseMessageService extends FirebaseMessagingService {
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    super.onMessageReceived(remoteMessage);
    Log.e("notification---",remoteMessage.getNotification().getBody());

    sendnotification(remoteMessage.getNotification().getBody());
}
private void sendnotification(String body){
    Intent intent = new Intent(this, MainActivity.class);
// use System.currentTimeMillis() to have a unique ID for the pending intent
    PendingIntent pIntent = PendingIntent.getActivity(this, (int) 
System.currentTimeMillis(), intent, 0);

// build notification
// the addAction re-use the same intent to keep the example short
    Notification.Builder n  = new Notification.Builder(this)
            .setContentTitle("Best Deals")
            .setContentText(body)
            .setSmallIcon(R.mipmap.ic_launcher)
            .setContentIntent(pIntent)
            .setAutoCancel(true);

    NotificationManager manager = (NotificationManager) 
    getSystemService(Context.NOTIFICATION_SERVICE);
    manager.notify(0, n.build());
}
}

谢谢。

如果问题是只有当应用程序位于前台时才会调用
onMessageReceived()
,而当应用程序位于后台时才会显示通知,但不会调用您的方法。。。而不是正常工作。请参阅文档:

当您希望FCM处理显示文件时,请使用通知消息 代表您的客户端应用程序发出通知。使用数据消息时 要处理客户端应用程序上的消息

请在此处阅读更多信息:

您正在发送通知消息。相反,您应该发送一条数据消息


如果问题不同:当应用程序处于后台时,不会发生任何事情,则可能是您的设备出现问题。请参见

我确信代码是完美的。但是您正在测试的设备可能有问题。请尝试使用其他设备测试代码。

请参阅我的答案