Skip to content

Commit a69b596

Browse files
committed
Issue phonegap#188: Use Google's InstanceID API
Remove gcm.jar Implement GCM Clould Messaging Implement Token Refresh Listener Update Android compilation instructions
1 parent 57a49ea commit a69b596

File tree

8 files changed

+111
-83
lines changed

8 files changed

+111
-83
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ Note: version 1.3.0 of this plugin begins to use Gradle to install the Android S
197197

198198
### Compiling
199199

200-
As of version 1.3.0 the plugin has been switched to using Gradle/Maven for building. You will need to ensure that you have installed the Android Support Library version 23 or greater and Android Support Repository version 20 or greater.
200+
As of version 1.3.0 the plugin has been switched to using Gradle/Maven for building. You will need to ensure that you have installed the Android Support Library version 23 or greater, Android Support Repository version 20 or greater, Google Play Services version 27 or greater and Google Repository version 22 or greater.
201201

202202
![android support library](https://cloud.githubusercontent.com/assets/353180/10230226/0627931e-684a-11e5-9a6b-72d72997f655.png)
203203

plugin.xml

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
</config-file>
3838

3939
<config-file target="AndroidManifest.xml" parent="/manifest">
40+
<uses-permission android:name="android.permission.INTERNET" />
4041
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
4142
<uses-permission android:name="android.permission.WAKE_LOCK" />
4243
<uses-permission android:name="android.permission.VIBRATE"/>
@@ -47,23 +48,39 @@
4748

4849
<config-file target="AndroidManifest.xml" parent="/manifest/application">
4950
<activity android:name="com.adobe.phonegap.push.PushHandlerActivity" android:exported="true"/>
50-
<receiver android:name="com.adobe.phonegap.push.CordovaGCMBroadcastReceiver" android:permission="com.google.android.c2dm.permission.SEND" >
51-
<intent-filter>
52-
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
53-
<action android:name="com.google.android.c2dm.intent.REGISTRATION" />
54-
<category android:name="$PACKAGE_NAME" />
55-
</intent-filter>
56-
</receiver>
57-
<service android:name="com.adobe.phonegap.push.GCMIntentService" />
51+
52+
<receiver
53+
android:name="com.google.android.gms.gcm.GcmReceiver"
54+
android:exported="true"
55+
android:permission="com.google.android.c2dm.permission.SEND" >
56+
<intent-filter>
57+
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
58+
<category android:name="com.example.gcm" />
59+
</intent-filter>
60+
</receiver>
61+
<service
62+
android:name="com.adobe.phonegap.push.GCMIntentService"
63+
android:exported="false" >
64+
<intent-filter>
65+
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
66+
</intent-filter>
67+
</service>
68+
<service
69+
android:name="com.adobe.phonegap.push.PushInstanceIDListenerService"
70+
android:exported="false">
71+
<intent-filter>
72+
<action android:name="com.google.android.gms.iid.InstanceID"/>
73+
</intent-filter>
74+
</service>
5875
</config-file>
5976

6077
<framework src="com.android.support:support-v13:23+" />
78+
<framework src="com.google.android.gms:play-services:+" />
6179

62-
<source-file src="src/android/libs/gcm.jar" target-dir="libs/" />
63-
<source-file src="src/android/com/adobe/phonegap/push/CordovaGCMBroadcastReceiver.java" target-dir="src/com/adobe/phonegap/push/" />
6480
<source-file src="src/android/com/adobe/phonegap/push/GCMIntentService.java" target-dir="src/com/adobe/phonegap/push/" />
6581
<source-file src="src/android/com/adobe/phonegap/push/PushConstants.java" target-dir="src/com/adobe/phonegap/push/" />
6682
<source-file src="src/android/com/adobe/phonegap/push/PushHandlerActivity.java" target-dir="src/com/adobe/phonegap/push/" />
83+
<source-file src="src/android/com/adobe/phonegap/push/PushInstanceIDListenerService.java" target-dir="src/com/adobe/phonegap/push/" />
6784
<source-file src="src/android/com/adobe/phonegap/push/PushPlugin.java" target-dir="src/com/adobe/phonegap/push/" />
6885

6986
</platform>

src/android/com/adobe/phonegap/push/CordovaGCMBroadcastReceiver.java

Lines changed: 0 additions & 19 deletions
This file was deleted.

src/android/com/adobe/phonegap/push/GCMIntentService.java

Lines changed: 7 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
import android.text.Html;
2020
import android.util.Log;
2121

22-
import com.google.android.gcm.GCMBaseIntentService;
22+
import com.google.android.gms.gcm.GcmListenerService;
2323

2424
import org.json.JSONArray;
2525
import org.json.JSONException;
@@ -34,7 +34,7 @@
3434
import java.util.Random;
3535

3636
@SuppressLint("NewApi")
37-
public class GCMIntentService extends GCMBaseIntentService implements PushConstants {
37+
public class GCMIntentService extends GcmListenerService implements PushConstants {
3838

3939
private static final String LOG_TAG = "PushPlugin_GCMIntentService";
4040
private static HashMap<Integer, ArrayList<String>> messageMap = new HashMap<Integer, ArrayList<String>>();
@@ -53,42 +53,13 @@ public void setNotification(int notId, String message){
5353
}
5454
}
5555

56-
public GCMIntentService() {
57-
super("GCMIntentService");
58-
}
59-
6056
@Override
61-
public void onRegistered(Context context, String regId) {
62-
63-
Log.v(LOG_TAG, "onRegistered: " + regId);
64-
65-
try {
66-
JSONObject json = new JSONObject().put(REGISTRATION_ID, regId);
67-
68-
Log.v(LOG_TAG, "onRegistered: " + json.toString());
69-
70-
PushPlugin.sendEvent( json );
71-
}
72-
catch(JSONException e) {
73-
// No message to the user is sent, JSON failed
74-
Log.e(LOG_TAG, "onRegistered: JSON exception");
75-
}
76-
}
57+
public void onMessageReceived(String from, Bundle extras) {
58+
Log.d(LOG_TAG, "onMessage - from: " + from);
7759

78-
@Override
79-
public void onUnregistered(Context context, String regId) {
80-
Log.d(LOG_TAG, "onUnregistered - regId: " + regId);
81-
}
82-
83-
@Override
84-
protected void onMessage(Context context, Intent intent) {
85-
Log.d(LOG_TAG, "onMessage - context: " + context);
86-
87-
// Extract the payload from the message
88-
Bundle extras = intent.getExtras();
8960
if (extras != null) {
9061

91-
SharedPreferences prefs = context.getSharedPreferences(PushPlugin.COM_ADOBE_PHONEGAP_PUSH, Context.MODE_PRIVATE);
62+
SharedPreferences prefs = getApplicationContext().getSharedPreferences(PushPlugin.COM_ADOBE_PHONEGAP_PUSH, Context.MODE_PRIVATE);
9263
boolean forceShow = prefs.getBoolean(FORCE_SHOW, false);
9364

9465
// if we are in the foreground and forceShow is `false` only send data
@@ -100,13 +71,13 @@ protected void onMessage(Context context, Intent intent) {
10071
else if (forceShow && PushPlugin.isInForeground()) {
10172
extras.putBoolean(FOREGROUND, true);
10273

103-
showNotificationIfPossible(context, extras);
74+
showNotificationIfPossible(getApplicationContext(), extras);
10475
}
10576
// if we are not in the foreground always send notification if the data has at least a message or title
10677
else {
10778
extras.putBoolean(FOREGROUND, false);
10879

109-
showNotificationIfPossible(context, extras);
80+
showNotificationIfPossible(getApplicationContext(), extras);
11081
}
11182
}
11283
}
@@ -521,15 +492,6 @@ private static String getAppName(Context context) {
521492
return (String)appName;
522493
}
523494

524-
@Override
525-
public void onError(Context context, String errorId) {
526-
Log.e(LOG_TAG, "onError - errorId: " + errorId);
527-
// if we are in the foreground, just send the error
528-
if (PushPlugin.isInForeground()) {
529-
PushPlugin.sendError(errorId);
530-
}
531-
}
532-
533495
private int parseInt(String value, Bundle extras) {
534496
int retval = 0;
535497

src/android/com/adobe/phonegap/push/PushConstants.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,5 @@ public interface PushConstants {
4242
public static final String FROM = "from";
4343
public static final String COLLAPSE_KEY = "collapse_key";
4444
public static final String FORCE_SHOW = "forceShow";
45+
public static final String GCM = "GCM";
4546
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.adobe.phonegap.push;
2+
3+
import android.content.Context;
4+
import android.content.SharedPreferences;
5+
import android.util.Log;
6+
7+
import com.google.android.gms.iid.InstanceID;
8+
import com.google.android.gms.iid.InstanceIDListenerService;
9+
10+
import org.json.JSONException;
11+
12+
import java.io.IOException;
13+
14+
public class PushInstanceIDListenerService extends InstanceIDListenerService implements PushConstants {
15+
public static final String LOG_TAG = "PushPlugin_PushInstanceIDListenerService";
16+
17+
public void onTokenRefresh() {
18+
// re-register
19+
SharedPreferences sharedPref = getApplicationContext().getSharedPreferences(COM_ADOBE_PHONEGAP_PUSH, Context.MODE_PRIVATE);
20+
String senderID = sharedPref.getString(SENDER_ID, "");
21+
if (!"".equals(senderID)) {
22+
try {
23+
String token = InstanceID.getInstance(getApplicationContext()).getToken(senderID, GCM);
24+
25+
// save new token
26+
SharedPreferences.Editor editor = sharedPref.edit();
27+
editor.putString(REGISTRATION_ID, token);
28+
editor.commit();
29+
} catch (IOException e) {
30+
Log.e(LOG_TAG, e.getLocalizedMessage(), e);
31+
}
32+
33+
}
34+
}
35+
}

src/android/com/adobe/phonegap/push/PushPlugin.java

Lines changed: 40 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import android.os.Bundle;
77
import android.util.Log;
88

9-
import com.google.android.gcm.GCMRegistrar;
9+
import com.google.android.gms.iid.InstanceID;
1010

1111
import org.apache.cordova.CallbackContext;
1212
import org.apache.cordova.CordovaInterface;
@@ -17,6 +17,7 @@
1717
import org.json.JSONException;
1818
import org.json.JSONObject;
1919

20+
import java.io.IOException;
2021
import java.util.Iterator;
2122

2223
public class PushPlugin extends CordovaPlugin implements PushConstants {
@@ -48,24 +49,49 @@ public void run() {
4849
JSONObject jo = null;
4950

5051
Log.v(LOG_TAG, "execute: data=" + data.toString());
52+
SharedPreferences sharedPref = getApplicationContext().getSharedPreferences(COM_ADOBE_PHONEGAP_PUSH, Context.MODE_PRIVATE);
53+
String token = null;
54+
String senderID = null;
5155

5256
try {
5357
jo = data.getJSONObject(0).getJSONObject(ANDROID);
5458

5559
Log.v(LOG_TAG, "execute: jo=" + jo.toString());
5660

57-
String senderID = jo.getString(SENDER_ID);
61+
senderID = jo.getString(SENDER_ID);
5862

5963
Log.v(LOG_TAG, "execute: senderID=" + senderID);
6064

61-
GCMRegistrar.register(getApplicationContext(), senderID);
65+
String savedSenderID = sharedPref.getString(SENDER_ID, "");
66+
String savedRegID = sharedPref.getString(REGISTRATION_ID, "");
67+
68+
// first time run get new token
69+
if ("".equals(savedSenderID) && "".equals(savedRegID)) {
70+
token = InstanceID.getInstance(getApplicationContext()).getToken(senderID, GCM);
71+
}
72+
// new sender ID, re-register
73+
else if (!savedSenderID.equals(senderID)) {
74+
token = InstanceID.getInstance(getApplicationContext()).getToken(senderID, GCM);
75+
}
76+
// use the saved one
77+
else {
78+
token = sharedPref.getString(REGISTRATION_ID, "");
79+
}
80+
81+
JSONObject json = new JSONObject().put(REGISTRATION_ID, token);
82+
83+
Log.v(LOG_TAG, "onRegistered: " + json.toString());
84+
85+
PushPlugin.sendEvent( json );
6286
} catch (JSONException e) {
6387
Log.e(LOG_TAG, "execute: Got JSON Exception " + e.getMessage());
6488
callbackContext.error(e.getMessage());
89+
} catch (IOException e) {
90+
Log.e(LOG_TAG, "execute: Got JSON Exception " + e.getMessage());
91+
callbackContext.error(e.getMessage());
6592
}
6693

6794
if (jo != null) {
68-
SharedPreferences sharedPref = getApplicationContext().getSharedPreferences(COM_ADOBE_PHONEGAP_PUSH, Context.MODE_PRIVATE);
6995
SharedPreferences.Editor editor = sharedPref.edit();
7096
try {
7197
editor.putString(ICON, jo.getString(ICON));
@@ -81,6 +107,8 @@ public void run() {
81107
editor.putBoolean(VIBRATE, jo.optBoolean(VIBRATE, true));
82108
editor.putBoolean(CLEAR_NOTIFICATIONS, jo.optBoolean(CLEAR_NOTIFICATIONS, true));
83109
editor.putBoolean(FORCE_SHOW, jo.optBoolean(FORCE_SHOW, false));
110+
editor.putString(SENDER_ID, senderID);
111+
editor.putString(REGISTRATION_ID, token);
84112
editor.commit();
85113
}
86114

@@ -94,11 +122,15 @@ public void run() {
94122
} else if (UNREGISTER.equals(action)) {
95123
cordova.getThreadPool().execute(new Runnable() {
96124
public void run() {
97-
GCMRegistrar.unregister(getApplicationContext());
98-
99-
Log.v(LOG_TAG, "UNREGISTER");
100-
callbackContext.success();
125+
try {
126+
InstanceID.getInstance(getApplicationContext()).deleteInstanceID();
127+
Log.v(LOG_TAG, "UNREGISTER");
128+
callbackContext.success();
129+
} catch (IOException e) {
130+
Log.e(LOG_TAG, "execute: Got JSON Exception " + e.getMessage());
131+
callbackContext.error(e.getMessage());
101132
}
133+
}
102134
});
103135
} else {
104136
Log.e(LOG_TAG, "Invalid action : " + action);

src/android/libs/gcm.jar

-13.3 KB
Binary file not shown.

0 commit comments

Comments
 (0)