简体   繁体   中英

Associate Xamarin GCM device token with logged in user

I'm working my way through a Xamarin tutorial on how to sent push notifications to an Android device [1]. As an example, this works great.

However, I have trouble figuring out how I can associate the device token with a currently logged in user. The tutorial does a pretty basic demonstration on how to obtain a device token and sending this to an app server (custom implementation requirement, I'm using a simple REST API). However, all the logic to connect to the app server is contained within the IntentService .

To associate the device token with a logged in user I assume I have two options:

  • a) Somehow inject the user information into the IntentService .
  • b) Have the IntentService emit a custom event in the OnHandleIntent handler to which my application can respond.

Since I would like to implement push notifications for both Android and iOS, ideally 'b' would be the best option since I can create a better abstraction but I guess either solution would be workable for the moment.

Given these options, what would be a good strategy to associate the device token with the currently logged in user?

[1] https://developer.xamarin.com/guides/cross-platform/application_fundamentals/notifications/android/remote_notifications_in_android

It´s not just the associating part. Your server must be able to send push notifications to google gcm and apple apn. I´m wondering if you already covered that part. Doing so manually would be a pain. If you are using .NET in the server side, check out this library . Here´s another for python/django

The association part can be done easily in a web api controller. Basically you need:

  1. A table (1 to many) for device registrations: user id foreign key + registration id/token
  2. Create a controller POST method to save user id (with fk to actual user) + registration id/token to the previous table
  3. Once the user logs in your app, run all the gmc/apn registration process.
  4. Once you get your registration id/token, send it to the controller

In fact, there are many other details to be implemented, like:

  1. What happens if you get 2/3 different registration ids for the same user?
  2. You should provide another REST method to unregister, and call it for example when the user logs out in the mobile app.
  3. Etc...

There are some libs taking care of all this. I suggest you take a look and pick one.

EDIT

Another option would be to subscribe to another tag that would be exactly the user id "topics/id"

you need to send unique android_id with url ,something like , where regid is token u get ,

which u will get from : android_id = Settings.Secure.getString(getContentResolver(), Settings.Secure.ANDROID_ID);

 String url = "https://push.monitoring.computer/api/notification/device/register/" + "1/" + regid + "/android/" + android_id;

        try {

            URL url1 = new URL(url);
            URLConnection uc = url1.openConnection();

            String basicAuth = Base64.encodeToString((APIKEY).getBytes("UTF-8"), Base64.NO_WRAP);

            uc.setRequestProperty("Authorization", basicAuth);
            InputStream in = uc.getInputStream();

            // Log.e(TAG, "response: " + response);

        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        },

This is java code which can easily translated to c#

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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