应用程序在前台时收到的推送通知与应用程序在后台时收到的推送通知不同 [英] Push notification received while the app is in the foreground is different from push notification received when the app is in background

查看:241
本文介绍了应用程序在前台时收到的推送通知与应用程序在后台时收到的推送通知不同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在Android应用中实现了FCM推送通知.

I have implemented FCM push notifications in an android app.

虽然我已登录到该应用程序.我收到的通知格式如下所示.

While I am logged in to the app. I get the notification in the form that I am expecting as below.

当应用程序在后台运行时,我会收到json响应,如下所示.

When the app is in background I receive the json response instead as below.

以下是我在onMessageRecieved()中添加的代码

Following is the code I have added in onMessageRecieved()

NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
            .setContentTitle("App")
            .setContentText(messageBody)
            .setAutoCancel(true)
            .setSound(defaultSoundUri)
            .setSmallIcon(R.mipmap.ic_launcher)
            .setContentIntent(pendingIntent)
            .setStyle(new NotificationCompat.BigPictureStyle()
                    .bigPicture(bitmap))/*Notification with Image*/;

在两种情况下如何以相同的方式获取通知.

How can I get the notification in the same manner in both cases.

任何帮助将不胜感激,谢谢

Any help will be appreciated thank you

推荐答案

在Android应用程序中设置FCM之后,您可以使用Firebase控制台发送通知.当前台应用程序收到通知时,将调用onMessageReceived方法.您应该重写此方法以处理通知,但是问题是当应用程序在后台接收通知时,通知传递到设备的系统托盘,并且您无法使用onMessageReceived方法处理通知.当用户点击通知时,默认情况下会打开应用启动器.例如,假设您想在收到用户通知时执行特定任务,或者在后台执行某项操作而用户没有意识到或不想向用户显示通知对话框,则当应用程序处于背景.

After setting up FCM in Android application, you can use Firebase console to send notifications. When foregrounded application receives a notification, onMessageReceived method is invoked. You should override this method to handle notification, but the problem is when the application is in the background and receives a notification, notification delivers to the device’s system tray and you can not handle notification with onMessageReceived method. When the user taps on a notification, it opens the app launcher by default. For example consider you want to do a specific task when a notification is received to the user or do something in background without the user realizing or don’t want to show notification dialog to the user, you can’t do these when the application is backgrounded.

使用FCM,您可以向客户发送两种类型的消息:

With FCM you can send two types of messages to clients :

1-通知消息,有时被认为是显示消息"

1- Notification message, sometimes thought of a "display message"

2-数据消息,由客户端应用程序处理

2- Data message, which are handled by the client application

根据Google文档,通知消息的限制为2KB,并且预定义了用户可见的密钥.数据消息使开发人员可以发送多达4KB的自定义键值对.

According to Google documents a notification message has a 2KB limit and a predefined user-visible keys. Data messages let developers send up to 4KB custom key-value pairs.

如果要在后台运行应用程序时处理通知,则应发送数据消息并使用onMessageReceived方法.

If you want to handle notification when the application is backgrounded you should send data message and use onMessageReceived method.

这是完整的文章.

这篇关于应用程序在前台时收到的推送通知与应用程序在后台时收到的推送通知不同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆