-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathPushPlugin.java
More file actions
163 lines (99 loc) · 5.15 KB
/
PushPlugin.java
File metadata and controls
163 lines (99 loc) · 5.15 KB
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
package com.plugin.GCM;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.util.Log;
import org.apache.cordova.api.CallbackContext;
import org.apache.cordova.api.CordovaPlugin;
import org.apache.cordova.CordovaWebView;
import com.google.android.gcm.*;
/**
* @author awysocki
*/
public class PushPlugin extends CordovaPlugin {
public static final String ME = "PushPlugin";
public static final String REGISTER = "register";
public static final String UNREGISTER = "unregister";
public static final String EXIT = "exit";
private static CordovaPlugin gwebView;
private static CordovaWebView staticWebView;
private static String gECB;
private static String gSenderID;
@SuppressWarnings("deprecation")
@Override
public boolean execute(String action, JSONArray data, CallbackContext callbak) {
Log.v(ME + ":execute", "action=" + action);
JSONObject result;
if (REGISTER.equals(action)) {
Log.v(ME + ":execute", "data=" + data.toString());
try {
JSONObject jo = new JSONObject(data.toString().substring(1, data.toString().length() - 1));
gwebView = this;
staticWebView = webView;
Log.v(ME + ":execute", "jo=" + jo.toString());
gECB = (String) jo.get("ecb");
gSenderID = (String) jo.get("senderID");
Log.v(ME + ":execute", "ECB=" + gECB + " senderID=" + gSenderID);
GCMRegistrar.register(cordova.getActivity(), gSenderID);
Log.v(ME + ":execute", "GCMRegistrar.register called ");
callbak.success(jo);
return true;
} catch (JSONException e) {
Log.e(ME, "Got JSON Exception " + e.getMessage());
callbak.error(e.getMessage());
}
// if a notification was touched while we were completely exited, process it now
try {
BufferedReader inputReader = new BufferedReader(new InputStreamReader(cordova.getActivity().getApplicationContext().openFileInput("cached_payload")));
String inputString;
StringBuffer stringBuffer = new StringBuffer();
while ((inputString = inputReader.readLine()) != null) {
stringBuffer.append(inputString);
}
// surface the cached payload
JSONObject jsonObj = new JSONObject(stringBuffer.toString());
sendJavascript(jsonObj);
cordova.getActivity().getApplicationContext().getFileStreamPath("cached_payload").delete();
callbak.success(jsonObj);
return true;
} catch (FileNotFoundException fnf) {
Log.e("REGISTER", fnf.getMessage());
callbak.error(fnf.getMessage());
} catch (IOException io) {
io.printStackTrace();
callbak.error(io.getMessage());
} catch (JSONException j) {
j.printStackTrace();
callbak.error(j.getMessage());
}
PushHandlerActivity.EXITED = false;
} else if (UNREGISTER.equals(action)) {
GCMRegistrar.unregister(this.cordova.getActivity());
Log.v(ME + ":" + UNREGISTER, "GCMRegistrar.unregister called ");
callbak.success("Unregistration Success");
return true;
} else {
Log.e(ME, "Invalid action : " + action);
callbak.error("Unregistration Failed");
return false;
}
return false;
}
public static void sendJavascript(JSONObject _json) {
String _d = "javascript:" + gECB + "(" + _json.toString() + ")";
Log.v(ME + ":sendJavascript", _d);
if (gECB != null) {
staticWebView.sendJavascript(_d);
}
}
public void onDestroy() {
super.onDestroy();
// let the service know we are exiting so it can cache the next notification payload.
PushHandlerActivity.EXITED = true;
GCMRegistrar.onDestroy(cordova.getActivity());
}
}