带有示例android testapp的GCM用于推送通知

时间:2016-02-18 19:20:10

标签: java android push-notification google-cloud-messaging

我一直在尝试通过逐步公会来学习如何使用02-18 14:07:42.866 32402-32402/com.example.mmillar.gcmpushnotificationtest D/Main Activity: Started 02-18 14:07:42.867 32402-32402/com.example.mmillar.gcmpushnotificationtest D/Checking PlayService: Started 02-18 14:07:42.868 32402-32402/com.example.mmillar.gcmpushnotificationtest D/Cheking player Service: true 02-18 14:07:42.868 32402-32402/com.example.mmillar.gcmpushnotificationtest D/Starting service:: Intent { cmp=com.example.mmillar.gcmpushnotificationtest/.Registration } 02-18 14:07:42.877 32402-32432/com.example.mmillar.gcmpushnotificationtest D/OpenGLRenderer: Use EGL_SWAP_BEHAVIOR_PRESERVED: true 02-18 14:07:42.949 32402-32432/com.example.mmillar.gcmpushnotificationtest I/OpenGLRenderer: Initialized EGL, version 1.4 02-18 14:07:43.008 32402-32432/com.example.mmillar.gcmpushnotificationtest W/EGL_emulation: eglSurfaceAttrib not implemented 02-18 14:07:43.008 32402-32432/com.example.mmillar.gcmpushnotificationtest W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0xad79f100, error=EGL_SUCCESS 02-18 14:07:45.679 32402-32407/com.example.mmillar.gcmpushnotificationtest W/art: Suspending all threads took: 12.876ms 02-18 14:07:53.143 32402-32429/com.example.mmillar.gcmpushnotificationtest I/GMPM: Tag Manager is not found and thus will not be used 02-18 14:08:33.167 32402-32411/com.example.mmillar.gcmpushnotificationtest W/art: Suspending all threads took: 5.121ms 02-18 14:10:19.493 32402-32407/com.example.mmillar.gcmpushnotificationtest W/art: Suspending all threads took: 6.036ms 02-18 14:11:15.635 32402-32407/com.example.mmillar.gcmpushnotificationtest W/art: Suspending all threads took: 12.898ms 。我到了注册的地步,但它实际上从未进行过项目的注册部分。我在项目中有配置文件,我正在使用此网站测试推送通知http://www.androidbegin.com/tutorial/gcm.html 所以这就是它在logcat中给我的东西:

onCreate

因为它显示它开始(public class GCMMainActivity extends AppCompatActivity{ private static final int PLAY_SERVICES_RESOLUTION_REQUEST = 9000; private static final String TAG = "MainActivity"; private BroadcastReceiver mRegistrationBroadcastReceiver; private ProgressBar mRegistrationProgressBar; private TextView mInformationTextView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_gcmmain); Log.d("Main Activity", "Started"); mRegistrationProgressBar = (ProgressBar) findViewById(R.id.registrationProgressBar); mRegistrationBroadcastReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { Log.d("Main Activity: ", "onReceive Method"); mRegistrationProgressBar.setVisibility(ProgressBar.GONE); SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context); boolean sentToken = sharedPreferences.getBoolean(Preferences.SENT_TOKEN_TO_SERVER, false); if (sentToken) { mInformationTextView.setText("Token retrieved and sent to server! You can now use gcmsender to\n" + " send downstream messages to this app"); } else { mInformationTextView.setText("An error occurred while either fetching the InstanceID token,\n" + "sending the fetched token to the server or subscribing to the PubSub topic. "); } } }; mInformationTextView = (TextView) findViewById(R.id.informationTextView); if (checkPlayServices()) { Log.d("Checking PlayService", "Started"); Boolean check = checkPlayServices(); Log.d("Cheking player Service ", check.toString()); // Start IntentService to register this application with GCM. Intent intent = new Intent(this, Registration.class); //this should call the registration code Log.d("Starting service: ", intent.toString()); startService(intent); } } @Override protected void onResume() { super.onResume(); LocalBroadcastManager.getInstance(this).registerReceiver(mRegistrationBroadcastReceiver, new IntentFilter(Preferences.REGISTRATION_COMPLETE)); } @Override protected void onPause() { LocalBroadcastManager.getInstance(this).unregisterReceiver(mRegistrationBroadcastReceiver); super.onPause(); } //deos the device support google play? private boolean checkPlayServices() { GoogleApiAvailability apiAvailability = GoogleApiAvailability.getInstance(); int resultCode = apiAvailability.isGooglePlayServicesAvailable(this); if (resultCode != ConnectionResult.SUCCESS) { if (apiAvailability.isUserResolvableError(resultCode)) { apiAvailability.getErrorDialog(this, resultCode, PLAY_SERVICES_RESOLUTION_REQUEST) .show(); } else { Log.d(TAG, "This device is not supported."); finish(); } return false; } return true; } } 方法的intent0部分。但它根本没有到达Register类。我对我在这里做错了一点感到有点困惑。任何帮助将不胜感激。

这是启动应用程序的主要活动代码

public class Registration  extends IntentService {

    private static final String TAG = "RegIntentService";
    private static final String[] TOPICS = {"global"};
    private final String SENDERID = "630172590481";

    public Registration() {
        super(TAG);
    }

    @Override
    protected void onHandleIntent(Intent intent) {
        Log.d("Registration Class: ", "onHandleIntent mehtod");
        SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);

        //Register for the gcm
        try {

            // Initially this call goes out to the network to retrieve the token, subsequent calls
            // are local.
            InstanceID instanceID = InstanceID.getInstance(this);

            //this is using the google configuration file crated at their website
            //https://developers.google.com/cloud-messaging/android/start
            String token = instanceID.getToken(getString(R.string.gcm_defaultSenderId),
                    GoogleCloudMessaging.INSTANCE_ID_SCOPE, null);
            /*used to hardcode the Sender ID
            String token = instanceID.getToken(SENDERID),
                    GoogleCloudMessaging.INSTANCE_ID_SCOPE, null);
                    */
            Log.d(TAG, "GCM Registration Token: " + token);

            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(Preferences.SENT_TOKEN_TO_SERVER, true).apply();
        } 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
            // update at a later time.
            sharedPreferences.edit().putBoolean(Preferences.SENT_TOKEN_TO_SERVER, false).apply();
        }
        // Notify UI that registration has completed, so the progress indicator can be hidden.
        Intent registrationComplete = new Intent(Preferences.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) {
        // Add custom implementation, as needed.
    }

    //Subscribe to any GCM topics of interest, as defined by the TOPICS constant.
    private void subscribeTopics(String token) throws IOException {
        GcmPubSub pubSub = GcmPubSub.getInstance(this);
        for (String topic : TOPICS) {
            pubSub.subscribe(token, "/topics/" + topic, null);
        }
    }
}

这是注册类

{{1}}

0 个答案:

没有答案