【问题标题】:Receive an FCM data notification on Android phone when app is not running应用未运行时在 Android 手机上接收 FCM 数据通知
【发布时间】:2020-09-07 12:55:56
【问题描述】:

我正在向端到端加密的 Android 客户端发送纯 FCM 数据通知。 android 应用程序解密数据通知负载并从加密数据创建本地通知。只要应用程序在前台或后台运行,它就可以正常工作:通知有效负载被接收、解密,甚至显示在锁定屏幕上,但前提是应用程序正在运行。我还在通知消息的 AndroidConfig 成员和数据字段中查看了消息优先级,并将其设置为发送到 FCM 的应用程序服务器中的“高”,但无济于事。将数据消息发送到相应应用当前未运行的 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 push-notification firebase-cloud-messaging firebase-notifications


    【解决方案1】:

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

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

    【讨论】:

    • 你能举例说明如何做到这一点吗?我的 androidmanifest.xml 看起来像这样: android:name="com.google.firebase.iid.FirebaseInstanceIdReceiver" android:exported="true" android:permission="com.google.android.c2dm.permission.SEND">
    • 是否已经符合条件,或者此片段是否缺少您希望启动应用程序以接收数据消息有效负载的内容?
    • 好的,让我与您分享如何处理通知背景和前景的代码......需要一些时间
    • 对不起,我在使用 Xamarin 构建期间以某种方式按需生成之前发布了 androidmanifest.xml 的片段。有没有办法可以发布我生成的文件,因为它跨越了几行?
    猜你喜欢
    • 2016-10-17
    • 1970-01-01
    • 2021-09-16
    • 1970-01-01
    • 1970-01-01
    • 2019-02-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多