Hello. I can successfully send push notification to device. But not sure how to make sure that push notification is sent to specific user (example multiple users log in on same device). Should i handle it in my code, so when I receive push notification, check if userID match and display alert only if it is. Or is there a smarter way to handle this?
Well⌠when a user starts to use your application, make him âlog inâ and then send to your back-end server his username associated with the reg-id for push notifications. then you will have a way of associating a user to a reg-id via your back-endâs database.
The problem is that cordova push plugin always creates the same token for same device. So I will end up having multiple users with same tokens in my DB, and sending push notification to one user will also send it to another user.
One solution I have in my mind is to remove token field from user in database every time he logs out! But this creates another problem of not being able to send push notifications to users who logged out and he will not receive it once he logs back in
Are you sure that cordova generates a unique id for a device? Even if you call unregister?
Iâm not thaaat sure, but I think it changes.
I think the solution would be work on PushPlugin then (written in java for Android and C for iOS).
For Android for example:
You can send in payload which user the notification is destined to, check if it is the same as the current logged in user and create the notification or not. (Line 76 creates the notification).
To save the current user, create a new action on execute in PushPlugin.java
Also, create a global variable, and call cordova.exec(); on javascript to send to Java the current user information, store on global variable just created, and check onMessage âŚ
If you plan to work on iOS, you also need to change the logic, itâs almost the same!
Thanks. I will double check tomorrow morning to make sure that token is different! Maybe I rushed with my conclusion.
I really really hope I wonât need to go and edit native code for this
Also as far as I know you can send custom data in payload with notification, but unless app is in focus on iOS you wonât be able to read it before it is clicked.
What back end service are you using for push notifications?
I am using PushWoosh on a couple of my apps. I always set the PushWoosh âtagâ with my appâs logged in username when they sign on, and all pushes are filtered by tag so that I can push directly to a logged in user.
We have a Java backend that sends push notifications through Parse.com for iOS and directly to GDC for Android. PooshWoosh is a good option, but I would rather try to do it with existing solution as everything else works great, I donât want to change it just because of this, but I might have to. Thanks for the tip!
Yes, it is a big job, and an inconvenience to switch push providers mid development. Does Parse support the concept of âtagsâ for devices?
The thing with my PushWoosh apps is that when the user logs in, their userId is essentially âtaggedâ to the deviceId that PushWoosh uses. This means that even if they are logged off or the app isnât running, any pushes filtered by that tag are still sent to that device.
Until another user logs in, then the deviceId is tagged with the new userId and all pushes to that tag go to that device and the other user misses out, until they log in to that device again⌠OR log on with another device. This also covers the eventuality that they are using TWO or more devices at the same time and logged in on all - the push gets sent to ALL the devices they are logged in to.
Seems to work well and I havenât seen any downsides as yet.
I have the same issue. When I log in as different users on a single device, the token I get ( in this instance an android token) is always the same. This is how we register the user:
Ionic.io();
var push = new Ionic.Push({});
// this will give you a fresh user or the previously saved 'current user'
var user = Ionic.User.current();
// if the user doesn't have an id, you'll need to give it one.
if (!user.id) {
user.id = window.localStorage.getItem(API_BASE.client_id_key);
}
var callback = function (data) {
console.log('Registered token:', data.token);
push.addTokenToUser(user);
//persist the user
user.save();
}
push.register(callback);
Anyone found a solution yet if this is indeed a bug or not?
When you sending push notification then there is a big problem appear that How to identify which user is currently login on the specific device, if more then one user using same device. Here is the details answer: