在MyFirebaseMessagingService

时间:2018-03-28 17:52:36

标签: android

我需要从我的MyFirebaseMessagingService类中调用一个worker方法,该类接受上下文作为参数。但我无法访问MyFirebaseMessagingService中的应用程序上下文。怎么办呢?

public class MyFirebaseMessagingService extends FirebaseMessagingService {

    private static final String TAG = MyFirebaseInstanceIDService.class.getSimpleName();

    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        Log.d(TAG, "From: " + remoteMessage.getFrom());

        if (remoteMessage.getData().size() > 0) {
            Log.d(TAG, "Message data payload: " + remoteMessage.getData());


            MessageSender messageSender = new MessageSender();
            messageSender.sendMessage(CONTEXT HERE);

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

1 个答案:

答案 0 :(得分:1)

FirebaseMessagingService扩展了Service

Service扩展了ContextWrapper

ContextWrapper延伸Context

因此它也是一个上下文,因此请使用MyFirebaseMessagingService.this或简单this