【问题标题】:FCM topic notifications being received on emulator but no other device在模拟器上收到 FCM 主题通知,但没有其他设备
【发布时间】:2018-11-14 09:25:54
【问题描述】:

我遇到了一个问题,即 Firebase Cloud Messaging 会将通知发送到模拟器(Android 7.0,安装了播放服务),但不会发送到运行 Android 8+ 的任何设备(Google Pixel 2、三星 s9+、华为 MATE 10)。我最初认为这可能是关闭监听器的节能问题,但即使我从应用程序中删除优化,它仍然没有通过。

我知道下游请求已成功发送,并且我从响应中收到状态代码200(当然,因为正在模拟器上收到通知)。然而,在设备上调试时,我的FCMService 类中的onMessageReceived 根本没有被调用,无论是在前台还是后台。

它肯定已在清单中注册,并且每个设备都使用相同版本的应用程序:

<service android:name=".services.FCMService">
    <intent-filter>
        <action android:name="com.google.firebase.MESSAGING_EVENT"/>
    </intent-filter>
</service>

如果需要更多信息,请告诉我。提前致谢。

【问题讨论】:

    标签: android firebase firebase-cloud-messaging


    【解决方案1】:

    为什么会这样?

    当我在问题中输入版本号时,我意识到它与 Android Oreo 有关。我正在搜索 FCM 的问题,而不是最近更改通知的工作方式。在 Android 26+ 中,notification channels 已被引入,如果您的应用针对 26 或更高版本(我曾经是),则它是必需的。因为我不假思索地更新了目标SDK,所以我当然没有通知。

    如何解决

    最简单的方法是只针对 API 25,但这并不理想。

    您可以做的是注册通知渠道。我已经在处理通知的地方完成了此操作(所以onMessageReceived),因为它不会多次注册同一个频道。

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
    
        // Set the default notification channel settings to be that of alert
        int importance = NotificationManager.IMPORTANCE_HIGH;
        String channel_id = ALERT_CHANNEL_ID;
        CharSequence channel_name = ALERT_CHANNEL_NAME;
        String channel_desc = ALERT_CHANNEL_DESC;
    
        // Logic to change the channel id, name and desc if required
    
        // Build and modify channel settings
        NotificationChannel channel = new NotificationChannel(channel_id, channel_name, importance);
        channel.setDescription(channel_desc);
        channel.enableLights(true);
        channel.enableVibration(true);
    
        // Create the notification channel
        notificationManager.createNotificationChannel(channel);
    }
    

    然后你要更改通知生成器:

    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, channel_id)
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-01-11
    • 2021-09-08
    • 1970-01-01
    • 2020-04-25
    • 1970-01-01
    • 2013-09-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多