Skip to content

Commit

Permalink
update code deprecate backgroundForground listener and update param p…
Browse files Browse the repository at this point in the history
…ackageId for registerDeviceToken
  • Loading branch information
Arief Nur Putranto committed Nov 6, 2024
1 parent 0908d6a commit 255d079
Show file tree
Hide file tree
Showing 6 changed files with 112 additions and 20 deletions.
Original file line number Diff line number Diff line change
@@ -1,15 +1,61 @@
package com.qiscus.sdk.chat.core;

import androidx.annotation.NonNull;
import androidx.lifecycle.DefaultLifecycleObserver;
import androidx.lifecycle.LifecycleOwner;
import com.qiscus.sdk.chat.core.util.QiscusLogger;
import com.qiscus.sdk.chat.core.util.QiscusServiceUtil;

import android.app.Activity;
import android.util.Log;
enum BackgroundForegroundListener implements DefaultLifecycleObserver {
INSTANCE;
private static boolean foreground;
@Override
public void onCreate(@NonNull LifecycleOwner owner) {
DefaultLifecycleObserver.super.onCreate(owner);
QiscusLogger.print("BackgroundForegroundListener", "onCreate");
}

@Override
public void onStart(@NonNull LifecycleOwner owner) {
DefaultLifecycleObserver.super.onStart(owner);
QiscusLogger.print("BackgroundForegroundListener", "onStart");
foreground = true;
if (!QiscusServiceUtil.isMyServiceRunning() && !QiscusCore.isSyncServiceDisabledManually()) {
QiscusCore.startSyncService();
}
}

@Override
public void onResume(@NonNull LifecycleOwner owner) {
DefaultLifecycleObserver.super.onResume(owner);
QiscusLogger.print("BackgroundForegroundListener", "onResume");
}

@Override
public void onPause(@NonNull LifecycleOwner owner) {
DefaultLifecycleObserver.super.onPause(owner);
QiscusLogger.print("BackgroundForegroundListener", "onPause");
}

@Override
public void onStop(@NonNull LifecycleOwner owner) {
DefaultLifecycleObserver.super.onStop(owner);
QiscusLogger.print("BackgroundForegroundListener", "onStop");
foreground = false;
}

@Override
public void onDestroy(@NonNull LifecycleOwner owner) {
DefaultLifecycleObserver.super.onDestroy(owner);
foreground = false;
QiscusLogger.print("BackgroundForegroundListener", "onDestroy");
}

class MyAppLifecycleObserver : ApplicationLifecycleObserver() {
override fun onAppForeground(owner: LifecycleOwner) {
Log.d(this::class.simpleName, "onAppForeground: Application resumed")
boolean isForeground() {
return foreground;
}

override fun onAppBackground(owner: LifecycleOwner) {
Log.d(this::class.simpleName, "onAppBackground: Application paused")
public static void setAppActiveOrForground(){
foreground = true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,13 @@

package com.qiscus.sdk.chat.core;

import android.util.Log;

import androidx.annotation.NonNull;
import androidx.lifecycle.DefaultLifecycleObserver;
import androidx.lifecycle.Lifecycle;
import androidx.lifecycle.LifecycleObserver;
import androidx.lifecycle.LifecycleOwner;
import androidx.lifecycle.OnLifecycleEvent;

import com.qiscus.sdk.chat.core.data.remote.QiscusPusherApi;
Expand Down
37 changes: 29 additions & 8 deletions chat-core/src/main/java/com/qiscus/sdk/chat/core/QiscusCore.java
Original file line number Diff line number Diff line change
Expand Up @@ -218,15 +218,15 @@ public static void initWithCustomServer(Application application, String qiscusAp
}

}

QiscusActivityCallback.INSTANCE.setAppActiveOrForground();
// QiscusActivityCallback.INSTANCE.setAppActiveOrForground();
BackgroundForegroundListener.INSTANCE.setAppActiveOrForground();

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
checkExactAlarm(application);
}

getAppConfig();
configureFcmToken();
//configureFcmToken();
}


Expand Down Expand Up @@ -321,8 +321,8 @@ private static void getAppConfig() {

startSyncService();
startNetworkCheckerService();
ProcessLifecycleOwner.get().getLifecycle().addObserver(QiscusActivityCallback.INSTANCE);

//ProcessLifecycleOwner.get().getLifecycle().addObserver(QiscusActivityCallback.INSTANCE);
ProcessLifecycleOwner.get().getLifecycle().addObserver(BackgroundForegroundListener.INSTANCE);
}, throwable -> {
QiscusErrorLogger.print(throwable);
QiscusApi.getInstance().reInitiateInstance();
Expand All @@ -333,7 +333,8 @@ private static void getAppConfig() {
QiscusCore.setCacheMqttBrokerUrl(appComponent.getMqttBrokerUrl(), false);
startSyncService();
startNetworkCheckerService();
ProcessLifecycleOwner.get().getLifecycle().addObserver(QiscusActivityCallback.INSTANCE);
//ProcessLifecycleOwner.get().getLifecycle().addObserver(QiscusActivityCallback.INSTANCE);
ProcessLifecycleOwner.get().getLifecycle().addObserver(BackgroundForegroundListener.INSTANCE);
});

}
Expand Down Expand Up @@ -1044,6 +1045,24 @@ public static void registerDeviceToken(String token) {
.setFcmToken(token);
}

/**
* Set the FCM token to configure push notification with firebase cloud messaging
*
* @param token the token (fcmToken) & packageId
*/
public static void registerDeviceToken(String token, String packageId) {
if (hasSetupUser() && getChatConfig().isEnableFcmPushNotification()) {
QiscusApi.getInstance().registerDeviceToken(token, packageId)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(aVoid -> {
}, throwable -> QiscusErrorLogger.print("SetFCMToken", throwable));
}

appComponent.getLocalDataManager()
.setFcmToken(token);
}

/**
* Remove the FCM token
*
Expand Down Expand Up @@ -1083,11 +1102,13 @@ private static void configureFcmToken() {
* @return true if apps on foreground, and false if on background
*/
public static boolean isOnForeground() {
return QiscusActivityCallback.INSTANCE.isForeground();
// return QiscusActivityCallback.INSTANCE.isForeground();
return BackgroundForegroundListener.INSTANCE.isForeground();
}

public static void setAppInForeground(){
QiscusActivityCallback.INSTANCE.setAppActiveOrForground();
//QiscusActivityCallback.INSTANCE.setAppActiveOrForground();
BackgroundForegroundListener.INSTANCE.setAppActiveOrForground();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import android.net.Uri;
import android.os.Build;
import android.provider.Settings;

import androidx.annotation.NonNull;
import androidx.core.util.Pair;
Expand Down Expand Up @@ -817,17 +818,22 @@ public Observable<Void> updateCommentStatus(long roomId, long lastReadId, long l

@Deprecated
public Observable<Void> registerFcmToken(String fcmToken) {
return api.registerFcmToken(QiscusHashMapUtil.registerOrRemoveFcmToken(fcmToken))
return api.registerFcmToken(QiscusHashMapUtil.registerFcmToken(fcmToken, QiscusCore.getApps().getPackageName()))
.map(jsonElement -> null);
}

public Observable<Void> registerDeviceToken(String token) {
return api.registerFcmToken(QiscusHashMapUtil.registerOrRemoveFcmToken(token))
return api.registerFcmToken(QiscusHashMapUtil.registerFcmToken(token, QiscusCore.getApps().getPackageName()))
.map(jsonElement -> null);
}

public Observable<Void> registerDeviceToken(String token, String packageId) {
return api.registerFcmToken(QiscusHashMapUtil.registerFcmToken(token, packageId))
.map(jsonElement -> null);
}

public Observable<Void> removeDeviceToken(String token) {
return api.removeDeviceToken(QiscusHashMapUtil.registerOrRemoveFcmToken(token))
return api.removeDeviceToken(QiscusHashMapUtil.removeFcmToken(token))
.map(jsonElement -> null);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.qiscus.sdk.chat.core.util;

import android.provider.Settings;

import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.qiscus.sdk.chat.core.BuildConfig;
Expand Down Expand Up @@ -161,16 +163,27 @@ public static HashMap<String, Object> updateCommentStatus(String roomId, String
return hashMap;
}

public static HashMap<String, Object> registerOrRemoveFcmToken(String fcmToken) {
public static HashMap<String, Object> registerFcmToken(String fcmToken, String packageId) {
HashMap<String, Object> hashMap = new HashMap<>();

hashMap.put("device_platform", "android");
hashMap.put("device_token", fcmToken);
hashMap.put("is_development", BuildConfig.DEBUG);
hashMap.put("bundle_id", packageId);
hashMap.put("device_id", Settings.Secure.getString(QiscusCore.getApps().getContentResolver(), Settings.Secure.ANDROID_ID));

return hashMap;
}

public static HashMap<String, Object> removeFcmToken(String fcmToken) {
HashMap<String, Object> hashMap = new HashMap<>();

hashMap.put("device_platform", "android");
hashMap.put("device_token", fcmToken);
hashMap.put("is_development", BuildConfig.DEBUG);
return hashMap;
}

public static HashMap<String, Object> getChatRooms(Object roomIds, Object roomUniqueIds, boolean showParticipants,
boolean showRemoved) {
HashMap<String, Object> hashMap = new HashMap<>();
Expand Down
3 changes: 2 additions & 1 deletion dependencies.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,8 @@ ext {
]

lifeCircleDeb = [
lifecycle : "androidx.lifecycle:lifecycle-extensions:2.2.0"
lifecycle : "androidx.lifecycle:lifecycle-extensions:2.2.0",
lifecycleProcess : "androidx.lifecycle:lifecycle-process"
]

rxunfurlDeb = [
Expand Down

0 comments on commit 255d079

Please sign in to comment.