[英]Receive an FCM data notification on Android phone when app is not running
I am sending pure FCM data notifications to an Android client which are end-to-end encrypted.我正在向端到端加密的 Android 客户端发送纯 FCM 数据通知。 The android app decrypts the data notification payload and creates a local notification from the encrypted data. android 应用程序解密数据通知有效负载并根据加密数据创建本地通知。 This works fine as long as the app is running in the foreground or in the background: the notification payload is received, decrypted and is even shown on the lock screen, but only as long as the app is running.只要应用程序在前台或后台运行,这就可以正常工作:接收、解密通知有效负载,甚至显示在锁定屏幕上,但前提是应用程序正在运行。 I also looked at the message priority and set this to "high" in my application server that sends to FCM, both in the AndroidConfig member of the notification message and in the data field, but to no avail.我还查看了消息优先级并将其在发送到 FCM 的应用程序服务器中设置为“高”,无论是在通知消息的 AndroidConfig 成员中还是在数据字段中,但都无济于事。 What is the correct way to send a data message to an android device where the corresponding app is currently not running?将数据消息发送到相应应用程序当前未运行的 android 设备的正确方法是什么? Is this possible at all?这可能吗?
My server code (in golang) looks something like this, it hopes the fragment makes sense:我的服务器代码(在 golang 中)看起来像这样,它希望片段有意义:
app, err := firebase.NewApp(context.Background(), nil)
if err != nil {
logger.Info("Error initializing app: %v\n", err)
os.Exit(-536870136) /// -536870136 = 0xE0000308 is ERR_INOTE_FCM_FAILED_APP_INITIALIZE
}
// Obtain a messaging.Client from the App.
ctx := context.Background()
client, err := app.Messaging(ctx)
if err != nil {
logger.Info("Error getting Messaging client: %v\n", err)
os.Exit(-536870137) /// -536870137 = 0xE0000307 is ERR_INOTE_FCM_FAILED_GET_MESSAGING_CLIENT
}
message := &messaging.Message{
Data: map[string]string{
"NotificationType": "Push",
"E2E": strEncrypted,
"IV": strIV,
"Key": strSessionKey,
"Signature": strSignature,
},
Token: registrationToken,
}
// Send a message to the device corresponding to the provided
// registration token.
response, err := client.Send(ctx, message)
if err != nil {
logger.Info(err)
os.Exit(-536870138) /// -536870138 = 0xE0000306 is ERR_INOTE_FCM_FAILED_SEND
}
Any help appreciated,任何帮助表示赞赏,
-- Stefan ——斯蒂芬
You are doing great and absolutely right It android OS behaviour when app in background you are no longer to handle message你做得很好而且绝对正确当应用程序在后台时你不再处理消息时它的android操作系统行为
You need to add default intent filter in android manifest file and if notifications comes when app in background.您需要在 android manifest 文件中添加默认意图过滤器,如果应用程序在后台时出现通知。 So after clicking on fcm you will launch default activity.因此,单击 fcm 后,您将启动默认活动。 Here you can add your logics在这里你可以添加你的逻辑
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.