Firebase在Android 12上推送通知无法接收

7
我的Xamarin.Android应用程序只在Android 11(Pixel 3 XL)上能收到推送通知。目前,我的应用程序针对Android 11进行了定位,但它也可以在Android 12(Pixel 6 Pro)上运行。唯一不起作用的是Firebase推送通知。以下是我正在使用的代码。在过去的一周中,我一直在研究这个问题,并看到有关Android 12(Pixel 6)无法接收推送通知的特定问题的帖子。我按照其他人建议的更改了手机配置,并且另一个应用程序的通知开始工作了,但是我的仍然没有。任何想法都会有所帮助。谢谢。
 if (Build.VERSION.SdkInt >= BuildVersionCodes.O)
            {
                // Notification channels are new in API 26 (and not a part of the
                // support library). There is no need to create a notification
                // channel on older versions of Android.


                var name = "NameOfChannel";
                var description = "Notification Channel";
                var channel = new NotificationChannel(CHANNEL_ID, name, NotificationImportance.Max)
                {
                    Description = description
                };

                var notificationManager = (NotificationManager)GetSystemService(NotificationService);
                notificationManager.CreateNotificationChannel(channel);

            }       

我不确定你遇到了什么问题,你是否收到了异常信息或者通知没有显示出来...... - FreakyAli
@FreakyAli 当我同时从Firebase控制台向Pixel 3和Pixel 6手机发送测试消息时,只有Pixel 3 Android 11收到了消息。对于Pixel 6 Android 12手机,我的日志中没有显示任何错误。我已经检查了我的代码和FCM令牌以确保它是正确的。但仍然没有任何进展。谢谢。 - Blackie
你是否设置了断点并检查是否收到了它? - FreakyAli
据我所知,Android 12 在网络方面有一些特殊之处。您可以在以下链接中获得进一步的帮助。https://github.com/xamarin/xamarin-android/issues - Wendy Zang - MSFT
4个回答

5
我错过的是什么。
PendingIntent.FLAG_IMMUTABLE

如果通知具有挂起意图,则将可变/不可变标志设置为它。

3
               PendingIntent pendingIntent;
                if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.S)
                {
                pendingIntent=PendingIntent.getActivity(context,notificationId,notificationIntent,PendingIntent.FLAG_UPDATE_CURRENT|PendingIntent.FLAG_MUTABLE);
                }
                else
                {
                pendingIntent=PendingIntent.getActivity(context,notificationId,notificationIntent,PendingIntent.FLAG_UPDATE_CURRENT);
                }
                builder.setContentIntent(pendingIntent)

3
找到了解决方案。就是在AndroidManifest中的Firebase服务标签上添加android:exported="false"

1
Firebase服务标签是什么?我在我的AndroidManifest文件中没有看到类似的内容,我遇到了Android 12设备无法接收推送通知的问题。 - Kyle Abens
3
我已经在FirebaseMessageService服务中添加了exported="false",但仍未收到任何通知。 - Dark Leonhart
将“changing to false”改为false会做什么? - DouggyC

0

我也遇到了类似的问题(Android/Kotlin),虽然我已经修改了Pending Intent的更改并设置了“exported=false”,但是应用程序仍然无法接收推送通知。

下面是Firebase版本

implementation platform('com.google.firebase:firebase-bom:31.0.3')
implementation 'androidx.work:work-runtime-ktx'
implementation 'com.google.firebase:firebase-messaging'
implementation 'com.google.firebase:firebase-iid'

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接