Skip to content

Commit

Permalink
fcm: fix with startup
Browse files Browse the repository at this point in the history
  • Loading branch information
hramovnik committed Jan 18, 2018
1 parent aebba95 commit b68a0c3
Show file tree
Hide file tree
Showing 12 changed files with 292 additions and 18 deletions.
16 changes: 16 additions & 0 deletions lib/commonAPI/coreapi/ext/shared/PushImpl.cpp
Expand Up @@ -30,6 +30,8 @@
#include "common/RhoStd.h"
#include "logging/RhoLog.h"
#include "sync/RhoconnectClientManager.h"
#include <queue>
#include <utility>

namespace rho {
namespace push {
Expand All @@ -43,6 +45,7 @@ CPushManager* CPushManager::getInstance()
//----------------------------------------------------------------------------------------------------------------------
class CPushSingleton: public CPushManager, public CPushFactoryBase {
VectorPtr<CPushClient*> m_clients;
std::queue<std::pair<String, String> > messageQueue;
DEFINE_LOGCLASS;
public:
virtual ~CPushSingleton()
Expand All @@ -55,6 +58,16 @@ class CPushSingleton: public CPushManager, public CPushFactoryBase {
{
m_clients.addElement(pClient);
LOG(INFO) + "New push client has added: " + pClient->getId();

std::queue<std::pair<String, String> > localQueue = messageQueue;
while(!messageQueue.empty()){
messageQueue.pop();
}
while(!localQueue.empty()){
callBack(localQueue.front().first, localQueue.front().second);
localQueue.pop();
}

}
void setDeviceId(const String& id, const String& deviceId);
void callBack(const String& id, const String& json);
Expand Down Expand Up @@ -116,9 +129,12 @@ void CPushSingleton::setDeviceId(const String& id, const String& deviceId)
//----------------------------------------------------------------------------------------------------------------------
void CPushSingleton::callBack(const String& id, const String& json)
{
LOG(INFO) + "FCM: calling callBack";
CPushClient* pClient = getClient(id);
if(pClient == 0)
{
messageQueue.push( std::pair<String,String>(id, json));
while (messageQueue.size() > 1024){messageQueue.pop();}
LOG(ERROR) + "CPushSingleton::callBack: Unknown push client: " + id;
}
else
Expand Down
10 changes: 7 additions & 3 deletions lib/extensions/fcm-push/ext.yml
Expand Up @@ -6,9 +6,13 @@ android:
- ext/android/ApplicationManifestAdds.erb
library_deps: [extras/google/google_play_services/libproject/google-play-services_lib]
source_list: ext/android/ext_java.files
maven_deps:
- 'com.google.firebase:firebase-messaging:11.0.2'

maven_deps: ['com.android.support:appcompat-v7:25.2.0']
maven_deps: ['com.google.android.gms:play-services:11.0.2']
maven_deps: ['com.google.android.gms:play-services-identity:11.0.2']
maven_deps: ['com.google.firebase:firebase-core:11.0.2']
maven_deps: ['com.google.firebase:firebase-config:11.0.2']
maven_deps: ['com.google.firebase:firebase-messaging:11.0.2']
adds: ext/android/res
libraries: ['fcm-push']
xml_api_paths: ext/fcm.xml
#will work with newer SDKs
1 change: 1 addition & 0 deletions lib/extensions/fcm-push/ext/android/AndroidManifest.rb
Expand Up @@ -4,3 +4,4 @@
generator.usesPermissions << 'android.permission.GET_ACCOUNTS'
generator.usesPermissions << 'android.permission.WAKE_LOCK'
generator.usesPermissions << 'android.permission.INTERNET'
generator.usesPermissions << 'android.permission.RECEIVE_BOOT_COMPLETED'
@@ -1,9 +1,15 @@
<service android:name="com.rhomobile.rhodes.fcm.FCMIntentService">
<intent-filter>
<intent-filter android:priority="900">
<action android:name="com.google.firebase.MESSAGING_EVENT"/>
</intent-filter>
</service>

<service android:exported="true" android:name="com.google.firebase.messaging.FirebaseMessagingService">
<intent-filter android:priority="-500">
<action android:name="com.google.firebase.MESSAGING_EVENT"/>
</intent-filter>
</service>

<service android:name="com.rhomobile.rhodes.fcm.FCMTokenRefresherService" android:exported="true">
<intent-filter>
<action android:name="com.google.firebase.INSTANCE_ID_EVENT"/>
Expand Down Expand Up @@ -34,5 +40,4 @@
</intent-filter>
</receiver>


<meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version"/>
3 changes: 2 additions & 1 deletion lib/extensions/fcm-push/ext/android/ext_java.files
@@ -1,3 +1,4 @@
ext/android/src/com/rhomobile/rhodes/fcm/FCMFacade.java
ext/android/src/com/rhomobile/rhodes/fcm/FCMIntentService.java
ext/android/src/com/rhomobile/rhodes/fcm/FCMTokenRefresherService.java
ext/android/src/com/rhomobile/rhodes/fcm/FCMTokenRefresherService.java
ext/android/src/com/rhomobile/rhodes/fcm/FCMListener.java
25 changes: 23 additions & 2 deletions lib/extensions/fcm-push/ext/android/jni/src/fcmpushclient.cpp
Expand Up @@ -65,7 +65,7 @@ String FcmPushClient::token = "";
FcmPushClient::FcmPushClient()
{
LOG(TRACE) + "FcmPushInit()";

canExecuteNotifications = false;
JNIEnv *env = jnienv();

static jclass cls = rho_find_class(env, s_FCM_FACADE_CLASS);
Expand Down Expand Up @@ -157,13 +157,17 @@ void FcmPushClient::startNotifications(CMethodResult& result)
{
LOG(TRACE) + "Start FCM push notifications";
m_oResult = result;
canExecuteNotifications = true;
executeCallBacks();
}

//----------------------------------------------------------------------------------------------------------------------
void FcmPushClient::stopNotifications(CMethodResult& result)
{
LOG(TRACE) + "Stop FCM push notifications";
canExecuteNotifications = false;
m_oResult = CMethodResult();

}

//----------------------------------------------------------------------------------------------------------------------
Expand All @@ -185,10 +189,27 @@ bool FcmPushClient::callBack(const String& json)
{
LOG(TRACE) + "FCM push notification: " + json;

m_oResult.setJSON(json);
if (canExecuteNotifications){
m_oResult.setJSON(json);
}else{
if (callBacksQueue.size() < 1024){
callBacksQueue.push(json);
}
}

return true;
}

void FcmPushClient::executeCallBacks()
{

while (!callBacksQueue.empty()){
callBack(callBacksQueue.front());
callBacksQueue.pop();
}

}


}}

5 changes: 4 additions & 1 deletion lib/extensions/fcm-push/ext/android/jni/src/fcmpushclient.h
Expand Up @@ -32,7 +32,7 @@

#include "logging/RhoLog.h"
#include "common/RhoStd.h"

#include <queue>

namespace rho { namespace fcm {

Expand All @@ -50,6 +50,9 @@ class FcmPushClient: public push::CPushClient
void setPropertyFromMethod(const char* methodName, CMethodResult &result);
void refreshToken();

std::queue<String> callBacksQueue;
void executeCallBacks();
bool canExecuteNotifications;
public:
DEFINE_LOGCLASS;

Expand Down
Expand Up @@ -37,14 +37,19 @@
import com.google.firebase.FirebaseApp;
import com.google.firebase.FirebaseOptions;
import com.google.firebase.messaging.FirebaseMessaging;
import com.google.android.gms.common.ConnectionResult;
import com.google.firebase.iid.FirebaseInstanceId;
import com.rhomobile.rhodes.R;
import android.util.Log;
import java.lang.Throwable;
import com.rhomobile.rhodes.extmanager.AbstractRhoListener;
import com.rhomobile.rhodes.RhodesActivity;
import com.rhomobile.rhodes.RhodesService;
import com.rhomobile.rhodes.extmanager.RhoExtManager;
import com.rhomobile.rhodes.extmanager.RhoExtManagerImpl;
import com.rhomobile.rhodes.extmanager.IRhoExtManager;

public final class FCMFacade {
private static final String TAG = FCMFacade.class.getSimpleName();
public static final FCMListener listener = FCMListener.getInstance();

static final String FCM_PUSH_CLIENT = "fcm";
static String clientToken = "";
Expand Down Expand Up @@ -74,7 +79,8 @@ public static String google_api_key(){
*/

public static void initFireBase() {
Log.d(TAG, "FCM: Send FCM push register req");

Logger.W(TAG, "FCM: Send FCM push register req");

try{
FirebaseApp.getInstance();
Expand Down Expand Up @@ -110,24 +116,27 @@ public static void initFireBase() {
}
}

refreshToken();
refreshToken();



}
public static void refreshToken(){
try{
Log.d(TAG, "FCM: registation of application");
Logger.T(TAG, "FCM: registation of application");
clientToken = "";
clientToken = FirebaseInstanceId.getInstance().getToken();
if ((clientToken != "")&&(clientToken != null)){
PushContract.handleRegistration(ContextFactory.getContext(), clientToken, FCMFacade.FCM_PUSH_CLIENT);
Log.d(TAG, "FCM: registation successfully");
Logger.T(TAG, "FCM: registation successfully");
}else{
clientToken = "";
Log.d(TAG, "FCM: can't get token, try to refresh later");
Logger.T(TAG, "FCM: can't get token, try to refresh later");
}
}catch(Exception exc){
Log.d(TAG, "FCM: can't handle registation");
Logger.T(TAG, "FCM: can't handle registation");
}
Log.d(TAG, "FCM: token = " + clientToken);
Logger.T(TAG, "FCM: token = " + clientToken);
}

}
Expand Up @@ -25,6 +25,7 @@
*------------------------------------------------------------------------*/

package com.rhomobile.rhodes.fcm;

import android.content.Context;
import android.content.Intent;
import android.app.NotificationManager;
Expand All @@ -45,13 +46,27 @@
import java.util.Map;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.TimeUnit;
import android.os.Handler;


public class FCMIntentService extends FirebaseMessagingService {

private static final String TAG = FCMIntentService.class.getSimpleName();
public static final FCMListener listener = FCMListener.getInstance();
static private String lastHandledIntent = null;

private static FirebaseMessagingService savedService = null;
private static Map<String, Intent> savedIntents = new HashMap<String, Intent>();

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
lastHandledIntent = null;
savedIntents.remove(remoteMessage.getMessageId());

Logger.W(TAG, "FCM: onMessageReceived()");
Map<String, String> params = new HashMap<String, String>();
params.put("id", remoteMessage.getMessageId());
params.put("from", remoteMessage.getFrom());
Expand All @@ -68,6 +83,69 @@ public void onMessageReceived(RemoteMessage remoteMessage) {

Logger.W(TAG, "FCM: push message: " + remoteMessage.getNotification().getBody());
Logger.W(TAG, "FCM: push message in JSON: " + jsonObject.toString());

PushContract.handleMessage(ContextFactory.getContext(), jsonObject.toString(), FCMFacade.FCM_PUSH_CLIENT);
}



@Override
public void onDeletedMessages() {
Logger.W(TAG, "FCM: onDeletedMessages()");

}

@Override
synchronized public void handleIntent(Intent intent) {
Logger.W(TAG, "FCM: onHandleIntent()");
savedService = this;
if (intent.getExtras() != null) {
for (String key : intent.getExtras().keySet()) {
Object value = intent.getExtras().get(key);
//Logger.W(TAG, "Key: " + key + " Value: " + value);
if (key.equals("google.message_id")){
savedIntents.put((String) value, intent);
lastHandledIntent = (String) value;
Logger.W(TAG, "FCM: onHandleIntent() : message id captured");
}
}
}
super.handleIntent(intent);
}

public static void tryToHandleIntent(String value){
try{
if (savedService != null){
Logger.W(TAG, "FCM: tryToHandleIntent() - trying to handle intent");
if (savedIntents.containsKey(value)){
savedService.handleIntent(savedIntents.get(value));
Logger.W(TAG, "FCM: tryToHandleIntent() - intent handled");
}
}
}catch(Exception e){
Logger.W(TAG, "FCM: tryToHandleIntent() - can't handle intent");
}
}


static public void resume()
{
if (lastHandledIntent != null){

Timer timerObj = new Timer();
TimerTask timerTaskObj = new TimerTask() {
public void run() {
if (lastHandledIntent != null){
tryToHandleIntent(lastHandledIntent);
}
}
};
timerObj.schedule(timerTaskObj, 1000);


}
}



}

0 comments on commit b68a0c3

Please sign in to comment.