import com.google.firebase.messaging.FirebaseMessagingService;
import com.google.firebase.messaging.RemoteMessage;
import com.netcore.android.smartechpush.SmartPush;
import org.json.JSONException;
import org.json.JSONObject;
import java.lang.ref.WeakReference;
import android.content.Context;
import androidx.annotation.NonNull;
public class FCM_Service extends FirebaseMessagingService {
@Override
public void onNewToken(@NonNull String token) {
super.onNewToken(token);
SmartPush.getInstance(new WeakReference<Context>(this)).setDevicePushToken(token);
//<Your code>
}
@Override
public void onMessageReceived(@NonNull RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);
boolean pushFromSmartPush = false;
try {
pushFromSmartPush = SmartPush.getInstance(new WeakReference<Context>(this)).isNotificationFromSmartech(new JSONObject(remoteMessage.getData().toString()));
} catch (JSONException e) {
throw new RuntimeException(e);
}
if(pushFromSmartPush){
SmartPush.getInstance(new WeakReference<>(getApplicationContext())).handlePushNotification(remoteMessage.getData().toString());
} else {
// Notification received from other sources
}
}
}
dependies:
pease add project level build.gralde file
dependencies {
classpath 'com.google.gms:google-services:4.3.15'
}
please add in android(app level build.gradleI file
apply plugin: 'com.google.gms.google-services'
dependencies {
implementation platform('com.google.firebase:firebase-bom:28.4.2')
implementation 'com.google.firebase:firebase-messaging'
}
</manifest>
<application>
<service
android:name=".FCM_Service"
android:exported="true">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
</application>
</manifest>