简体   繁体   English

未调用GCM onHandleIntent(Intent intent)

[英]GCM onHandleIntent(Intent intent) not being called

My onHandleIntent(Intent intent) function in gcmIntentService has the registration for GCM as well as the call variable to another function that sends the gcm token to the back-end db. 在我的onHandleIntent(意向意图)函数gcmIntentService具有用于登记GCM以及呼叫变量到用于发送GCM令牌后端分贝另一功能。 While I run my MainActivity the constructor of GcmintentService gets called while onHandleIntent doesnt. 虽然我跑我的MainActivity的构造GcmintentService被调用,而onHandleIntent犯规。 this is my GcmIntentService . 这是我的GcmIntentService

public class GcmIntentService extends IntentService {

    private static final String TAG = "RegIntentService";
    private static final String[] TOPICS = {"global"};
    String senderId = "1048700326431";

    AsyncHttpClient client = new AsyncHttpClient();

    public GcmIntentService() {

        super(TAG);
        Log.i(TAG, "ALWAYS IN HERE");
    }

    @Override
    public void onHandleIntent(Intent intent) {

        Log.i(TAG, " NOW I NEED IT IN HERE");

        SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);

        try {
            // [START register_for_gcm]
            // Initially this call goes out to the network to retrieve the token, subsequent calls
            // are local.
            // [START get_token]
            InstanceID instanceID = InstanceID.getInstance(this);
            String token = instanceID.getToken(senderId, GoogleCloudMessaging.INSTANCE_ID_SCOPE, null);

            // [END get_token]
            Log.i(TAG, "GCM Registration Token: " + token);

            // TODO: Implement this method to send any registration to your app's servers.
            sendRegistrationToServer(token);

            // Subscribe to topic channels
            subscribeTopics(token);

            // You should store a boolean that indicates whether the generated token has been
            // sent to your server. If the boolean is false, send the token to your server,
            // otherwise your server should have already received the token.
            sharedPreferences.edit().putBoolean(QuickstartPreferences.SENT_TOKEN_TO_SERVER, true).apply();
            // [END register_for_gcm]
        } catch (Exception e) {
            Log.d(TAG, "Failed to complete token refresh", e);
            // If an exception happens while fetching the new token or updating our registration data
            // on a third-party server, this ensures that we'll attempt the update at a later time.
            sharedPreferences.edit().putBoolean(QuickstartPreferences.SENT_TOKEN_TO_SERVER, false).apply();
        }
        // Notify UI that registration has completed, so the progress indicator can be hidden.
        Intent registrationComplete = new Intent(QuickstartPreferences.REGISTRATION_COMPLETE);
        LocalBroadcastManager.getInstance(this).sendBroadcast(registrationComplete);
        }

    /**
     * Persist registration to third-party servers.
     *
     * Modify this method to associate the user's GCM registration token with any server-side account
     * maintained by your application.
     *
     * @param token The new token.
     */
    private void sendRegistrationToServer(String token) {
            final RequestParams params = new RequestParams();
            params.put("id", token);

            client.post("", params, new JsonHttpResponseHandler() {

                @Override
                public void onStart() {
                    // called before request is started
                }

                public void onSuccess(int statusCode, PreferenceActivity.Header[] headers, JSONObject response) {

                }

                public void onFailure(int statusCode, PreferenceActivity.Header[] headers, JSONObject errorResponse, Throwable e) {
                    // called when response HTTP status is "4XX" (eg. 401, 403, 404)
                }

                @Override
                public void onRetry(int retryNo) {
                    // called when request is retried
                }
            });
    }

    /**
     * Subscribe to any GCM topics of interest, as defined by the TOPICS constant.
     *
     * @param token GCM token
     * @throws IOException if unable to reach the GCM PubSub service
     */
    // [START subscribe_topics]
    private void subscribeTopics(String token) throws IOException {
        for (String topic : TOPICS) {
            GcmPubSub pubSub = GcmPubSub.getInstance(this);
            pubSub.subscribe(token, "/topics/" + topic, null);
        }
    }

}

I had same issue but solved it by adding this to the manifest: 我有同样的问题,但通过将其添加到清单解决了它:

<service
            android:name="com.futec.h2o.RegistrationIntentService"
            android:exported="false" >
            <intent-filter>
                <action android:name="com.google.android.c2dm.intent.REGISTRATION" />
            </intent-filter>
        </service>

You might be missing this on your manifest: 您可能在清单上遗漏了这个:

<service
    android:name="your.package.name.RegistrationIntentService"
    android:exported="false">
</service>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM