在FirebaseInstanceIdService上创建自定义侦听器以实现活动

时间:2016-11-12 17:07:09

标签: android firebase firebase-cloud-messaging

是否有可能在FirebaseInstanceIdService类中创建接口和侦听器并在活动中实现它?

因为我试图这样做而且我收到了错误。

这是我的代码:

public class _FirebaseInstanceIDService extends FirebaseInstanceIdService {

    private static final String TAG = "MyFirebaseIIDService";
    private static Context mContext = null;

    private  onListener mListener = null;




    /**
     * Called if InstanceID token is updated. This may occur if the security of
     * the previous token had been compromised. Note that this is called when the InstanceID token
     * is initially generated so this is where you would retrieve the token.
     */
    // [START refresh_token]
    @Override
    public void onTokenRefresh() {
        mListener=(onListener)this; //i got error in here
        // Get updated InstanceID token.
        String refreshedToken = FirebaseInstanceId.getInstance().getToken();
        Log.e(TAG, "Refreshed token: " + refreshedToken);

        if (!refreshedToken.isEmpty()){
            boolean b = PreferencesManager.init(this).saveToken(refreshedToken);

            if (b)
                if(mListener != null)
                    mListener.onTokenListener(refreshedToken);
        }

    }
    // [END refresh_token]




    public interface onListener{

        void onTokenListener(String token);
    }

}

错误:

java.lang.ClassCastException: rezkyaulia.android.dont_do.services._FirebaseInstanceIDService cannot be cast to rezkyaulia.android.dont_do.services._FirebaseInstanceIDService$onListener
    at rezkyaulia.android.dont_do.services._FirebaseInstanceIDService.onTokenRefresh(_FirebaseInstanceIDService.java:55)

我想知道它是否可以做。真的需要建议。

2 个答案:

答案 0 :(得分:1)

您正面临这个问题,因为您的服务未使用onListener实现,因为您正在使用'这个'来初始化它。这意味着你将实现引用到同一个类,但我得到了你想要的东西:

  

您希望服务与其他类之间的动态绑定   实施列表,但我会建议一个冗长的方法来做到这一点   因为

     

FirebaseInstanceIdService

是服务并且不建议使用服务接口,如果你的应用程序没有运行,你可以通过发送广播从服务更新,如果你的应用程序正在运行,那么使用IPC来执行它并从信使发送令牌

在您的活动或片段中使用以下代码 第1步:创建传入处理程序

class IncomingHandler extends Handler {
            @Override
            public void handleMessage(Message msg) {
                switch (msg.what) {
                    case MessengerService.MSG_SET_VALUE:
                        mCallbackText.setText("Received from service: " + msg.obj);
                        break;
                    default:
                        super.handleMessage(msg);
                }
            }
        }

        /**
         * Target we publish for clients to send messages to IncomingHandler.
         */
        final Messenger mMessenger = new Messenger(new IncomingHandler());

2:创建服务连接:

 /**
     * Class for interacting with the main interface of the service.
     */
    private ServiceConnection mConnection = new ServiceConnection() {
        public void onServiceConnected(ComponentName className,
                                       IBinder service) {
            // This is called when the connection with the service has been
            // established, giving us the service object we can use to
            // interact with the service.  We are communicating with our
            // service through an IDL interface, so get a client-side
            // representation of that from the raw service object.
            mService = new Messenger(service);
            mCallbackText.setText("Attached.");

            // We want to monitor the service for as long as we are
            // connected to it.
            try {
                Message msg = Message.obtain(null,
                        MessengerService.MSG_REGISTER_CLIENT);
                msg.replyTo = mMessenger;
                mService.send(msg);

                // Give it some value as an example.
                msg = Message.obtain(null,
                        MessengerService.MSG_SET_VALUE, this.hashCode(), 0);
                mService.send(msg);
            } catch (RemoteException e) {
                // In this case the service has crashed before we could even
                // do anything with it; we can count on soon being
                // disconnected (and then reconnected if it can be restarted)
                // so there is no need to do anything here.
            }

        }

        public void onServiceDisconnected(ComponentName className) {
            // This is called when the connection with the service has been
            // unexpectedly disconnected -- that is, its process crashed.
            mService = null;
        }
    };

3:在调用get Token之前调用Binding

    void doBindService() {
            // Establish a connection with the service.  We use an explicit
            // class name because there is no reason to be able to let other
            // applications replace our component.
            bindService(new Intent(MessengerServiceActivities.this,
                    MessengerService.class), mConnection, Context.BIND_AUTO_CREATE);
            mIsBound = true;
        }
FirebaseInstanceId.getInstance().getToken();

4:最后在您的FirebaseInstanceIdService类

public class MyFirebaseInstanceIDService extends FirebaseInstanceIdService {

    private static final String TAG = "MyFirebaseIIDService";

    /**
     * Keeps track of all current registered clients.
     */
    ArrayList<Messenger> mClients = new ArrayList<Messenger>();
    /**
     * Holds last value set by a client.
     */

    /**
     * Command to the service to register a client, receiving callbacks
     * from the service.  The Message's replyTo field must be a Messenger of
     * the client where callbacks should be sent.
     */
    public static final int MSG_REGISTER_CLIENT = 1;

    /**
     * Command to the service to unregister a client, ot stop receiving callbacks
     * from the service.  The Message's replyTo field must be a Messenger of
     * the client as previously given with MSG_REGISTER_CLIENT.
     */
    public static final int MSG_UNREGISTER_CLIENT = 2;

    /**
     * Command to service to set a new value.  This can be sent to the
     * service to supply a new value, and will be sent by the service to
     * any registered clients with the new value.
     */
    public static final int TOKEN_REFRESHED = 3;

    class IncomingHandler extends Handler {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case MSG_REGISTER_CLIENT:
                    mClients.add(msg.replyTo);
                    break;
                case MSG_UNREGISTER_CLIENT:
                    mClients.remove(msg.replyTo);
                    break;
                case TOKEN_REFRESHED:
                    for (int i = mClients.size() - 1; i >= 0; i--) {
                        try {
                            mClients.get(i).send(Message.obtain(null,
                                    TOKEN_REFRESHED, msg.arg1, 0));
                        } catch (RemoteException e) {
                            // The client is dead.  Remove it from the list;
                            // we are going through the list from back to front
                            // so this is safe to do inside the loop.
                            mClients.remove(i);
                        }
                    }
                    break;
                default:
                    super.handleMessage(msg);
            }
        }
    }


    /**
     * Called if InstanceID token is updated. This may occur if the security of
     * the previous token had been compromised. Note that this is called when the InstanceID token
     * is initially generated so this is where you would retrieve the token.
     */
    // [START refresh_token]
    @Override
    public void onTokenRefresh() {
        // Get updated InstanceID token.
        String refreshedToken = FirebaseInstanceId.getInstance().getToken();
        Message msg = Message.obtain(null,
                TOKEN_REFRESHED);
        msg.obj = refreshedToken;
        // TODO: Implement this method to send any registration to your app's servers.
        sendRegistrationToServer(refreshedToken);
    }
    // [END refresh_token]

    /**
     * Persist token to third-party servers.
     * <p/>
     * Modify this method to associate the user's FCM InstanceID token with any server-side account
     * maintained by your application.
     *
     * @param token The new token.
     */
    private void sendRegistrationToServer(String token) {
        // Add custom implementation, as needed.
        Log.e("", "FCM Token: " + token);
    }
}

答案 1 :(得分:-1)

FirebaseInstanceIdService .onTokenRefresh由服务实现。它无法在活动中实现。

无法监视令牌刷新活动,因为当您的应用处于非活动状态时,也可能发生令牌刷新。由于活动在应用程序被杀死时无法运行,因此您必须在服务中实施FirebaseInstanceIdService .onTokenRefresh来处理令牌刷新。