How to make a connection between a service and Delphi under Android? - android

I add this in the android manifest to create a service :
<service
android:name=".MyFirebaseInstanceIDService">
<intent-filter>
<action android:name="com.google.firebase.INSTANCE_ID_EVENT"/>
</intent-filter>
</service>
and i have this code in java :
public class MyFirebaseInstanceIDService extends FirebaseInstanceIdService {
private static final String TAG = "MyFirebaseIIDService";
#Override
public void onTokenRefresh() {
// Get updated InstanceID token.
String refreshedToken = FirebaseInstanceId.getInstance().getToken();
Log.d(TAG, "Refreshed token: " + refreshedToken);
}
// [END refresh_token]
}
now the question, how i can make the connection between MyFirebaseInstanceIDService and Delphi ? how from MyFirebaseInstanceIDService.onTokenRefresh i can fire a method declared in delphi ? Or how from delphi i can get the instance of MyFirebaseInstanceIDService
do we need to use indent for this ?

Related

Firebase Cloud Messaging notifications not being received on Android app

I'm new to firebase. I have implemented cloud messaging in my project yet when I send the messages from the firebase console I didn't receive it .
Here is my FirebaseInstanceIdService
import android.util.Log;
import com.google.firebase.iid.FirebaseInstanceId;
import com.google.firebase.iid.FirebaseInstanceIdService;
public class MyFireBaseInstanceIDService extends FirebaseInstanceIdService {
private static final String TAG = MyFireBaseInstanceIDService.class.getSimpleName();
#Override
public void onTokenRefresh() {
// Get updated InstanceID token.
String refreshedToken = FirebaseInstanceId.getInstance().getToken();
Log.d(TAG, "Refreshed token: " + refreshedToken);
// TODO: Implement this method to send any registration to your app's servers.
sendRegistrationToServer(refreshedToken);
}
// [END refresh_token]
/**
* Persist token to third-party servers.
*
* Modify this method to associate the user's FCM InstanceID token with any server-side account
* maintained by your application.
*
* #param token The new token.
*/
private void sendRegistrationToServer(String token) {
// Add custom implementation, as needed.
}}
and this is my FirebaseMessagingService
import com.google.firebase.messaging.FirebaseMessagingService;
import com.google.firebase.messaging.RemoteMessage;
public class GCMNotificationIntentService extends FirebaseMessagingService {
// Sets an ID for the notification, so it can be updated
public GCMNotificationIntentService() {
super();
}
#Override
public void onMessageReceived(RemoteMessage message) {
}
}
also I have included the two services into my manifest.xml
<service
android:name=".MyFireBaseInstanceIDService">
<intent-filter>
<action android:name="com.google.firebase.INSTANCE_ID_EVENT"/>
</intent-filter>
</service>
<service
android:name=".GCMNotificationIntentService">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT"/>
</intent-filter>
</service>

Receive notification in chat with fcm

Hi there I'm building the chat app using firebase and I would like to push notification when a user send message to another user with FCM.
Any help please
add compile 'com.google.firebase:firebase-messaging:11.8.0' to your app/build.gradle file.
and below code to your AndroidManifest.xml
<service
android:name=".MyFirebaseMessagingService"
android:exported="false">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
<service
android:name=".MyFirebaseInstanceIdService"
android:exported="false">
<intent-filter>
<action android:name="com.google.firebase.INSTANCE_ID_EVENT" />
</intent-filter>
</service>
MyFirebaseMessagingService.java
public class MyFirebaseMessagingService extends FirebaseMessagingService {
private static final String TAG = "MyFMService";
#Override
public void onMessageReceived(RemoteMessage remoteMessage) {
// Handle data payload of FCM messages.
Log.d(TAG, "FCM Message Id: " + remoteMessage.getMessageId());
Log.d(TAG, "FCM Notification Message: " +
remoteMessage.getNotification());
Log.d(TAG, "FCM Data Message: " + remoteMessage.getData());
}
}
MyFirebaseInstanceIdService.java
public class MyFirebaseInstanceIdService extends FirebaseInstanceIdService {
private static final String TAG = "MyFirebaseIIDService";
private static final String FRIENDLY_ENGAGE_TOPIC = "friendly_engage";
/**
* The Application's current Instance ID token is no longer valid
* and thus a new one must be requested.
*/
#Override
public void onTokenRefresh() {
// If you need to handle the generation of a token, initially or
// after a refresh this is where you should do that.
String token = FirebaseInstanceId.getInstance().getToken();
Log.d(TAG, "FCM Token: " + token);
// Once a token is generated, we subscribe to topic.
FirebaseMessaging.getInstance()
.subscribeToTopic(FRIENDLY_ENGAGE_TOPIC);
}
}
Hope this will help you

Fetch old notification from fcm Android

I am using FCM for notification and it is working good in all scenarios in Foreground and background. My problem is when I am logout from my application and another user send me message, I am not getting any notification as I am logout which is fine, but when I again login in my application I want to receive that old unread notification so how to do this any help would be appreciated following is the code I used
public class FirebaseIDService extends FirebaseInstanceIdService {
private static final String TAG = "FirebaseIDService";
Context ctx = this;
#Override
public void onTokenRefresh() {
// Get updated InstanceID token.
try {
String refreshedToken = FirebaseInstanceId.getInstance().getToken();
Log.d(TAG, "Refreshed token: " + refreshedToken);
} catch (Exception e) {
e.printStackTrace();
}
}
}
public class MyFirebaseMessagingService extends FirebaseMessagingService {
private static final String TAG = "FCM Service";
#Override
public void onMessageReceived(RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);
FirebaseDatabase.getInstance().setPersistenceEnabled(true);
Log.e("remote", remoteMessage.getData().get("type") + "");
Log.e("remoteMessage", remoteMessage.getData() + "");
Log.e("remoteMessagebody", remoteMessage.getData().get("body") + "");
// handling other data also according to my app which i am not mentioning
}
}
I have used this code for fetching old notification but didnot worked
FirebaseDatabase.getInstance().setPersistenceEnabled(true);
and in manifest
<service
android:name=".MyFirebaseMessagingService"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
<service android:name=".FirebaseIDService">
<intent-filter>
<action android:name="com.google.firebase.INSTANCE_ID_EVENT" />
</intent-filter>
</service>
and in gradle
compile 'com.google.firebase:firebase-messaging:9.2.1'
An FCM message has its own lifetime -- 4 weeks default, could be modified by setting time_to_live in your payload. After that time, the message would be discarded.
What you could do is implement that each notif is saved in a your Server DB, if it is not read yet and you detect that the user re-logs in, re-send them as push notifications, or simply retrieve them in your app and display them as notifications.

Firebase (FCM) - Users stop receiving notifications on subscribed topic

I'm using Firebase to send notifications to users who installed my app.
After a while some (not many) of them stop receiving notifications although they didn't unsubscribed.
I have a class that suppose to refresh then token, so I have no idea what can cause that.
public class MyFirebaseInstanceIDService extends FirebaseInstanceIdService {
private static final String TAG = "MyFirebaseIIDService";
#Override
public void onTokenRefresh() {
String refreshedToken = FirebaseInstanceId.getInstance().getToken();
Log.d(TAG, "Refreshed token: " + refreshedToken);
}
}
Any idea?
Thanks
Try the code snippet below:
#Override
public void onTokenRefresh() {
String refreshedToken = FirebaseInstanceId.getInstance().getToken();
Log.d(TAG, "Refreshed token: " + refreshedToken);
if(null != refreshedToken) {
FirebaseMessaging.getInstance().subscribeToTopic("news");
}
}
Have you declared the service in your manifest, like so?
<service android:name="MyFirebaseInstanceIDService" android:exported="false">
<intent-filter>
<action android:name="com.google.firebase.INSTANCE_ID_EVENT"/>
</intent-filter>
</service>

Migrating GCM to FirebaseCM: onMessageReceived() not called in foreground

I'm trying to migrate Android client app from Google Cloud Messaging to Firebase Cloud Messaging. I strictly followed an official tutorial, but didn't succeed - onMessageReceived() method is not called when app in foreground.
So here are code snippets that I touched.
build.gradle (project level)
dependencies {
//other stuff
classpath 'com.google.gms:google-services:3.0.0'
}
build.gradle (app level)
apply plugin: 'com.google.gms.google-services' //this line in the bottom
and
dependencies {
//other stuff here
compile 'com.google.firebase:firebase-messaging:9.0.0'
}
AndroidManifest.xml
<service android:name=".service.FirebaseService">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
and
<service
android:name=".service.MyInstanceIDListenerService"
android:exported="false">
<intent-filter>
<action android:name="com.google.firebase.INSTANCE_ID_EVENT" />
</intent-filter>
</service>
and
<service
android:name=".service.RegistrationIntentService"
android:exported="false" />
as childs of <application> tag.
MyInstanceIDListenerService.java
public class MyInstanceIDListenerService extends FirebaseInstanceIdService {
final String TAG = "Firebase instance id";
#Override
public void onTokenRefresh() {
String refreshedToken = FirebaseInstanceId.getInstance().getToken();
Log.d(TAG, "Refreshed token: " + refreshedToken);
sendRegistrationToServer(refreshedToken);
}
private void sendRegistrationToServer(String token) {
Intent intent = new Intent(RegistrationIntentServiceEvent.TOKEN);
intent.putExtra(RegistrationIntentServiceEvent.TOKEN, token);
LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
}
}
FirebaseService.java
public class FirebaseService extends FirebaseMessagingService {
//OtherStuff
#Override
public void onMessageReceived(RemoteMessage message){
String from = message.getFrom();
Map data = message.getData();
Log.d(TAG, "Message: " + from); //Never appears in logcat
//other stuff
}
}
RegistrationIntentService
public class RegistrationIntentService extends IntentService {
private static final String TAG = "RegIntentService";
public RegistrationIntentService() {
super(TAG);
}
#Override
protected void onHandleIntent(Intent intent) {
try {
synchronized (TAG) {
String token = FirebaseInstanceId.getInstance().getToken();
Log.i(TAG, "GCM Registration Token: " + token);//This works fine
//and shows this line - 12-19 17:59:33.295 3146-5386/ru.bpc.mobilebank.bpc I/RegIntentService: GCM Registration Token: *some_token*
sendRegistrationToServer(token);
}
} catch (Exception e) {
Log.d(TAG, "Failed to complete token refresh", e);
}
}
private void sendRegistrationToServer(String token) {
Intent intent = new Intent(RegistrationIntentServiceEvent.TOKEN);
intent.putExtra(RegistrationIntentServiceEvent.TOKEN, token);
LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
}
}
Please, tell if i did something wrong and why registrations seems to be done properly, but the onMessageReceived() method is never called even if the app is in foreground. Thanks in advance.
P.S. By the way, is adding SHA-1 keys in Firebase console necessary? maybe this could cause the problem? But Firebase says this action is optional.
if you are using FCM then you should replace this
<service android:name=".service.GcmService">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
to
<service android:name=".service.FirebaseService">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
your service not registered properly..

Categories

Resources