简体   繁体   English

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

[英]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.

相关问题 使用 FCM 杀死应用程序时,Android 无法接收通知 - Android unable to receive notification when app is killed using FCM 如何在收到通知 [FCM] 时自动启动 android 应用程序? - How to auto launch android app when you receive notification [FCM]? 退出应用程序时收到FCM的通知 - Receive notification by FCM when exit the app FCM:当应用程序处于前台状态时处理其应用程序的不同数据通知 - FCM : Handling different data notification of an android app when app is in foreground 应用未运行时未显示通知徽标(FCM) - Notification logo not shown when app not running (FCM) 应用程序未运行时如何发送 fcm 通知 - How to send fcm notification when app is not running Android 应用程序未通过 Firebase FCM 接收推送通知 - Android App do not receive push notification trough Firebase FCM Android FCM:当应用程序在后台时 FCM 显示(通知)消息登录 - Android FCM: FCM display(notification) message loggin when app is in background 设置条件时,Android fcm从mysql接收通知 - Android fcm receive notification from mysql when condition is set 当应用程序在前台时未收到FCM推送通知,但在应用程序在后台时收到 - FCM Push notification didn't receive when app is in foreground but receive when app in background
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM