1

I am trying to use GCM for android. After some steps that are provided by developer.google i got the registration id in toast, Now i want to store it in database. How to change here to store it in database. What steps i need to change for this updation???

code

 mRegistrationBroadcastReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {

            // checking for type intent filter
            if (intent.getAction().equals(config.REGISTRATION_COMPLETE)) {
                // gcm successfully registered
                // now subscribe to `global` topic to receive app wide notifications
                String token = intent.getStringExtra("token");

                Toast.makeText(getApplicationContext(), "GCM registration token: " + token, Toast.LENGTH_LONG).show();

            } else if (intent.getAction().equals(config.SENT_TOKEN_TO_SERVER)) {
                // gcm registration id is stored in our server's MySQL

                Toast.makeText(getApplicationContext(), "GCM registration token is stored in server!", Toast.LENGTH_LONG).show();

            } else if (intent.getAction().equals(config.PUSH_NOTIFICATION)) {
                // new push notification is received

                Toast.makeText(getApplicationContext(), "Push notification is received!", Toast.LENGTH_LONG).show();
            }
        }
    };
proversion
  • 573
  • 3
  • 12
  • 24
  • You can use SharedPreference to store your token.As token is just a single variable so instead of saving it in database you can save it in sharedprefences. – Lips_coder Apr 20 '16 at 10:23

3 Answers3

1

You can store the GCM token in SharedPreferences itself.

Here is small piece of code that you can add after receiving the token:

SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(mContext);

SharedPreferences.Editor e = sharedPreferences.edit();
            e.putString("GCMTOKEN", token);
            e.commit();

And to get the token you can use this,

String token = sharedPreferences.getString("GCMTOKEN", null);
  • Actually what i want to do is, I would like to get notification from database when it is updated by values. I have different client system and corresponding to the system i have to send different notifications. – proversion Apr 20 '16 at 06:10
0

I would recommend you to keep the token as a key-value pair in SharedPreference. You will be able to retrieve and save/update your registration ID with ease using it instead of creating a record in mySQL.

There is an implementation in the top answer in this question. Do take a look.

How to get RegistrationID using GCM in android

Community
  • 1
  • 1
sihao
  • 431
  • 2
  • 11
0

You can follow this sample for how to deal with push token registration and refreshing token as well which is an important part

https://github.com/ChamariaUmang/MoEngageDemo

Umang
  • 966
  • 2
  • 7
  • 17