MyFirebaseMessagingService

MyFirebaseMessagingService

我需要从MyFirebaseMessagingService类调用一个工作方法,该方法接受上下文作为参数。但是我无权访问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.
    }
}

最佳答案

FirebaseMessagingService扩展Service

Service扩展ContextWrapper

ContextWrapper扩展Context

所以它也是一个上下文,所以使用MyFirebaseMessagingService.this或简单地this

关于android - 在MyFirebaseMessagingService中获取应用程序上下文,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/49541125/

10-08 20:35