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:
IntentService
. 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?
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:
In fact, there are many other details to be implemented, like:
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.