安装应用程序时永远不会调用 FirebaseMessagingService
Posted
技术标签:
【中文标题】安装应用程序时永远不会调用 FirebaseMessagingService【英文标题】:FirebaseMessagingService is never called when app is installed 【发布时间】:2019-07-09 09:53:44 【问题描述】:我刚刚将 FCM 添加到我现有的应用程序中,看起来它不是 FirebaseMessagingService,根本没有被调用。
我为此创建了一个 POC,它运行良好。在安装应用时调用 onNewToken 回调,并在应用处于前台时调用 onMessageReceived。
但在我现有的项目中,它似乎不起作用。请注意,我的项目已经支持 firebase 远程配置
google-services.json 文件已被下载并放在 app 目录下。
androidManifest.xml
<service
android:name=".MyFirebaseMessaging"
android:exported="false">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
MyFirebaseMessaging.java
public class MyFirebaseMessaging extends FirebaseMessagingService
private final String TAG = MyFirebaseMessaging.class.getSimpleName();
public final static String channel1ID = "channel1ID";
public final static String channel1Name = "channel1Name";
@Override
public void onMessageReceived(RemoteMessage remoteMessage)
Log.i(TAG, "<<notification>> onMessageReceived");
if (remoteMessage == null)
return;
if (remoteMessage.getData() != null && remoteMessage.getData().size() > 0)
Map<String, String> data = remoteMessage.getData();
String body = data.get("body");
String title = data.get("title");
generateNotification(title, body);
else if(remoteMessage.getNotification() != null)
RemoteMessage.Notification notification = remoteMessage.getNotification();
String title = notification.getTitle();
String body = notification.getBody();
generateNotification(title, body);
private void generateNotification(String title, String body)
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
NotificationChannel channel1 = new NotificationChannel(channel1ID, channel1Name,
NotificationManager.IMPORTANCE_DEFAULT);
notificationManager.createNotificationChannel(channel1);
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.my_large_icon);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, channel1ID)
.setSmallIcon(R.drawable.my_icon)
.setContentTitle(title)
.setContentText(body)
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
.setLargeIcon(bitmap)
.setStyle(new NotificationCompat.BigPictureStyle().bigPicture(bitmap).bigLargeIcon(null));
notificationManager.notify(0, builder.build());
@Override
public void onNewToken(String s)
super.onNewToken(s);
Log.i(TAG, "<<notification>> onNewToken: " + s);
build.gradle - 应用程序
dependencies
...
implementation 'com.google.firebase:firebase-core:17.0.0'
implementation 'com.google.firebase:firebase-messaging:19.0.1'
...
apply plugin: 'com.google.gms.google-services'
build.gradle - 项目
dependencies
...
classpath 'com.google.gms:google-services:4.2.0'
...
【问题讨论】:
您可以尝试使用降级的消息吗?比如'com.google.firebase:firebase-messaging:17.1.0' 你使用 Firebase 控制台发送消息吗? @JolsonDaCosta 我使用了 Firebase 控制台和邮递员来发送数据负载。两个 r 都不起作用 @majuran 会尝试。但它在我的 poc 项目中运行良好 【参考方案1】:你能试试这些版本吗?因为在我的应用中它运行良好。
implementation 'com.google.firebase:firebase-core:16.0.8'
implementation 'com.google.firebase:firebase-messaging:17.6.0'
【讨论】:
【参考方案2】:发现问题。问题是我的项目正在使用已弃用的 FirebaseInstanceIdService。从我的清单文件中删除它们有效
<receiver
android:name="com.google.firebase.iid.FirebaseInstanceIdInternalReceiver"
tools:node="remove" />
<receiver
android:name="com.google.firebase.iid.FirebaseInstanceIdReceiver"
tools:node="remove" />
<service
android:name="com.google.firebase.iid.FirebaseInstanceIdService"
tools:node="remove" />
【讨论】:
以上是关于安装应用程序时永远不会调用 FirebaseMessagingService的主要内容,如果未能解决你的问题,请参考以下文章
FCM onMessageReceived() 永远不会被调用