I am implementing FCM notifications in Android, but how does notifications differ depending on the app status (background vs. foreground)?
Update 2019 August.
[wasted couple of days just because py doesn't support latest changes for notification]
Just add image=url into your notification object.
It works in Native Android. Just add image
into notification object. Also please note that in Python library image field doesn't exist. [As of Aug 19] https://github.com/firebase/firebase-admin-python
I used PHP and this library https://github.com/kreait/firebase-php/ Its super simple and more importantly, it works for big image notification when app is in background or has been killed.
Code:
<?php
require __DIR__.'/vendor/autoload.php';
require __DIR__.'/simple_html_dom.php';
use Kreait\Firebase;
use Kreait\Firebase\ServiceAccount;
use Kreait\Firebase\Messaging\Notification;
use Kreait\Firebase\Messaging\CloudMessage;
$serviceAccount = ServiceAccount::fromJsonFile('/path/to/cred.json');
$firebase = (new Firebase\Factory())->withServiceAccount($serviceAccount)->create();
$messaging = $firebase->getMessaging();
// this works when app is closed or in bg
$notification = Notification::fromArray([
'title' => $title,
'body' => $body,
'image' => $imageUrl,
]);
// for foreground process
$data = [
'image' => $imageUrl,
'news_id' => $news_id,
];
$topic = 'default_topic1';
$message = CloudMessage::withTarget('topic', $topic)
->withNotification($notification) // optional
->withData($data);
$messaging->send($message);
print_r($message);
You can send messages using this rest client tool.Using this tool You can send messages to client app in background and foreground too. To send a message using API, you can use a tool called AdvancedREST Client, its a chrome extension, and send a message with the following parameters.
Rest client tool Link: https://chrome.google.com/webstore/detail/advanced-rest-client/hgmloofddffdnphfgcellkdfbfbjeloo
use this url:- https://fcm.googleapis.com/fcm/send Content-Type:application/json Authorization:key=Your Server key From or Authoization key(see below ref)
{ "data": { "image": "https://static.pexels.com/photos/4825/red-love-romantic-flowers.jpg", "message": "Firebase Push Message Using API" "AnotherActivity": "True" }, "to" : "device id Or Device token" }
Authorization key can be obtained by visiting Google developers console and click on Credentials button on the left menu for your project. Among the API keys listed, the server key will be your authorization key.
And you need to put tokenID of the receiver in the “to” section of your POST request sent using API.
And This piece of android code //message will contain the Push Message
String message = remoteMessage.getData().get("message1");
//imageUri will contain URL of the image to be displayed with Notification
String imageUri = remoteMessage.getData().get("image");
//If the key AnotherActivity has value as True then when the user taps on notification, in the app AnotherActivity will be opened.
//If the key AnotherActivity has value as False then when the user taps on notification, in the app MainActivity2 will be opened.
String TrueOrFlase = remoteMessage.getData().get("AnotherActivity");
//To get a Bitmap image from the URL received
bitmap = getBitmapfromUrl(imageUri);
sendNotification(message, bitmap, TrueOrFlase);
Push 5otally depends push request received and also device capabilities like in some device battery saber impacts the all what we paas is url image and not actual image in push request so device should have those capabilities to download image and android/ apk have to display the image and firebase or APNS
In case some lands here in 2019, you can simply add an image field to the notification object:
{
notification: {
title: title,
body: body,
image: "http://path_to_image"
},
data: {
click_action: "FLUTTER_NOTIFICATION_CLICK",
your_data: ...,
},
token: token
}
I have tested it using Flutter on Android and I would assume it works on native Android since they both probably use the same native SDK.
Send Big Picture notification from Firebase console : Works for both background and foreground app
Instead of onMessageReceived
, override zzm()
of FirebaseMessagingService
and create your custom notification from here
@Override
public void zzm(Intent intent) {
Log.e(TAG, "zzm : " + intent);
createBigPictureNotification();
}
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
}
FCM notification messages
don't support the largeIcon or bigPicture.
if you need them while in background you can use a FCM data message
.
For data messages the onMessageReceived(message)
method is always called, so you can use the message.getData()
method and create your custom notification.
Read more about notification messages vs data messages here: https://firebase.google.com/docs/cloud-messaging/concept-options#notifications_and_data_messages