Similar question here: jQueryMobile, Phonegap and Device Token – iOS
The scenario is, I have this PhoneGap web based application, and the native iOS help me registered the device on APN and I received the device token in my server database.
Question 1: How do you associate a registered user (through the UIWebView) to this device token using PhoneGap?
- What’s in my mind now is to write a custom plugin and pass the device token along during user registration. Is there any better alternative?
Question 2: Since device_token can be changed from time to time, how should I re-link this user to this device_token?
- Perhaps whenever the user login, I do a window.plugins.PluginName.getDeviceToken and sync it?
{user_id:123, old_device_token: 'xxxx..', new_device_token: 'xxx...'}?
Fyi, this application is built for an event and the client has requested people to people messaging on this mobile app. How do you push a new message notification to “John Doe” when he received a message from his friend? – Question is how to link “John Doe” to a specific device_token?
This could not be too iOS specific as this application has to be deployed on Android as well (C2DM).
Any help is welcomed!
Edit: Possible solution?
Restless research emerges this possible solution:
- [Native] Application started – APN registration started and device_token is received
- [Native] Store this device_token into local store (CoreData/SqlLite or Property Lists?) and send it to server to do device_token registration
- [WebView] Whenever a user is login or signup, this device_token will be queried through PhoneGap, hashed and sent over to the server to do a sign in, comparison and link up.
Any unforeseen scenario is problematic?
EDIT: Answer
I have my complete working solution posted in the answer. Check it out here: https://stackoverflow.com/a/9628592/534862
Ok, I finally made a plugin that seems to work
1 – Make sure your PhoneGap Xcode project has been updated for the iOS 4 SDK.
2 – Create a PushToken folder in your Plugins folder add the following PushToken.m and PushToken.h files to it and then drag the folder to the Plugin folder in XCode, using “Create groups for any added folders”
3 – Add the PushToken.js files to your www folder on disk, and add reference(s) to the .js files as tags in your html file(s)
4 – Add new entry with key PushToken and string value PushToken to Plugins in PhoneGap.plist
PushToken.h
PushToken.m
PushToken.js
How to use
AppDelegate.h
AppDelegate.m
I made it work on PhoneGap 1.1
Hope this helps