I want to download a pfd file when ever a specific notification is pushed via FCM.
I have wrote a service which extends FirebaseMessagingService and in onMessageReceived event after showing the notification I want to download the file. This process only works when application is active when application is in background it does not work.
Is this because of a restriction or you need a work around to achieve this?
Basic Idea is to send a push notification with a URL in it and accessing the URL in onMessageReceived will download a file from web server. Even if the application is stopped/ in background.
Here is the code.
#Override
public void onMessageReceived(RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);
// TODO(developer): 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. See sendNotification method below.
RemoteMessage.Notification notification = remoteMessage.getNotification();
Map<String, String> data = remoteMessage.getData();
downloadPdf("www.dummmy.file/file.pdf");
}
public void downloadPdf(String url) {
DownloadManager downloadmanager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
Uri Download_Uri = Uri.parse(url);
DownloadManager.Request request = new DownloadManager.Request(Download_Uri);
request.setTitle("My File");
request.setDescription("Downloading");
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
request.setVisibleInDownloadsUi(false);
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS,"downloadfileName");
downloadmanager.enqueue(request);
}
Related
I'm using FirebaseMessagingService for my push notifications. It is working as intented in app. onMessageReceived is called, I can get notification title and body + data payload and send reaction depends on payload data to my Activity using LocalBroadcastManager.
But there is problem with background push notification. This will only show notification in background, but it is not created in onMessageReceived because this function cannot be called if app is in background.
According to documentation, data part of push notification is stored in extras and can be found when Activity is resumed. I have function for this already and its working. But problem is that I dont have title and message, because it was not send to onMessageReceived and I cannot catch this information.
Is there any way how to obtain it? Because I need to show it in my dialog window inside app. Push notification is not only text and title and it is not just information for user, but it will do some action.
Receiving notification title, body and payload in FirebaseMessagingService:
override fun onMessageReceived(msg: RemoteMessage?) {
val pNotification = msg?.notification
val payload = msg?.data
if (pNotification != null){
val ntitle = pNotification.title
val nMsg = pNotification.body
}
//working with payload - creating notification and Intent for LocalBroadcastmanager
}
Catching payload from extras inside onResume()
private fun maybeFindBackgroundPayload(extras: Bundle?){
if (extras != null){
extras.keySet().forEach { key->
val keyValue = extras[key]
App.log("BackgroundKeyValues: $keyValue")
if (key.equals("gcm.notification.body", ignoreCase = true) && keyValue != null){
...
//Working with payload data key value - dont have title and body
}
}
}
}
Unfortunately, it is not possible the way you want it to work.
If the push notification has a notification part in the payload and your app is in the background, onMessageReceived will never be called. The notification is automatically created and displayed by firebase. There is no way to intercept this and modify the notification or do any other operation for that matter. Your app will only get control once the notification is clicked.
When the app is in foreground, as you mentioned, onMessageReceived will always be called.
The only way to ensure that onMessageReceived is called in all situations(foreground, background and app killed) is to send a data-only payload from firebase. That is, remove the notification part of the firebase payload body and put the content(title,body etc) in the data part of the payload. And in your onMessageReceived assign this title, body etc to the notification you are creating.
Here is how your payload from firebase will need to be:
{
"registration_ids":[
"firebase_token"
],
"data":{
"body":"body of notification",
"title":"title of notification",
"otherdatakey":"otherdatavalue"
}
}
Then in your onMessageReceived function, you need to explicitly extract the title and body and assign it to the notification(along with any other operations you want to do of course):
#Override
public void onMessageReceived(RemoteMessage message) {
Map<String, String> dataMap = message.getData();
String title = dataMap.get("title");
String body = dataMap.get("body");
String otherdatavalue = dataMap.get("otherdatakey");
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setContentTitle(title)
.setContentText(body)
.setContentIntent(pendingIntent);//To handle click of notification
}
I am using FCM in my project and when trying to test the incoming notifications with the firebase "compose notification" feature I am putting a title, a body and an image URL to the message and it shows what it should look like - a rich notification with image. But the notification that is being sent to me is a normal one without any image.
here is the firebase UI and what is suposed to happen -
My issue is that I am getting only the text, without the image.
here is my MyFirebaseMessagingService class -
public class MyFirebaseMessagingService extends FirebaseMessagingService {
public static final String RECEIVED_FCM_ACTION = "com.onemdtalent.app.RECEIVED_FCM_ACTION";
public static final String BD_KEY_BODY = "BD_KEY_BODY";
#Override
public void onMessageReceived(RemoteMessage remoteMessage) {
// [START_EXCLUDE]
// There are two types of messages data messages and notification messages. Data messages are handled
// here in onMessageReceived whether the app is in the foreground or background. Data messages are the type
// traditionally used with GCM. Notification messages are only received here in onMessageReceived when the app
// is in the foreground. When the app is in the background an automatically generated notification is displayed.
// When the user taps on the notification they are returned to the app. Messages containing both notification
// and data payloads are treated as notification messages. The Firebase console always sends notification
// messages. For more see: https://firebase.google.com/docs/cloud-messaging/concept-options
// [END_EXCLUDE]
String image = remoteMessage.getData().get("image");
Timber.d("onMessageReceived: %s", remoteMessage.getFrom());
// Check if message contains a notification payload.
if (remoteMessage.getNotification() != null) {
String body = remoteMessage.getNotification().getBody();
Timber.d("Message Notification Body: %s", body);
// broadcast
Intent localIntent = new Intent(RECEIVED_FCM_ACTION);
localIntent.putExtra(BD_KEY_BODY, image);
LocalBroadcastManager.getInstance(this).sendBroadcast(localIntent);
}
}
}
As I sayed, I am getting only the text without the image. what am I missing?
Solved - I used an old version of firebase messaging dependency and I updated it, including my entire project to androidX and now I can see the images :)
I have implemented firebase admin SDK in NodeJS and trying to send push notification from server.My problem is I am not getting data part in my notification(account and balance).It is only showing notification part(title and body).
I have implemented server side code something like this:
const admin = require("firebase-admin");
const serviceAccount = require("./my_service_account.json");
admin.initializeApp({
credential: admin.credential.cert(serviceAccount),
databaseURL: "https://<myapp name>.firebaseio.com"
});
var payload = {
notification: {
title: "Hello",
body: "How are you."
},
data: {
account: "Savings",
balance: "$3020.25"
}
};
admin.messaging().sendToDevice(registrationToken,payload)
.then((response) =>{
console.log("Response", response);
}).catch((error) => {
console.log("Error", error);
});
On client side I am doing like this below:
public class MessagingReceive extends FirebaseMessagingService {
#Override
public void onMessageReceived(RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);
if(remoteMessage.getData().size() > 0){
Map<String, String> data = remoteMessage.getData();
handleData(data);
}
}
private void handleData(Map<String,String> data){
String title = data.get("account");
String body = data.get("balance");
Intent intent = new Intent(MessagingReceive.this,MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this,0,intent,PendingIntent.FLAG_ONE_SHOT);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this);
notificationBuilder.setContentTitle(title);
notificationBuilder.setContentText(body);
notificationBuilder.setAutoCancel(true);
notificationBuilder.setSmallIcon(R.mipmap.ic_launcher);
notificationBuilder.setContentIntent(pendingIntent);
NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0,notificationBuilder.build());
}
}
Someone let me know how can I get data part in my notification. Any help would be appreciated.
THANKS
According to Handling messages in Receive messages in an Android app section
Messages with both notification and data payload, when received in the background. In this case, the notification is delivered to the device’s system tray, and the data payload is delivered in the extras of the intent of your launcher Activity.
Now , according to Notification messages with optional data payload
App behavior when receiving messages that include both notification and data payloads depends on whether the app is in the background or the foreground essentially, whether or not it is active at the time of receipt.
When in the background, apps receive the notification payload in the notification tray, and only handle the data payload when the user taps on the notification.
When in the foreground, your app receives a message object with both payloads available.
so during the time the app is in the background , the notification obj in your payload is available in order to build a standart notification (title,body).
to get the payload data object when the user taps on the notification for example is implemented by intent .
#Override
public void onMessageReceived(RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);
String account;
String balance;
String body = remoteMessage.getNotification().getBody();
String title = remoteMessage.getNotification().getTitle();
if(remoteMessage.getData().size() > 0){
Map<String, String> data = remoteMessage.getData();
account = data.get("account");
balance = data.get("balance");
}
handleData(title,body,account,balance);
}
private void(String body , String title , Stirng account , String balance){
Intent intent = new Intent(MessagingReceive.this,MainActivity.class);
intent.putExtra("account",account);
intent.putExtra("balance",balance);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent =
PendingIntent.getActivity(this,0,intent,PendingIntent.FLAG_ONE_SHOT);
// rest of your code
}
Here i used DownloadManger to download and it will show the progress in Notification bar
DownloadManager.Request request = new DownloadManager.Request(uri);
request.setTitle("Data Download");
//Setting description of request
request.setDescription("Android Data download using DownloadManager.");
request.allowScanningByMediaScanner();
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
registerReceiver(downloadReceiver, filter);
Broadcast receiver to receive click action(DownloadManager.ACTION_NOTIFICATION_CLICKED) during downloading and the action is working during download
private BroadcastReceiver downloadReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (DownloadManager.ACTION_NOTIFICATION_CLICKED.equals(action))
{
startActivity(new Intent(DownloadManager.ACTION_VIEW_DOWNLOADS));
}
}
};
but after download DownloadManager.ACTION_NOTIFICATION_CLICKED is not working to redirect to downloads app.
Can any one help me how can i redirect after clicking on DownloadManger notification(after complete download) to downloads app ????????????
when my app in foreground then the notification url link open automatically without user's any contribution.
Now,I want to keep notification in notification bar and when user press the notification that will open the url link.
One more thing I have to mention that,when my app in background it works fine.Notification arrive in notification bar.
I want same thing to do for foreground.
please help,
Thanks in advance.
my FirebaseMessagingServices
public class MyFirebaseMessagingService extends FirebaseMessagingService {
private static final String TAG = "MyFirebaseMsgService";
#Override
public void onMessageReceived(RemoteMessage remoteMessage) {
if(remoteMessage.getData().size()>0){
String url = remoteMessage.getData().get("url");
Intent intent = new Intent("com.bellecarib_FCM-MESSAGE");
intent.putExtra("url",url);
LocalBroadcastManager localBroadcastManager = LocalBroadcastManager.getInstance(this);
localBroadcastManager.sendBroadcast(intent);
}
}
here is my MainActivity code
LocalBroadcastManager.getInstance(this).registerReceiver(mHandler, new IntentFilter("com.bellecarib_FCM-MESSAGE"));
if(getIntent().getExtras() != null){
for(String key: getIntent().getExtras().keySet()){
if(key.equals(("url"))){
mwebView.loadUrl(getIntent().getExtras().getString(key));
Toast.makeText(getApplicationContext(),"Opening The Notification Page",Toast.LENGTH_LONG).show();
}
}
}
The behaviour you are seeing , is when you send notification payload from FCM. If you want a customised behaviour, then send only a data payload and when you receive the message, throw up a notification manually. This means more work for your app (at the cost of customisation). Also data payloads are delivered with a normal priority which means if your device is in idle mode, then FCM Data payload messages are not delivered immediately and deferred till the next maintenance window (Read upon Android Doze mode and App Standby).
Hope this is clear