安装应用程序时,从未调用FirebaseMessagingService



我刚刚将FCM添加到了我现有的应用中,看起来根本没有被命名为Firebasemessagingservice。

我为此创建了一个POC,而且效果很好。安装应用程序时,请调用NewToken回调,并在应用程序在前景时被调用。

但是在我现有的项目中,它似乎不起作用。请注意,我的项目已经支持Firebase远程配置

google-services.json文件已经下载并放置在app dir下。

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 -app

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'
   ...
 }

您可以尝试这些版本吗?因为在我的应用程序中工作正常。

implementation 'com.google.firebase:firebase-core:16.0.8'
implementation 'com.google.firebase:firebase-messaging:17.6.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" />

相关内容

  • 没有找到相关文章

最新更新