1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
|
Phonegap Parse.com Plugin
=========================
Phonegap/Cordova 3.0+ plugin for Parse.com push service
Using [Parse.com's](http://parse.com) REST API for push requires the installation id, which isn't available in JS
This plugin exposes native API push services to JS:
* <a href="https://www.parse.com/docs/android/api/com/parse/ParseInstallation.html#getInstallationId()">getInstallationId</a>
* <a href="https://www.parse.com/docs/android/api/com/parse/PushService.html#getSubscriptions(android.content.Context)">getSubscriptions</a>
* <a href="https://www.parse.com/docs/android/api/com/parse/PushService.html#subscribe(android.content.Context, java.lang.String, java.lang.Class, int)">subscribe</a>
* <a href="https://www.parse.com/docs/android/api/com/parse/PushService.html#unsubscribe(android.content.Context, java.lang.String)">unsubscribe</a>
* <a href="https://parse.com/docs/osx/api/Classes/PFAnalytics.html#//api/name/trackEvent:dimensions:">trackEvent (iOS only)</a>
As well as other utility methods:
* registerCallback: allows us to get the notification back in javascript
* resetBadge: resets the badge to 0 (iOS only) --this can also be accomplished by setting the badge to 0 in the _Installation table using the <a href="https://parse.com/docs/rest/guide#objects-updating-objects">Parse REST API</a>
Installation
------------
Pick one of these two commands:
```
phonegap local plugin add https://github.com/grrrian/phonegap-parse-plugin --variable APP_ID=PARSE_APP_ID --variable CLIENT_KEY=PARSE_CLIENT_KEY
cordova plugin add https://github.com/grrrian/phonegap-parse-plugin --variable APP_ID=PARSE_APP_ID --variable CLIENT_KEY=PARSE_CLIENT_KEY
```
Initial Setup
-------------
A parsePlugin variable is defined globally (e.g. $window.parsePlugin).
Once the device is ready (see: http://docs.phonegap.com/en/4.0.0/cordova_events_events.md.html#deviceready), call ```parsePlugin.initialize()```. This will register the device with Parse, you should see this reflected in your Parse control panel. After this runs you probably want to save the installationID somewhere, and perhaps subscribe the user to a few channels. Here is a contrived example.
(Note: When using Windows Phone, clientKey must be your .NET client key from Parse. So you will need to set this based on platform i.e. if( window.device.platform == "Win32NT"))
```
parsePlugin.initialize(appId, clientKey, function() {
parsePlugin.subscribe('SampleChannel', function() {
parsePlugin.getInstallationId(function(id) {
/**
* Now you can construct an object and save it to your own services, or Parse, and correlate users to parse installations
*
var install_data = {
installation_id: id,
channels: ['SampleChannel']
}
*
*/
}, function(e) {
alert('error');
});
}, function(e) {
alert('error');
});
}, function(e) {
alert('error');
});
```
Alternatively, we can store the user in the installation table and use queries to push notifications.
```
// on sign in, add the user pointer to the Installation
parsePlugin.initialize(appId, clientKey, function() {
parsePlugin.getInstallationObjectId( function(id) {
// Success! You can now use Parse REST API to modify the Installation
// see: https://parse.com/docs/rest/guide#objects for more info
console.log("installation object id: " + id)
}, function(error) {
console.error('Error getting installation object id. ' + error);
});
}, function(e) {
alert('Error initializing.');
});
```
To receive notification callbacks, on device ready:
```
parsePlugin.registerCallback('onNotification', function() {
window.onNotification = function(pnObj) {
alert('We received this push notification: ' + JSON.stringify(pnObj));
if (pnObj.receivedInForeground === false) {
// TODO: route the user to the uri in pnObj
}
};
}, function(error) {
console.error(error);
});
```
Usage
-----
```
<script type="text/javascript">
parsePlugin.initialize(appId, clientKey, function() {
alert('success');
}, function(e) {
alert('error');
});
parsePlugin.getInstallationId(function(id) {
alert(id);
}, function(e) {
alert('error');
});
parsePlugin.getSubscriptions(function(subscriptions) {
alert(subscriptions);
}, function(e) {
alert('error');
});
parsePlugin.subscribe('SampleChannel', function() {
alert('OK');
}, function(e) {
alert('error');
});
parsePlugin.unsubscribe('SampleChannel', function(msg) {
alert('OK');
}, function(e) {
alert('error');
});
parsePlugin.resetBadge(function() {
alert('OK');
}, function(e) {
alert('error');
});
parsePlugin.trackEvent(function(name, dimensions) {
alert('OK');
}, function(e) {
alert('error');
});
</script>
```
Quirks
------
### Android
Parse needs to be initialized once in the `onCreate` method of your application class using the `initializeParseWithApplication` method.
If you don’t have an application class (which is most likely the case for a Cordova app), you can create one using this template:
```java
package my.package.namespace;
import android.app.Application;
import org.apache.cordova.core.ParsePlugin;
public class App extends Application {
@Override
public void onCreate() {
super.onCreate();
ParsePlugin.initializeParseWithApplication(this);
}
}
```
And add your application name to `AndroidManifest.xml`:
```xml
<application android:name="my.package.namespace.App" ... >...</application>
```
Compatibility
-------------
- Phonegap/Cordova > 3.0
- iOS > 7 (on versions lower than 7, you will not receive push notification callbacks when the app is running in foreground)
- Android > 2.3
|