Skip to content
This repository has been archived by the owner on Sep 4, 2020. It is now read-only.

Added partial Android FCM support #975

Merged
merged 7 commits into from
Jun 10, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "phonegap-plugin-push",
"description": "Register and receive push notifications.",
"version": "1.7.0",
"version": "2.0.0",
"homepage": "http://github.com/phonegap/phonegap-plugin-push#readme",
"repository": {
"type": "git",
Expand Down
43 changes: 11 additions & 32 deletions plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
xmlns:amazon="http://schemas.android.com/apk/lib/com.amazon.device.ads"
xmlns:rim="http://www.blackberry.com/ns/widgets"
id="phonegap-plugin-push"
version="1.7.0">
version="2.0.0">

<name>PushPlugin</name>
<description>
Expand Down Expand Up @@ -41,60 +41,39 @@
<string name="google_app_id">$SENDER_ID</string>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jcesarmobile do we need this anymore? Is it just contained in the google-services.json file?

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@macdonst yes, the new google-services.json file contains that, and much more

</config-file>

<config-file target="AndroidManifest.xml" parent="/manifest">
<config-file target="AndroidManifest.xml" parent="/manifest">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.VIBRATE"/>
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<permission android:name="${applicationId}.permission.C2D_MESSAGE" android:protectionLevel="signature" />
<uses-permission android:name="${applicationId}.permission.C2D_MESSAGE" />
</config-file>

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.VIBRATE"/>
</config-file>

<config-file target="AndroidManifest.xml" parent="/manifest/application">
<activity android:name="com.adobe.phonegap.push.PushHandlerActivity" android:exported="true"/>
<receiver android:name="com.adobe.phonegap.push.BackgroundActionButtonHandler"/>

<receiver
android:name="com.google.android.gms.gcm.GcmReceiver"
android:exported="true"
android:permission="com.google.android.c2dm.permission.SEND" >
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<category android:name="${applicationId}" />
</intent-filter>
</receiver>
<service
android:name="com.adobe.phonegap.push.GCMIntentService"
android:exported="false" >
android:name="com.adobe.phonegap.push.GCMIntentService">
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
<service
android:name="com.adobe.phonegap.push.PushInstanceIDListenerService"
android:exported="false">
android:name="com.adobe.phonegap.push.PushInstanceIDListenerService">
<intent-filter>
<action android:name="com.google.android.gms.iid.InstanceID"/>
<action android:name="com.google.firebase.INSTANCE_ID_EVENT" />
</intent-filter>
</service>
<service
android:name="com.adobe.phonegap.push.RegistrationIntentService"
android:exported="false">
</service>
</config-file>

<framework src="push.gradle" custom="true" type="gradleReference" />
<framework src="com.android.support:support-v13:23+" />
<framework src="com.google.android.gms:play-services-gcm:+" />
<framework src="me.leolin:ShortcutBadger:1.1.4@aar" />
<framework src="com.google.firebase:firebase-messaging:9.0.0" />
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we make it 9+ to allow later versions of the lib?


<source-file src="src/android/com/adobe/phonegap/push/GCMIntentService.java" target-dir="src/com/adobe/phonegap/push/" />
<source-file src="src/android/com/adobe/phonegap/push/PushConstants.java" target-dir="src/com/adobe/phonegap/push/" />
<source-file src="src/android/com/adobe/phonegap/push/PushHandlerActivity.java" target-dir="src/com/adobe/phonegap/push/" />
<source-file src="src/android/com/adobe/phonegap/push/PushInstanceIDListenerService.java" target-dir="src/com/adobe/phonegap/push/" />
<source-file src="src/android/com/adobe/phonegap/push/PushPlugin.java" target-dir="src/com/adobe/phonegap/push/" />
<source-file src="src/android/com/adobe/phonegap/push/RegistrationIntentService.java" target-dir="src/com/adobe/phonegap/push/" />
<source-file src="src/android/com/adobe/phonegap/push/PermissionUtils.java" target-dir="src/com/adobe/phonegap/push/" />
<source-file src="src/android/com/adobe/phonegap/push/BackgroundActionButtonHandler.java" target-dir="src/com/adobe/phonegap/push/" />

Expand Down
23 changes: 18 additions & 5 deletions src/android/com/adobe/phonegap/push/GCMIntentService.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
import android.text.Spanned;
import android.util.Log;

import com.google.android.gms.gcm.GcmListenerService;
import com.google.firebase.messaging.FirebaseMessagingService;
import com.google.firebase.messaging.RemoteMessage;

import org.json.JSONArray;
import org.json.JSONException;
Expand All @@ -35,12 +36,13 @@
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Random;

@SuppressLint("NewApi")
public class GCMIntentService extends GcmListenerService implements PushConstants {
public class GCMIntentService extends FirebaseMessagingService implements PushConstants {

private static final String LOG_TAG = "PushPlugin_GCMIntentService";
private static final String LOG_TAG = "PushPlugin_FCMService";
private static HashMap<Integer, ArrayList<String>> messageMap = new HashMap<Integer, ArrayList<String>>();

public void setNotification(int notId, String message){
Expand All @@ -58,8 +60,19 @@ public void setNotification(int notId, String message){
}

@Override
public void onMessageReceived(String from, Bundle extras) {
Log.d(LOG_TAG, "onMessage - from: " + from);
public void onMessageReceived(RemoteMessage message){

Log.d(LOG_TAG, "onMessage - from: " + message.getFrom());

Bundle extras = new Bundle();

if (message.getNotification()!=null) {
extras.putString(TITLE,message.getNotification().getTitle());
extras.putString(MESSAGE,message.getNotification().getBody());
}
for (Map.Entry<String, String> entry : message.getData().entrySet()) {
extras.putString(entry.getKey(), entry.getValue());
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmmm....need to look into RemoteMessage some more but looks like a good idea to minimize changes for now.


if (extras != null) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,22 @@
import android.content.SharedPreferences;
import android.util.Log;

import com.google.android.gms.iid.InstanceID;
import com.google.android.gms.iid.InstanceIDListenerService;
import com.google.firebase.iid.FirebaseInstanceId;
import com.google.firebase.iid.FirebaseInstanceIdService;

import org.json.JSONException;

import java.io.IOException;

public class PushInstanceIDListenerService extends InstanceIDListenerService implements PushConstants {
public static final String LOG_TAG = "PushPlugin_PushInstanceIDListenerService";
public class PushInstanceIDListenerService extends FirebaseInstanceIdService implements PushConstants {
public static final String LOG_TAG = "PushPlugin_InsIdService";

@Override
public void onTokenRefresh() {
SharedPreferences sharedPref = getApplicationContext().getSharedPreferences(COM_ADOBE_PHONEGAP_PUSH, Context.MODE_PRIVATE);
String senderID = sharedPref.getString(SENDER_ID, "");
if (!"".equals(senderID)) {
Intent intent = new Intent(this, RegistrationIntentService.class);
startService(intent);
}
// Get updated InstanceID token.
String refreshedToken = FirebaseInstanceId.getInstance().getToken();
Log.d(LOG_TAG, "Refreshed token: " + refreshedToken);
// TODO: Implement this method to send any registration to your app's servers.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here we will need to add:

 // save new token
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString(REGISTRATION_ID, token);
editor.commit();

to save the new token or the PushPlugin.java code may try to load the old token. Also, we'll need to figure out a way to fire a registration event.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I use FirebaseInstanceId.getInstance().getToken() to get the token, this should return the current token, so I don't think we have to store it on the SharedPreferences anymore

//sendRegistrationToServer(refreshedToken);
}
}
52 changes: 14 additions & 38 deletions src/android/com/adobe/phonegap/push/PushPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
import android.os.Bundle;
import android.util.Log;

import com.google.android.gms.gcm.GcmPubSub;
import com.google.android.gms.iid.InstanceID;
import com.google.firebase.iid.FirebaseInstanceId;
import com.google.firebase.messaging.FirebaseMessaging;

import org.apache.cordova.CallbackContext;
import org.apache.cordova.CordovaInterface;
Expand Down Expand Up @@ -67,21 +67,7 @@ public void run() {

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

String savedSenderID = sharedPref.getString(SENDER_ID, "");
String savedRegID = sharedPref.getString(REGISTRATION_ID, "");

// first time run get new token
if ("".equals(savedRegID)) {
token = InstanceID.getInstance(getApplicationContext()).getToken(senderID, GCM);
}
// new sender ID, re-register
else if (!savedSenderID.equals(senderID)) {
token = InstanceID.getInstance(getApplicationContext()).getToken(senderID, GCM);
}
// use the saved one
else {
token = sharedPref.getString(REGISTRATION_ID, "");
}
token = FirebaseInstanceId.getInstance().getToken();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So if it never uses the SENDER_ID it must read it from the google_services.json file. We'll need to update the .init() JS code.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@macdonst the "google-services.json" file needs to go into the native /app folder (root).

Not really sure how to get that mapping, from an app to the plugin

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@matzew yeah, we may need to do some "resource file" tricks or update cordova-android to be support this use case as I'm seeing it more and more.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are three options :

  1. Apply the Project Number and Application IDs to firebase manually : https://gist.github.com/secondsun/dfd3a301a5cfbc34d74af18854276f1b
  2. Add to values.xml the following : gcm_defaultSenderId and google_app_id
  3. Include the plugin and the google-services.json file and let them do their magic.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. sounds more saine, staying w/ how it is supposed to work, no ?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. is Google's "Golden Scenario"

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, 3) would be better if we can deliver the file to the right location at compile time. If not we will have to go with 2) as plugin.xml can collect those values as variables and put them into values.xml.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On my sample project I have this string on res/values/strings.xml (created by the plugin) <string name="google_app_id">myAppId3333</string> and I'm able to receive push notifications sent from FCM console, but I have nothing for gcm_defaultSenderId anywhere

Copy link

@secondsun secondsun Jun 11, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The senderID is part of the app id, so Firebase probably only loads that
value once.

Summers Pittman


if (!"".equals(token)) {
JSONObject json = new JSONObject().put(REGISTRATION_ID, token);
Expand All @@ -93,15 +79,12 @@ else if (!savedSenderID.equals(senderID)) {

PushPlugin.sendEvent( json );
} else {
callbackContext.error("Empty registration ID received from GCM");
callbackContext.error("Empty registration ID received from FCM");
return;
}
} catch (JSONException e) {
Log.e(LOG_TAG, "execute: Got JSON Exception " + e.getMessage());
callbackContext.error(e.getMessage());
} catch (IOException e) {
Log.e(LOG_TAG, "execute: Got JSON Exception " + e.getMessage());
callbackContext.error(e.getMessage());
}

if (jo != null) {
Expand Down Expand Up @@ -142,7 +125,7 @@ public void run() {
if (topics != null && !"".equals(token)) {
unsubscribeFromTopics(topics, token);
} else {
InstanceID.getInstance(getApplicationContext()).deleteInstanceID();
FirebaseInstanceId.getInstance().deleteInstanceId();
Log.v(LOG_TAG, "UNREGISTER");

// Remove shared prefs
Expand Down Expand Up @@ -288,14 +271,10 @@ private void subscribeToTopics(JSONArray topics, String registrationToken) {
if (topics != null) {
String topic = null;
for (int i=0; i<topics.length(); i++) {
try {
topic = topics.optString(i, null);
if (topic != null) {
Log.d(LOG_TAG, "Subscribing to topic: " + topic);
GcmPubSub.getInstance(getApplicationContext()).subscribe(registrationToken, "/topics/" + topic, null);
}
} catch (IOException e) {
Log.e(LOG_TAG, "Failed to subscribe to topic: " + topic, e);
topic = topics.optString(i, null);
if (topic != null) {
Log.d(LOG_TAG, "Subscribing to topic: " + topic);
FirebaseMessaging.getInstance().subscribeToTopic(topic);
}
}
}
Expand All @@ -305,14 +284,11 @@ private void unsubscribeFromTopics(JSONArray topics, String registrationToken) {
if (topics != null) {
String topic = null;
for (int i=0; i<topics.length(); i++) {
try {
topic = topics.optString(i, null);
if (topic != null) {
Log.d(LOG_TAG, "Unsubscribing to topic: " + topic);
GcmPubSub.getInstance(getApplicationContext()).unsubscribe(registrationToken, "/topics/" + topic);
}
} catch (IOException e) {
Log.e(LOG_TAG, "Failed to unsubscribe to topic: " + topic, e);
topic = topics.optString(i, null);
if (topic != null) {
Log.d(LOG_TAG, "Unsubscribing to topic: " + topic);
FirebaseMessaging.getInstance().unsubscribeFromTopic(topic);

}
}
}
Expand Down
42 changes: 0 additions & 42 deletions src/android/com/adobe/phonegap/push/RegistrationIntentService.java

This file was deleted.