未解决的类“ MyFirebaseMessagingService”

时间:2019-02-16 05:53:25

标签: android firebase-cloud-messaging

我正在关注有关Setting up a Firebase Cloud Messaging client app on Android的教程,该教程涉及将<service>组件添加到AndroidManifest.xml

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

但是,我得到Unresolved class MyFirebaseMessagingService。我到底从哪里导入该类?

注意:我正在用科特林写作

1 个答案:

答案 0 :(得分:0)

在您提供的同一link上,提到您需要在项目中添加服务类,并在AndroidManifest.xml中声明它,如下所示:

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

所以基本上这些是您需要遵循的步骤:

  1. 在您的保留Java类的包中,创建一个名为MyFirebaseMessagingService的类(您可以选择任何名称)
  2. 如上所述,在AndroidManifest.xml中声明它。
  3. 您在上面的步骤1中定义的此类将扩展FirebaseMessagingService,因此它将具有一个称为onMessageReceived的方法,您必须重写该方法,如下所示:< / p>

       @Override
        public void onMessageReceived(RemoteMessage remoteMessage) {
            // ...
    
        // TODO(developer): Handle FCM messages here.
        Log.d(TAG, "From: " + remoteMessage.getFrom());
    
        // Check if message contains a data payload.
        if (remoteMessage.getData().size() > 0) {
            Log.d(TAG, "Message data payload: " + remoteMessage.getData());
    
            if (/* Check if data needs to be processed by long running job */ true) {
                // For long-running tasks (10 seconds or more) use Firebase Job Dispatcher.
                scheduleJob();
            } else {
                // Handle message within 10 seconds
                handleNow();
            }
    
        }
    
        // Check if message contains a notification payload.
        if (remoteMessage.getNotification() != null) {
            Log.d(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody());
        }
    
        // Also if you intend on generating your own notifications as a result of a received FCM
        // message, here is where that should be initiated. See sendNotification method below.
    }
    

如果您使用Kotlin编写代码,则需要将您的类命名为MyFirebaseMessagingService.kt,然后创建一个类并放在下面的代码中。该链接本身也提到了这一点。

override fun onMessageReceived(remoteMessage: RemoteMessage?) {
// ...

// TODO(developer): Handle FCM messages here.
Log.d(TAG, "From: ${remoteMessage?.from}")

// Check if message contains a data payload.
remoteMessage?.data?.isNotEmpty()?.let {
    Log.d(TAG, "Message data payload: " + remoteMessage.data)

    if (/* Check if data needs to be processed by long running job */ true) {
        // For long-running tasks (10 seconds or more) use Firebase Job Dispatcher.
        scheduleJob()
    } else {
        // Handle message within 10 seconds
        handleNow()
    }
}

// Check if message contains a notification payload.
remoteMessage?.notification?.let {
    Log.d(TAG, "Message Notification Body: ${it.body}")
}

// Also if you intend on generating your own notifications as a result of a received FCM
// message, here is where that should be initiated. See sendNotification method below.
}