Fetch old notification from fcm Android - 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.

Related

FirebaseMessasingService gets notifications from my server, but if I use the FirebaseConsole, my class is not being called

This is what I have inside my AndroidManifest:
<service android:name=".MyFirebaseMessagingService">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
<service android:name="io.smooch.core.service.SmoochService" />
<service android:name=".MyFirebaseInstanceIDService">
<intent-filter>
<action android:name="com.google.firebase.INSTANCE_ID_EVENT" />
</intent-filter>
</service>
I am using version: implementation 'com.google.firebase:firebase-messaging:17.0.0'
This is how my FirebaseMessagingService looks like:
public class MyFirebaseMessagingService extends FirebaseMessagingService {
public void onMessageReceived(final RemoteMessage message) {
Log.i("", "MyFirebaseMessagingService onMessageReceived");
final Map data = message.getData();
FcmService.triggerSmoochNotification(data, this);
final Map bundle = data;
Handler handler = new Handler(Looper.getMainLooper());
handler.post(new Runnable() {
#Override
public void run() {
Log.i("", "GLOBAL intances before GCM:" + Realm.getGlobalInstanceCount(PSApplicationClass.Config));
try {
String title = getString(R.string.alert_title_warning) + " " + getString(R.string.trips_remaining_free, user.getTrips_left());
String message = getString(R.string.alert_freemium_upgrade);
Notification.generateNotificationStandard(MyFirebaseMessagingService.this, title, message, null, null, false, false);
} catch (Exception e) {
Utils.appendLog("GCMIntentService onMessageReceived error: " + e.getMessage(), "E", Constants.PUSH_NOTIFICATIONS);
}
}
});
}
}
And basically I put a breakpoint at the onMessageReceived of this function.
If the server sends a notification to my app, it will be received by this Service, and I can handle it as needed.
BUT. If I use the FirebaseConsole -> GROW -> Could Messaging -> I create a new Message and I send it. My app will get the notifications. but it will be created "automatically" without entering my onMessageReceived method.
How can I force it to do so?
This is the data that I send via the console:
https://s3.amazonaws.com/uploads.hipchat.com/39260/829560/7Pp0KJIErZ8XVZZ/upload.png
What am I doing wrong?
EDIT:
Like I said, I get notifications, but the notifications are being created automatically instead of going through my FirebaseMessaging Service.
I did manage to change the icon like this:
<meta-data
android:name="com.google.firebase.messaging.default_notification_icon"
android:resource="#drawable/notification_icon" />
But I still need to run other code when I get it, not just show it. and that I do not know how

Push notification not call OnMessageReceive when application kill

I am getting push notification when application in foreground/ background/ killed but onMessageReceived not called when application Kill. I write a code to play custom notification sound in onMessageReceived method continouesly. Custom notification sound also play once when application Killed. We are passing Data message and Notification message from cloud server(FCM). I appreciate the help.
MyFirebaseMessagingService.java
public class MyFirebaseMessagingService extends FirebaseMessagingService {
private static final String TAG = "MyFirebaseMessagingServ";
#Override
public void onMessageReceived(RemoteMessage remoteMessage) {
Log.e("##", ">>>>>>>>>: Inside CLASS");
Log.e("##", ">>>>>>>>>: Inside ON MESSAGE RECEIVED");
Map<String, String> data = remoteMessage.getData();
String orderID = data.get("orderId");
String orderType = data.get("orderType");
String orderStatus = data.get("orderStatus");
Intent intentService = new Intent(getApplicationContext(), SoundIntentService.class);
startService(intentService);
}
}
MyFirebaseInstanceIDService.java
public class MyFirebaseInstanceIDService extends FirebaseInstanceIdService {
private static final String TAG = MyFirebaseInstanceIDService.class.getSimpleName();
#Override
public void onTokenRefresh() {
super.onTokenRefresh();
String refreshedToken = FirebaseInstanceId.getInstance().getToken();
// sending reg id to your server
sendRegistrationToServer(refreshedToken);
// Notify UI that registration has completed, so the progress indicator can be hidden.
Intent registrationComplete = new Intent(Config.REGISTRATION_COMPLETE);
registrationComplete.putExtra("token", refreshedToken);
LocalBroadcastManager.getInstance(this).sendBroadcast(registrationComplete);
}
private void sendRegistrationToServer(final String token) {
// sending gcm token to server
Log.e(TAG, "sendRegistrationToServer: " + token);
}
}
Manifest.xml
<service
android:name=".service.SoundIntentService"
android:exported="false" />
<service android:name=".service.MyFirebaseMessagingService">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
<service android:name=".service.MyFirebaseInstanceIDService">
<intent-filter>
<action android:name="com.google.firebase.INSTANCE_ID_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

Push notification not works in firebase

I have created one new app for push notification in https://console.firebase.google.com. I have followed all steps mentioned at last it shows -unknown app- under the head Targeting user segment.
How to test push notification in firebase?
AndroidManifest.xml
`
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
`
First, for using firebase push notifications add the dependencies
dependencies {
compile 'com.google.firebase:firebase-messaging:11.6.2' // this line must be included to use FCM
}
Add a service that extends FirebaseMessagingService
public class MyFirebaseMessagingService extends FirebaseMessagingService {
private static final String TAG = "FCM Service";
#Override
public void onMessageReceived(RemoteMessage remoteMessage) {
// TODO: Handle FCM messages here.
// If the application is in the foreground handle both data and notification messages here.
// Also if you intend on generating your own notifications as a result of a received FCM
// message, here is where that should be initiated.
Log.d(TAG, "From: " + remoteMessage.getFrom());
Log.d(TAG, "Notification Message Body: " + remoteMessage.getNotification().getBody());
}
}
In your Manifest add this
<service android:name=".MyFirebaseMessagingService">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT"/>
</intent-filter>
</service>
Add a service that extends FirebaseInstanceIdService
public class FirebaseIDService extends FirebaseInstanceIdService {
private static final String TAG = "FirebaseIDService";
#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);
}
/**
* 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.
}
}
Add it into the AndroidManifest.xml file, this makes sure that the service is loaded
<service android:name=".FirebaseIDService">
<intent-filter>
<action android:name="com.google.firebase.INSTANCE_ID_EVENT"/>
</intent-filter>
</service>
You are done implementing firebase push notifications !
Test and send your first push notification from firebase console !
hope it helped
For more info check this: https://firebase.google.com/docs/cloud-messaging/android/client?hl
happy coding !

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