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

Commit

Permalink
Added partial Android FCM support (#975)
Browse files Browse the repository at this point in the history
* Modified plugin.xml to include FCM changes

* Plugin.xml changes for FCM

* Java changes for FCM

* Increased plugin version to 2.0.0

* Increased plugin version in package.json

* Added topic subscription/unsubscription

* Removed some empty lines
  • Loading branch information
jcesarmobile authored and macdonst committed Apr 27, 2017
1 parent cebb640 commit c119bf0
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 49 deletions.
33 changes: 20 additions & 13 deletions plugin.xml
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<plugin xmlns="http://www.phonegap.com/ns/plugins/1.0" xmlns:android="http://schemas.android.com/apk/res/android" 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.10.2">
<?xml version="1.0" encoding="UTF-8"?>
<plugin xmlns="http://www.phonegap.com/ns/plugins/1.0"
xmlns:android="http://schemas.android.com/apk/res/android"
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="2.0.0">

<name>PushPlugin</name>
<description>
This plugin allows your application to receive push notifications on Android, iOS and Windows devices.
Android uses Google Cloud Messaging.
This plugin allows your application to receive push notifications on Android, iOS and Windows devices.
Android uses Firebase Cloud Messaging.
iOS uses Apple APNS Notifications.
Windows uses Microsoft WNS Notifications.
</description>
</description>

<license>MIT</license>
<js-module src="www/push.js" name="PushNotification">
<clobbers target="PushNotification"/>
Expand Down Expand Up @@ -40,34 +47,34 @@
<config-file target="AndroidManifest.xml" parent="/manifest/application">
<activity android:name="com.adobe.phonegap.push.PushHandlerActivity" android:exported="true" android:permission="${applicationId}.permission.PushHandlerActivity"/>
<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">
<!-- 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>
</receiver -->
<service android:name="com.adobe.phonegap.push.GCMIntentService" android:exported="false">
<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">
<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 android:name="com.adobe.phonegap.push.RegistrationIntentService" android:exported="false"/ -->
</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:10.2+"/>
<framework src="com.google.firebase:firebase-messaging:9.0.0" />
<framework src="me.leolin:ShortcutBadger:1.1.14@aar"/>
<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/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/"/>
</platform>
Expand Down Expand Up @@ -120,4 +127,4 @@
<preference name="WindowsToastCapable" value="true"/>
</config-file>
</platform>
</plugin>
</plugin>
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 @@ -28,7 +28,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 @@ -41,12 +42,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 @@ -64,8 +66,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());
}

if (extras != null && isAvailableSender(from)) {
Context applicationContext = getApplicationContext();
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.
//sendRegistrationToServer(refreshedToken);
}
}
38 changes: 17 additions & 21 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 @@ -70,10 +70,10 @@ public void run() {

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

registration_id = InstanceID.getInstance(getApplicationContext()).getToken(senderID, GCM);
token = FirebaseInstanceId.getInstance().getToken();

if (!"".equals(registration_id)) {
JSONObject json = new JSONObject().put(REGISTRATION_ID, registration_id);
if (!"".equals(token)) {
JSONObject json = new JSONObject().put(REGISTRATION_ID, token);

Log.v(LOG_TAG, "onRegistered: " + json.toString());

Expand All @@ -82,15 +82,12 @@ public void run() {

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 @@ -144,7 +141,7 @@ public void run() {
if (topics != null && !"".equals(registration_id)) {
unsubscribeFromTopics(topics, registration_id);
} else {
InstanceID.getInstance(getApplicationContext()).deleteInstanceID();
FirebaseInstanceId.getInstance().deleteInstanceId();
Log.v(LOG_TAG, "UNREGISTER");

// Remove shared prefs
Expand Down Expand Up @@ -288,7 +285,7 @@ public static int getApplicationIconBadgeNumber(Context context){
SharedPreferences settings = context.getSharedPreferences(BADGE, Context.MODE_PRIVATE);
return settings.getInt(BADGE, 0);
}

/*
* Sets badge count on application icon and in SharedPreferences
*/
Expand All @@ -298,7 +295,7 @@ public static void setApplicationIconBadgeNumber(Context context, int badgeCount
}else{
ShortcutBadger.removeCount(context);
}

SharedPreferences.Editor editor = context.getSharedPreferences(BADGE, Context.MODE_PRIVATE).edit();
editor.putInt(BADGE, Math.max(badgeCount, 0));
editor.apply();
Expand Down Expand Up @@ -365,7 +362,10 @@ private void subscribeToTopics(JSONArray topics, String registrationToken) throw
String topic = null;
for (int i=0; i<topics.length(); i++) {
topic = topics.optString(i, null);
subscribeToTopic(topic, registrationToken);
if (topic != null) {
Log.d(LOG_TAG, "Subscribing to topic: " + topic);
FirebaseMessaging.getInstance().subscribeToTopic(topic);
}
}
}
}
Expand All @@ -389,14 +389,10 @@ 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, getTopicPath(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

0 comments on commit c119bf0

Please sign in to comment.