【问题标题】:Issue with receiving notifications when app is in background应用程序在后台时接收通知的问题
【发布时间】:2020-07-10 20:04:03
【问题描述】:

我正在为我的 Android 应用程序使用 Firebase FCM 服务。面对其中列出的问题。

  1. 有时当应用程序收到通知时它不会发出声音。下面是代码sn-p。
private fun createNotificationChannel(
    context: Context, id: String, name: String, description: String,
    path: Uri?, dnd: Boolean, importance: Int,
    notificationManager: NotificationManager
) {


    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        val existingChannel =
            notificationManager.getNotificationChannel(id)

        if (existingChannel != null) {
            notificationManager.deleteNotificationChannel(id)
        }
        val mAudioManager =
            context.getSystemService(Context.AUDIO_SERVICE) as AudioManager

        mAudioManager.setStreamVolume(
            AudioManager.STREAM_ALARM,
            mAudioManager.getStreamMaxVolume(AudioManager.STREAM_ALARM),
            0
        )

        val channel = NotificationChannel(id, name, importance)

        channel.description = description
        channel.enableLights(true)
        channel.setShowBadge(true)
        channel.describeContents()
        channel.setBypassDnd(dnd)
        channel.lockscreenVisibility = Notification.VISIBILITY_PRIVATE
        channel.lightColor = R.color.red
        channel.enableVibration(true)

        if (id == CHANNEL_ID_NORMAL_PRIORITY) {
            if (!SharedPrefUtils.getSharedPrefIsAppInForeground(context)) {
                channel.setSound(
                    path,
                    AudioAttributes.Builder()
                        .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
                        .setUsage(AudioAttributes.USAGE_ALARM)
                        .build()
                )
            }
        } else if (id == CHANNEL_ID_HIGH_PRIORITY) {

            channel.setSound(
                path,
                AudioAttributes.Builder()
                    .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
                    .setUsage(AudioAttributes.USAGE_ALARM)
                    .build()
            )
        }

        notificationManager.createNotificationChannel(channel)
    }
}
  1. 如果 App 长时间未运行,则不会调用 firebase 消息服务的 onMessageReceived() 方法。在这种情况下,当应用程序进入前台时,我会收到通知。
  2. 在推送通知中,我只发送数据负载,因为当应用程序处于后台(运行或关闭)并接收通知时,如果我们使用通知负载,它不会为横幅设置大图标。下面是推送通知的代码 sn-p。
AndroidConfig androidConfig = AndroidConfig.builder()
        .setTtl(3600 * 1000)
        .setPriority(AndroidConfig.Priority.HIGH)
        //.setNotification(androidNotification)
        .putData("title", title)
        .putData("body", StringEscapeUtils.unescapeJava(content))
        .putData("icon", icon)
        .putData("sound", soundData)
        .putData("channelid", channelId)
        .build();

Message msg = Message.builder()
        .setAndroidConfig(androidConfig)
        .setToken(token)
        .build();

String response = FirebaseMessaging.getInstance().send(msg);

【问题讨论】:

  • “如果 App 长时间未运行,则不会调用 firebase 消息服务的 onMessageReceived() 方法” 这将取决于您发送的消息类型到您的应用程序(通知消息或数据消息)。对于通知消息,onMessageReceived 仅在您的应用位于前台时才会调用。否则消息将被传递到系统通知托盘。
  • 嗨迈克尔,我只使用数据消息。

标签: android kotlin firebase-cloud-messaging


【解决方案1】:

就像Micheal pointed out in the comment 一样,这是(不幸的是)使用notification 字段时FCM 的预期行为。

Handling messages section of the documentation 中了解更多信息。还有最重要的一点:


通常的解决方法是改变 FCM 的结构:

不要使用 notification 字段(让它完全不用),但使用 data 字段并基于此构建您的本地推送通知。

【讨论】:

  • 嗨@Bartek Lipinski,我只使用数据消息。但是当应用程序被杀死时。该应用没有收到通知。
  • @DeepashreeMulay 也许你在notification 字段中有一些东西(即使你没有直接使用)。如果它存在(即使你没有使用它)当你的应用被杀死时你不会有onMessageReceived
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-06-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多