我刚刚将FCM添加到现有应用程序中,看起来根本没有调用FirebaseMessagingService。
我为此创建了一个POC,它工作正常。安装应用程序时,当应用程序位于前台时,将调用onNewToken回调并调用onMessageReceived。
但是在我现有的项目中,它似乎不起作用。请注意,我的项目已经支持firebase远程配置
google-services.json文件已经下载并放置在应用程序目录下。
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'
...
}
答案 0 :(得分:0)
您可以尝试使用这些版本。因为在我的应用程序中,它运行良好。
implementation 'com.google.firebase:firebase-core:16.0.8'
implementation 'com.google.firebase:firebase-messaging:17.6.0'
答案 1 :(得分:0)
找出问题所在。问题是我的项目正在使用不推荐使用的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" />