繁体   English   中英

当应用程序未运行时,在 Android 手机上接收 FCM 数据通知

[英]Receive an FCM data notification on Android phone when app is not running

我正在向端到端加密的 Android 客户端发送纯 FCM 数据通知。 android 应用程序解密数据通知有效负载并根据加密数据创建本地通知。 只要应用程序在前台或后台运行,这就可以正常工作:接收、解密通知有效负载,甚至显示在锁定屏幕上,但前提是应用程序正在运行。 我还查看了消息优先级并将其在发送到 FCM 的应用程序服务器中设置为“高”,无论是在通知消息的 AndroidConfig 成员中还是在数据字段中,但都无济于事。 将数据消息发送到相应应用程序当前未运行的 android 设备的正确方法是什么? 这可能吗?

我的服务器代码(在 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
}

任何帮助表示赞赏,

——斯蒂芬

你做得很好而且绝对正确当应用程序在后台时你不再处理消息时它的android操作系统行为

您需要在 android manifest 文件中添加默认意图过滤器,如果应用程序在后台时出现通知。 因此,单击 fcm 后,您将启动默认活动。 在这里你可以添加你的逻辑

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM