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

Notifications not appearing in background state and killed state of an app

我正在尝试在上传 post 时向应用程序的最终用户推送通知。当应用程序处于前台时它工作正常但当应用程序处于后台或被杀死时不显示。当应用程序被杀死或在后台 运行 时,有什么方法可以显示通知。 这是我正在使用的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() 仅在应用程序处于前台时被调用,而当它处于后台时会显示通知但未调用您的方法...那么它工作正常。请参阅文档:

Use notification messages when you want FCM to handle displaying a notification on your client app's behalf. Use data messages when you want to process the messages on your client app.

在此处阅读更多内容:https://firebase.google.com/docs/cloud-messaging/concept-options#notifications_and_data_messages

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

如果问题不同:当应用程序在后台时没有任何反应,这可能是您设备的问题。参见 Push notifications using FCM not received when app is killed android

我确信代码是完美的。但是您使用的设备可能存在一些问题 testing.Please 尝试使用其他设备测试代码。